【问题标题】:How can I fix `DiscordAPIError: Cannot send messages to this user`?如何修复“DiscordAPIError:无法向该用户发送消息”?
【发布时间】:2020-09-24 22:40:54
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案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
    

    【讨论】:

      【解决方案2】:

      send 函数将始终存在,即使用户 dm 已关闭或打开,要检查 dm 是否已关闭,您可以使用 .catch 捕获 DiscordAPIError 错误。
      见:Promise.catch

      试试这个:

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

      【讨论】:

        猜你喜欢
        • 2021-10-15
        • 2021-11-06
        • 2020-06-04
        • 2021-02-07
        • 2020-04-07
        • 2020-02-17
        • 2020-10-01
        • 2018-12-26
        相关资源
        最近更新 更多