1

I am writing a discord bot. I have the following code:

@client.event
async def on_reaction_add(reaction, user):
    print("on_reaction_add called")

@client.event
async def on_reaction_remove(reaction, user):
    print("on_reaction_remove called")

# same problem if I remove this part
@client.event
async def on_raw_reaction_remove(payload):
    print("on_raw_reaction_remove called")

However, if I start the bot and react to a event, then remove the reaction again, I see:

on_reaction_add called
on_raw_reaction_remove called

As you can see, on_reaction_remove is never called. I must have the correct permissions since on_reaction_add() works properly. This also means the message must be in the message cache, since otherwise on_reaction_add would not work either. on_raw_reaction_remove is still called, so it must have properly detected that I removed a reaction.

Note that even if I don't include the on_raw_reaction_remove event I get the same problem.

Is there a problem with my code, is this a problem with discord.py or is there something I am not understanding?

mousetail
  • 7,009
  • 4
  • 25
  • 45
  • Why are you using `on_reaction_remove()` and `on_reaction_add()` instead of `on_raw_reaction_remove()` and `on_raw_reaction_add()` in the first place? – MrMetacom Nov 05 '20 at 03:51
  • @MrMetacom It is very convenient to be able to access the message, and I only care about recent messages anyway so it's not a problem if reactions get missed before the bot starts up – mousetail Nov 05 '20 at 08:14

2 Answers2

0

The issue is that, unlike on_reaction_add, on_reaction_remove requires that both the message and the user can be found in the cache.

Luckily, ensuring every user is in the cache is quite simple. You will need the Intents.members intent enabled on your bot so it can pre-fetch the list of channel members on startup, and on_reaction_remove can be called properly.

See the recently added comment in the documentation

mousetail
  • 7,009
  • 4
  • 25
  • 45
-2

If you read the documentation of on_raw_reaction_remove you will notice something, it says:

Called when a message has a reaction added. Unlike on_reaction_add(), this is called regardless of the state of the internal message cache.

That means, raw events are called even message is not in bot's cache, whereas normal reactions are called when message is in the bot's cache.

That is the problem, the message you are removing reaction from is not in bot's cache so it does not trigger normal event, but it does trigger raw event.

Just for fun
  • 4,102
  • 1
  • 5
  • 11