简体   繁体   中英

Reaction event discord.js

I'm trying to make a starboard code with my bot, and everything else is working good. But I'm trying to make it to where the bot ignores reactions from the author of the actual message.

This is my current code:

client.on('messageReactionAdd', (reaction_orig, message, user) => {
  if (message.author.id === reaction_orig.users.id) return

  manageBoard(reaction_orig)
})

It returns the following error:

if (message.author.id === reaction_orig.users.id) return;
                   ^
TypeError: Cannot read property 'id' of undefined

The problem is that messageReactionAdd takes two parameters; the message reaction as the first one, and the user that applied the emoji as the second one. When you write reaction_orig, message, user , reaction_orig is the reaction (which is correct), but message is the user who reacted as it's the second parameter. The user variable will be undefined .

Another issue is that reaction_orig.users returns a ReactionUserManager that doesn't have an id property. Luckily, the user is already passed down to your callback so you can use its ID.

Also, reaction_orig has a message property, the original message that this reaction refers to so you can get its authors' ID from it.

You can change your code to this to work:

client.on('messageReactionAdd', (reaction_orig, user) => {
  if (reaction_orig.message.author.id === user.id) {
    // the reaction is coming from the same user who posted the message
    return;
  }

  manageBoard(reaction_orig);
});

However, the code above only works on cached messages, ones posted after the bot is connected. Reacting on older messages won't fire the messageReactionAdd event. If you also want to listen to reactions on old messages you need to enable partial structures for MESSAGE , CHANNEL and REACTION when instantiating your client, like this:

const client = new Discord.Client({
  partials: ['MESSAGE', 'CHANNEL', 'REACTION'],
});

You can check if the message is cached by eg checking if its author property is not null . If it's null , you can fetch the message . Now, you have both the message author and the user who reacted, so you can compare their IDs:

// make sure it's an async function
client.on('messageReactionAdd', async (reaction_orig, user) => {
  // fetch the message if it's not cached
  const message = !reaction_orig.message.author
    ? await reaction_orig.message.fetch()
    : reaction_orig.message;

  if (message.author.id === user.id) {
    // the reaction is coming from the same user who posted the message
    return;
  }
  
  // the reaction is coming from a different user
  manageBoard(reaction_orig);
});

Try doing this:

client.on('messageReactionAdd', (reaction, user) => {
    if (!reaction.message.author.id === user.id){
        //Do whatever you like with it
        console.log(reaction.name)
    }
});

Note: The message must be cached. For that you'll need to do this

Client.channels.cache.get("ChannelID").messages.fetch("MessageID");

I'm guessing you're using discord.js v12

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