【问题标题】:"UnhandledPromiseRejectionWarning: DiscordAPIError: Cannot send messages to this user"“UnhandledPromiseRejectionWarning:DiscordAPIError:无法向该用户发送消息”
【发布时间】:2020-12-20 08:09:38
【问题描述】:

我正在寻找一种方法来检查机器人是否可以向某人发送消息。

例如:

我:/dm @someone

机器人:我不能私信这个用户!

而不是这个错误:

UnhandledPromiseRejectionWarning: DiscordAPIError: 无法向该用户发送消息

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案1】:

    您应该使用try ... catch 语句来捕获异常:

    参考:MDN

    try {
        // Check here if you can DM
    } catch (error) {
        // Send message to user ("Can't DM")
    }
    

    另外,如果你使用 Promise(例如 .send() 函数),你可以捕捉到这样的错误:

    <client>.send("Hey!")
        .catch(() => {
            // Send message to user ("Can't DM")
        });
    

    【讨论】:

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