如何在 discord.js 中执行禁令之前 dm 用户?

问题描述

有人可以告诉我如何在用户禁止之前向他发送 dm 吗?一切正常,但 dm.看来,如果我把 dm 代码放在现在的位置,用户首先被禁止,然后收到一条直接消息,这显然不起作用,因为机器人和用户不共享服务器。 我的代码

const { discordAPIError } = require("discord.js");
const discord = require('discord.js');
const client = new discord.Client();

module.exports = {
  name: 'ban',description: "Allows you to ban a member from the server!",execute(message,args) {
    const member = message.mentions.users.first();
    const notBanYourself = new discord.MessageEmbed()
    .setColor("#ff0000")
        .setTitle("Ban")
        .setDescription("You cannot ban yourself!")
        .setFooter(message.author.username)
        .setTimestamp()
        ;

    if(member.id === message.author.id) return message.channel.send(notBanYourself);
    if(message.member.permissions.has("BAN_MEMBERS") || message.member.permissions.has("ADMINISTRATOR")){
        const memberTarget = message.guild.members.cache.get(member.id);
        const ban_dm = new discord.MessageEmbed()
        .setColor("#ff0000")
        .setTitle("Ban")
        .setDescription("You got banned from the " + message.guild.name + " Server by " + message.author.username)
        .setFooter(message.author.username)
        .setTimestamp()
        ;
        memberTarget.send(ban_dm)
    if(member) {
        memberTarget.ban({ days: 3,reason:  args.slice(1).join(" ") });
        const banned = new discord.MessageEmbed()
        .setColor("#ff0000")
        .setTitle("Ban")
        .setDescription("User has been banned from the server successfully.")
        .setFooter(message.author.username)
        .setTimestamp()
        ;
        message.channel.send(banned)
    }
        

    else {
        const notBanned = new discord.MessageEmbed()
        .setColor("#ff0000")
        .setTitle("Ban")
        .setDescription("There was an error: You need to mention the user,which you want to ban! \n If you did that,you probably do not have the permission to ban users.")
        .setFooter(message.author.username)
        .setTimestamp()
        ;
        message.channel.send(notBanned)
    }
        
}
  }}

解决方法

尝试使用 .then(),使其仅在 message.channel.send() 事件完成后才发生禁令。

const { DiscordAPIError } = require("discord.js");
const Discord = require('discord.js');
const client = new Discord.Client();

module.exports = {
    name: 'ban',description: "Allows you to ban a member from the server!",execute(message,args) {

        const member = message.mentions.users.first();
        const notBanYourself = new Discord.MessageEmbed()
            .setColor("#ff0000")
            .setTitle("Ban")
            .setDescription("You cannot ban yourself!")
            .setFooter(message.author.username)
            .setTimestamp()

        if (member.id === message.author.id) return message.channel.send(notBanYourself);
        if (message.member.permissions.has("BAN_MEMBERS") || message.member.permissions.has("ADMINISTRATOR")) {

            const memberTarget = message.guild.members.cache.get(member.id);
            const ban_dm = new Discord.MessageEmbed()
                .setColor("#ff0000")
                .setTitle("Ban")
                .setDescription("You got banned from the " + message.guild.name + " Server by " + message.author.username)
                .setFooter(message.author.username)
                .setTimestamp()

            memberTarget.send(ban_dm).then(() => {
                if (member) {

                    memberTarget.ban({ days: 3,reason: args.slice(1).join(" ") });
                    const banned = new Discord.MessageEmbed()
                        .setColor("#ff0000")
                        .setTitle("Ban")
                        .setDescription("User has been banned from the server successfully.")
                        .setFooter(message.author.username)
                        .setTimestamp()
                    message.channel.send(banned);
                } else {
                    const notBanned = new Discord.MessageEmbed()
                        .setColor("#ff0000")
                        .setTitle("Ban")
                        .setDescription("There was an error: You need to mention the user,which you want to ban! \n If you did that,you probably do not have the permission to ban users.")
                        .setFooter(message.author.username)
                        .setTimestamp()
                    message.channel.send(notBanned);
                }
            });
        }
    }
}