如何在discord.js v12中禁止用户之前给用户DM?

时间:2020-09-28 02:17:50

标签: javascript discord.js

这是我的代码,我想在被禁止之前向该用户发送DM,我该怎么办?

    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'
}

我正在使用discord.jsv12

1 个答案:

答案 0 :(得分:2)

您可以使用expanding the Subscription将邮件发送到您要禁止的User#send

请注意,它会返回GuildMember,如果漫游器无法DM Promise,则可以拒绝(失败)。

由于它是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.`);
});