如何修复DiscordAPIError:无法向该用户发送消息?

时间:2020-06-05 16:35:23

标签: javascript node.js bots discord discord.js

我正在执行一个新命令,向作者发送一条消息,但是,当作者的DM关闭时,会给我一个错误DiscordAPIError: Cannot send messages to this user

    if (!message.author.send) {
        return message.channel.send("DMs closed.");
    } else {
        message.author.send("DMs opened")
    }

2 个答案:

答案 0 :(得分:1)

好吧,正如您所说的,作者的DM已关闭,因此它不起作用。您可以尝试这样的事情:


    //Put the messages you wanted to send the author's DM's
    //Add this below it
    .catch(error => {
          console.error(
            `Could not send help DM to ${message.author.tag}.\n`,
            error
          );
          message.reply("it seems like I can't DM you! Do you have DMs disabled?");
     });

     //If the user has the DM's turned off, then it'll send this in the channel and `console.log` the error

答案 1 :(得分:0)

send函数将始终存在,即使用户dm已关闭或打开,要检查dm是否已关闭,也可以使用.catch来捕获DiscordAPIError错误。 br /> 请参阅:Promise.catch

尝试一下:

message.author.send("DMs opened").catch(error => message.channel.send("DMs closed"))