Discord JS错误:UnhandledPromiseRejection警告:DiscordAPI错误:无法向该用户发送消息

时间:2020-02-25 02:09:53

标签: javascript node.js discord.js

试图建立一个系统,当您说出一个列入黑名单的单词时,它将删除该单词,然后让DM告诉该人该单词被删除的通道,原因以及他说的信息。但是我的代码不断告诉我:

2020-02-25T02:05:30.557281+00:00 app[worker.1]: (node:4) UnhandledPromiseRejectionWarning: DiscordAPIError: Cannot send messages to this user
2020-02-25T02:05:30.557293+00:00 app[worker.1]:     at /app/node_modules/discord.js/src/client/rest/RequestHandlers/Sequential.js:85:15
2020-02-25T02:05:30.557294+00:00 app[worker.1]:     at /app/node_modules/snekfetch/src/index.js:215:21
2020-02-25T02:05:30.557295+00:00 app[worker.1]:     at processTicksAndRejections (internal/process/task_queues.js:97:5)
2020-02-25T02:05:30.557358+00:00 app[worker.1]: (node:4) 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: 6)

这是我的代码:

bot.on('message', async message => {

  var sender = message.author
  var channel = message.channel.id

  if(sender.id === 'BOT ID') {
    return;
    }


if(message.content.includes('discord.gg/')) {
      message.delete();
      message.author.send(`**Your message in <#${channel}> had been deleted.**  
      \n**__Reason:__** *Promotion*
      \n**__Your Message:__** *${message.content}*`)
  };

2 个答案:

答案 0 :(得分:0)

该用户可能不允许来自此服务器的DM。您基本上可以拒绝其他用户从特定服务器向您发送DM。为避免显示错误,您需要使用try catch块并进行相应的处理。

答案 1 :(得分:0)

用户不允许从该服务器发送DM消息。最好不要使用try - catch构造。如果消息接收成功,方法DM Channel send返回消息promise,如果某些地方出错了,则拒绝err。您可以使用单个块.catch(err => )

处理它
if(message.content.includes('discord.gg/')) {
      message.delete();
      message.author.send(`**Your message in <#${channel}> had been deleted.**  
      \n**__Reason:__** *Promotion*
      \n**__Your Message:__** *${message.content}*`).catch(err => console.log('User don`t allow to send DM message from him'))
  }