1
votes

I have bot which get user message content, delete user message, then send private message to message.author and then send content of user message in embed. After few messages bot sends 2 messages instead of 1 and warn appears. Any idea how to fix it?

Warn:

superadmin@vps-XXXXXX:~/path$ node test.js (node:1059) UnhandledPromiseRejectionWarning: DiscordAPIError: Unknown Message at RequestHandler.execute (path/node_modules/discord.js/src/rest/RequestHandler.js:170:25) at processTicksAndRejections (internal/process/task_queues.js:97:5) (node:1059) 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:1059) [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.

Script:

const discord = require('discord.js');
const client = new discord.Client;

client.on('message', message => {
if (message.author === client.user && message.channel.id !== (508728211837026325)) {
  return;
}
if (message.channel.id == (508728211837026325)) {
  message.delete();
  message.author.send("Success!");
  message.channel.send({
    "embed": {
      "color": 61183,
      "description": message.content + "\n\nCreated by: " + "<@" + message.author.id + ">",
      "author": {
        "icon_url": "imgururl;",
        "url": "imgururl",
        "name": "test",
      },
      timestamp: new Date()
    }
  })
}
})
client.login('token');
2

2 Answers

0
votes

The behavior you describe is probably caused by the fact that your bot is launched twice at the same time. Indeed, if your bot is launched twice:

  • you're getting the Unknown Message warning, as the message was already deleted by another instance of the bot.
  • the bot sends the message twice.

Read this post, it should fix your issue.

0
votes

The bot is being activated by himself.

if (message.author === client.user && message.channel.id !== (508728211837026325))

# should probably be
if (message.author === client.user || message.channel.id !== (508728211837026325))

On another note, the channel id is a twitter snowflake, which can be as big as a uint64, for javascript, that means you can't handle an id that is too large because the limit is 9007199254740991, so you should use the string versions.

if (message.channel.id === "508728211837026325")