Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Discord js Add reaction to a bot message

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
like image 779
ZaraaKai Avatar asked May 31 '17 12:05

ZaraaKai


3 Answers

Those aren't error, those are warning. As it is said, you don't check when your promise is rejected. You should use .catch() after .then() in case it get rejected.

https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Promise/catch

Try :

message.channel.send(":apple:***SONDAGE :apple:\n "+choix1+" ou "+""+choix2+"***")
            .then(function (message) {
              message.react("👍")
              message.react("👎")
              message.pin()
              message.delete()
            }).catch(function() {
              //Something
             });
like image 120
Nevosis Avatar answered Sep 28 '22 21:09

Nevosis


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:

  • TextChannel.send(message)
  • Message.react(emoji)
  • Message.pin()
  • Message.delete()
like image 36
James Whiteley Avatar answered Sep 28 '22 23:09

James Whiteley


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.

like image 21
Cecemel_PvP Avatar answered Sep 28 '22 23:09

Cecemel_PvP