0
votes

Trying to make a system where, when you say a blacklisted word, it deletes it and goes DMs the person to tell him which channel it got deleted, the reason why and the message that he said. But my code keeps telling me this:

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)

And here is my code :

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

2 Answers

0
votes

This user probably doesn't allow DM's from this server. You can basically deny that other users DM you from specific servers. To avoid the error showing up, you need to use a try catch block and handle it accordingly.

0
votes

User can not allow to send him DM message from this server. Better dont use try - catch constrution. Method DM Channel send return a promise of message if message recive success and reject err if somethink went wrong. You can handle it with single block .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'))
  }