简体   繁体   中英

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

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
             });

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.

Now on version 14 some things has changed a bit

The message and interaction events are now removed. You've to use messageCreate and interactionCreate instead.

So the code would like something like this...

client.on(Events.MessageCreate, async (message) => {
  try {
    if (message.channel.id === channel) {
      await message.react('😊');
    }
  } catch (err) {
    console.error(err);
  }
});

Further reading:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM