0
votes

I'm currently working on a system which allows users to create Role Reaction messages. I'm using Enmap (js <=> sqlite database) to store the pairs given by users emojiIdOrName: roleId.

So, as you can read in the title, I'm having issues with Reactions. When a member reacts with whatever emoji of a message stored in my DB, my code first makes sure that all the emoji-role pairs stored for this message are correct (checks if the stored roles are still available, if all the reactions you see under the message are in the DB and vice versa). If something's wrong, the pair is deleted from the DB and the reaction is deleted from the message. So according to what I said above (if it's clear enough), when you remove an emoji from the message (on discord side, e.g. "Remove All Reactions" button), the next time someone reacts should update the database and remove the correspondign entry in the DB before even getting a role. But it actually doesn't.

What I tried

First of all I obviously tried to debug my code by putting some console.logs everywhere. For what I understood, when a reaction was cached once trying to fetch the message again does not update the cache and the reaction still appears to be on the message (when it's not ^^). So I tried different things I could see around Stackoverflow, like adding the client.on('raw', ....) bit. It didn't change anything. Then I tried using partials (I thought it might help smh) for Messages and Reactions. Nothing different. Even tried iterating other the reactions like so

reaction.message.reactions.cache.forEach(async r => {
    await r.fetch();
});

Here is a concrete example of what I said. I just removed a reaction: enter image description here, only one remaining but here is what I get when logging the reaction emoji: enter image description here. I'm using the following code

case 'dev':
  message.channel.messages.fetch(arguments[1]).then(m => {
    console.log(m.reactions.cache.map(r => {r.fetch();return r.emoji.name}))
  })
  break;

What I did not try (and I don't want to use it)

I don't want to use other events like messageReactionRemove, messageReactionRemoveAll, messageReactionRemoveEmoji etc. as there are too much ways of removing a reaction (it would be a pain to handle all of these).

Making it shorter

To make it shorter, I'd like to be able to fetch a message's real reactions and not get cached ones with the messageReactionAdd event.



Edit

Ok soo I've been busy trying a few things lately and I found something really interesting. In fact it appears that everything I said above is true but only for reactions added by everyone who isn't the client. I'm now trying to correctly fetch my bot's reactions.

1

1 Answers

0
votes

Okay so I finally got an answer. I can do what I want to do by clearing both the message's and reactions' cache, and then recache the message by fetching it. In JS, it is the following:

client.on('messageReactionAdd', async (reaction, user) => {
    await reaction.message.channel.messages.cache.delete(reaction.message.id);
    await reaction.message.reactions.cache.clear();
    let message = await reaction.message.fetch();
    ....

Message is exaclty the same as reaction.message, but the reactions are the new ones (it doesn't include the deleted ones).