Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

on_reaction_add not being run

I'm new to discord.py and trying to make a translator bot. When the user reacts with a certain flag, the bot translates it, but the event is never getting called hence I have no code to translate any messages yet. I know it's not getting called because the program isn't printing an 'x' to the console.

@client.event
async def on_reaction_add(reaction, user):
    channel = reaction.message.channel
    print('x')
    await client.send_message(channel, '{} has added {} to the the message {}'.format(user.name, reaction.emoji, reaction.message.content))

    await client.process_commands(reaction.message)
like image 658
Crawley Avatar asked Dec 09 '25 00:12

Crawley


2 Answers

Probably a bit late to this thread but, the answer above is a valid answer. But you can also use on_raw_reaction_add which gets called even if the messages aren't in the Bot's cache.

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

Documentation Link

Example:

@commands.Cog.listener()
async def on_raw_reaction_add(self, payload):
    channel = await self.bot.fetch_channel(payload.channel_id)
    message = await channel.fetch_message(payload.message_id)
    user = await self.bot.fetch_user(payload.user_id)
    emoji = payload.emoji

    await channel.send("Hello")

There isn't much valid reason for why the event isn't registered/called.

One of which is stated in the docs: http://discordpy.readthedocs.io/en/async/api.html#discord.on_reaction_add. Try adding a reaction immediately to a message that is sent after the bot is online. Since messages sent before the bot is online will not be recognized by the bot (not in Client.messages).

if the message is not found in the Client.messages cache, then this event will not be called.

Another possible reason is that this function was never defined before the client loop commenced. Verify your indentation. And/Or try placing the function directly under client = Bot(...), to check if this is the problem.

If neither of the aforementioned solves your problem, please post a minimal, complete, verifiable example (a short runnable code from top to bottom that indicates your problem).

like image 43
Taku Avatar answered Dec 10 '25 13:12

Taku



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!