Discord.js尝试在用户被禁止时向其发送DM

时间:2020-06-24 19:16:09

标签: discord.js

您好,因此,当我的discord bot被禁止时,我正尝试使用该用户来管理该用户,但每次禁止用户时,此消息都会出现在日志中:

2020-06-24T18:55:42.825881+00:00 app[worker.1]: Unhandled promise rejection: DiscordAPIError: Cannot send messages to this user

2020-06-24T18:55:42.825916+00:00 app[worker.1]:     at RequestHandler.execute (/app/node_modules/discord.js/src/rest/RequestHandler.js:170:25)

2020-06-24T18:55:42.825918+00:00 app[worker.1]:     at processTicksAndRejections (internal/process/task_queues.js:97:5) {

2020-06-24T18:55:42.825918+00:00 app[worker.1]:   method: 'post',

2020-06-24T18:55:42.825919+00:00 app[worker.1]:   path: '/channels/725422295631396876/messages',

2020-06-24T18:55:42.825920+00:00 app[worker.1]:   code: 50007,

2020-06-24T18:55:42.825921+00:00 app[worker.1]:   httpStatus: 403

2020-06-24T18:55:42.825921+00:00 app[worker.1]: }

当前,这是我的代码:

module.exports = {
    name: 'ban',
    description: 'Bans a member.',
    usage: '[mention]',
    guildOnly: true,
    execute(message, args) {
        if (message.member.hasPermission('BAN_MEMBERS')) {
            const user = message.mentions.users.first();

            if (user) {
                const member = message.guild.member(user);
                if (member) {
                    user.send("You we're banned from Jakooobs Cat House!")
                  member
                    .ban({
                      reason: 'BANNED!',
                    })
                    .then(() => {
                      message.reply(`Successfully banned ${user.tag}`);
                    })
                    .catch(err => {
                      message.reply('I was unable to ban the member');
                      console.error(err);
                    });
                } else {
                  message.reply("That user isn't in this guild!");
                }
              } else {
                message.reply("You didn't mention the user to ban!");
              }
            }
          
        else {
            message.reply('you don\'t have permission to ban members!')
        }
    },
};

我看了互联网,但没找到任何东西,所以如果有人知道该请求者,请回复谢谢。

1 个答案:

答案 0 :(得分:1)

{member.send()是一种异步方法,因此它不等待发送消息然后禁止,因此您可以使用await或.then,因为您已经在使用.then,那么您也可以使用像这样链接承诺:

if (member) {
    user.send("You we're banned from Jakooobs Cat House!")
        //incase bot can't dm user
        .catch(() => message.reply("Unable to send message to user"))
        .then(() => member.ban({ reason: "BANNED!" }))
        .then(() => message.reply(`Successfully banned ${user.tag}`))
        .catch(err => {
            message.reply('I was unable to ban the member');
            console.error(err);
        });
}