Skip to content
Advertisement

Remove a specific user reaction from a specific message – Discord.js

I hope you can help. This has been driving me crazy. I am, new to programming and JS, but I am making a discord bot as a hobby project. Please forgive me if I don’t use the correct terminology but I think my question should actually be quite simple.

I have a bot that builds a message with an embed. The bot listens with a collector and adds players to fields in the embed depending on which reaction they react so. See screenshot for example. Players can add themselves to “Farming” “Not Farming” or “Starter”

Screenshot of bot’s post

When I post the initial embed, I am clearing all the pinned messages in the channel and pining this message, so the post with my status embed will always be the only pinned post in the channel.

What I would like to do is type a command like “!placed @user” and the user which was @mentioned should have thier reaction removed from the origional post. I have no problem getting the message ID and user ID, but I cannot seem to combine the two to remove thier reaction. Here is an extract from my code:

message.channel.messages.fetchPinned().then(messages => {
   console.log(`Received ${messages.size} messages`);
   var testuserid = message.mentions.users.first().id;
   messages.forEach(message => {
      message.reactions.resolve("👍").users.remove(testuserid);
   })
})

The problem is that last line message.reactions.resolve. I have tried every combination of using the emoji character or code, hardcoding the user ID, etc etc

I always seem to end up with an error similar to:

UnhandledPromiseRejectionWarning: TypeError: Cannot read property ‘users’ of null

message.reactions.removeAll() does work, but removes all reactions including the bot’s.

I got this code from here but have tried many other combinations of code including this one which I cant seem to get to work at all (something to do with not being in an async function).

Please tell me I am missing something simple!

Advertisement

Answer

I don’t know if you’ve taken a look at the docs for the message.reactions.resolve() method, but the way you’re trying to use it isn’t quite the right way to use it (and isn’t how it is intended to be used). message.reactions.resolve() only takes in two different types of parameters: either a MessageReaction object or a Snowflake (the specific message reaction’s ID — note that this does not refer to the ID or value of the emoji, but of the specific reaction itself).

So basically, the .resolve() method is supposed to convert an ID into an object. So if you have a message with ID 811386028425609247 and you do messages.resolve(811386028425609247), it will return the specific message whose ID that is. (Of course, that’s an example with messages and not message reactions). In your case, that’s not what you want to do either. .resolve() would be used to get a single, specific reaction using the ID of that reaction, which you don’t have.

And note that it is the ID of the reaction itself that I am referring to, and not the ID of the emoji being reacted. If I were to react with a 👍 on two different messages, the emoji ID would be the same for both (because they are the same emoji), but the reaction ID would be different (because they are two separate reactions).

What you really want to do is find a specific emote that has been reacted on a message, and remove the user that reacted that. So what you’re actually looking for is something like:

message.reactions.cache.find(reaction => reaction.emoji.name == "👍").users.remove(testuserid);

What this new line of code does is it looks through the reactions on the message, finds the reaction with the 👍 emoji, and then removes the user from that reaction. And just for fun, I’ll add what your old line of code was doing: it was looking for a specific reaction with ID “👍”, which of course doesn’t exist, and attempting to remove the user from that nonexistent reaction (hence the error you were getting: Cannot read property 'users' of null, because the reaction itself was nonexistent and therefore null).

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement