【问题标题】:How do I fix the 'Cannot send messages to this user' DiscordAPIError?如何修复“无法向该用户发送消息”DiscordAPIError?
【发布时间】:2021-10-15 21:22:57
【问题描述】:

我在发送关于 guildMemberAdd 事件的私人消息时遇到问题。

代码如下:

bot.on('guildMemberAdd', member => {
    member.send('Welcome to the server.');
});

这会导致这个错误:

(node:11760) UnhandledPromiseRejectionWarning: DiscordAPIError: Cannot send messages to this user
    at RequestHandler.execute (C:\Users\AckeeXZ\Desktop\BOT\californianetwork\node_modules\discord.js\src\rest\RequestHandler.js:154:13)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at async RequestHandler.push (C:\Users\AckeeXZ\Desktop\BOT\californianetwork\node_modules\discord.js\src\rest\RequestHandler.js:39:14)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:11760) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:11760) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

【问题讨论】:

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


    【解决方案1】:

    这个错误很可能是因为该成员的 DM 已关闭,这意味着机器人无法向他们发送任何消息。

    用户可以在“隐私和安全”设置中禁止来自服务器成员的直接消息。

    没有其他方法可以绕过此问题,但您可以使用.catch()try - catch 块来处理错误。

    bot.on('guildMemberAdd', member => {
        member.send('Welcome to the server.').catch(e => console.log(e));
    });
    
    // or 
    
    bot.on('guildMemberAdd', async member => {
        try { 
            await member.send('Welcome to the server.'); 
        } catch(e) { 
            console.log(e); 
        }
    });
    

    【讨论】:

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