Discord js Add reaction to a bot message

Viewed 64225

I've been created my own discord bot but i have this error for this code:

    message.channel.send(":apple:***SONDAGE :apple:\n "+choix1+" ou "+""+choix2+"***")
      .then(function (message) {
          message.react("")
          message.react("")
         message.pin()
          message.delete()
       });

It's send a message to the channel and add reaction, and in my console i have this error:

(node:11728) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): DiscordAPIError: Unknown Message
(node:11728) 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.
(node:11728) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): DiscordAPIError: Unknown Message

3 Answers

On top of the previous things that the other answers have mentioned, functions like message.react(...) all return Promises, so for example the message might get deleted before the react/pin promises are resolved.

If you want things to happen in order you need to have a long .then(...).then(...).then(...) chain, or alternatively you could use async/await. Here's an async/await example:

client.on("message", async (message) => {
  const sentMessage = await message.channel.send("my message");
  await sentMessage.react("");
  await sentMessage.react("");
  await sentMessage.pin();
  await sentMessage.delete();
});

If you want to do any error handling on failures, you can wrap the await stuff in a try/catch block:

client.on("message", async (message) => {
  try {
    const sentMessage = await message.channel.send("my message");
    await sentMessage.react("");
    await sentMessage.react("");
    await sentMessage.pin();
    await sentMessage.delete();
  } catch (e) {
    console.error(e);
  }
});

Further reading:

I had the same error, you do message.delete(), but you want to add reactions. When a message is deleted the bot can't add reactions. Just remove the message.delete() and no error will come.

Related