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

How to dm user before executing a ban in discord.js?

有人可以告诉我如何在用户被封禁之前向他发送私信吗?一切正常,但 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);
                }
            });
        }
    }
}