Javascript Discord JS错误:未处理PromisejectionWarning:DiscordAPIError:无法向此用户发送消息

Javascript Discord JS错误:未处理PromisejectionWarning:DiscordAPIError:无法向此用户发送消息,javascript,node.js,discord.js,Javascript,Node.js,Discord.js,试图建立一个系统,当你说一个被列入黑名单的单词时,它会将其删除,然后让人告诉他删除了哪个频道,原因以及他所说的信息。但我的代码一直告诉我: 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[w

试图建立一个系统,当你说一个被列入黑名单的单词时,它会将其删除,然后让人告诉他删除了哪个频道,原因以及他所说的信息。但我的代码一直告诉我:

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}*`)
  };
bot.on('message',异步消息=>{
var sender=message.author
var channel=message.channel.id
如果(sender.id=='BOT id'){
返回;
}
if(message.content.includes('discord.gg/')){
message.delete();
message.author.send(`**您在中的邮件已被删除。**
\原因:***促销*
\n**\u您的邮件:{***${Message.content}*`)
};

此用户可能不允许来自此服务器的DM。您基本上可以拒绝其他用户从特定服务器对您进行DM。为了避免出现错误,您需要使用
try-catch
块并相应地处理它。

用户不能允许从此服务器向他发送DM消息。最好不要使用
try-catch
constrution.Method在消息接收成功时返回一条消息,在某些思考出错时拒绝err。您可以使用单个块
。catch(err=>)

if(message.content.includes('discord.gg/')){
message.delete();
message.author.send(`**您在中的邮件已被删除。**
\原因:***促销*
\n**\u您的消息:{Message.content}*`.catch(err=>console.log('User't allow to send DM Message from him'))
}

由于他使用异步函数,我有点希望他使用
await
,其中try-catch完全有意义。await用于什么?他不尝试将回调函数的结果常量化为variabale.Hm?您不必仅为此目的使用await。您可以使用
await message.author.send()
。通常情况下,混合使用基于承诺的代码和等待是不好的做法,因此根据代码的其余部分,使用等待是完全可行的。那么它是如何解决他的问题的呢?它只是停止他的代码执行以等待承诺拒绝或解决这只是一种替代方法,因为您说过最好不要使用try-catch,尽管这取决于他编码,同样可行。
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'))
  }