简体   繁体   中英

How do I DM a user before they are banned in discord.js v12?

Here's my code, I want to DM the user BEFORE they get banned, how would I do that?

    run: async(client, message, args) => {
        if(!message.member.hasPermission('BAN_MEMBERS')) {
            message.channel.send("You don't have permission to use that command.");
        }
        else {
            try {
                let bannedMember = await message.guild.members.ban(args);
        if(bannedMember)
                    message.channel.send(`<:Checkmark:721061661582295222> \`${bannedMember.tag}\` **has been successfully banned.**`);
            }
            catch(err) {
                console.log(err);
            }
        }
    },
    aliases: [],
    description: 'Bans a guild member by their ID'
}

Im using discord.jsv12

You can use User#send to send the message to the GuildMember you are about to ban.

Note that it returns a Promise , which can be rejected (fail) if the bot cannot DM the User .

Since it is a Promise , you can wait for it to finish before banning the user.


if (!message.member.hasPermission("BAN_MEMBERS")) return message.channel.send("You don't have permission to use that command.");

const Member = await message.guild.members.fetch("id");
if (!Member) return message.channel.send("Couldn't find the member.");

await Member.send("You have been banned.").catch(error => console.error(error));
Member.ban("Reason").then(() => {
    message.channel.send(`${Member.user.tag} has been banned.`);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM