Discord.py modify a message based on reaction

Question:

I have an issue and I can’t understand how to do it.

Basically I have a bot that send a message if he detect a word.
Now I wanted to add the possibility to modify that message based on who add a reaction (the bot add 3 reactions at each message he send).

So far, it’s working, my bot respond and add the 3 reactions, but now I don’t understant how to make my bot edit his message (I don’t even manage to make the bot say who click on a reaction) and also remove the reaction.

I’ve read the docs, read a lot of forum and I can’t find a way to make it work…
Please help =)

blue = '<:blue:1033370324135333898>'
red = '<:red:1033370379663704148>'
yellow = '<:yellow:1033370423078952970>'

@client.event
async def on_message(message):
    if 'ping' in message.content.lower():
        if message.channel.id == 1032275628470308895:
            channel = client.get_channel(1032275628470308895)
            react = await channel.send('pong!')
            await react.add_reaction(blue)
            await react.add_reaction(red)
            await react.add_reaction(yellow)
            
@client.event
async def on_raw_reaction_add(payload):

# and then I don't know what to do, everything I've tried doesn't work
# I wanted to modify the message to (for example "{user} said blue pong!" and remove the reaction the user just use, and just keep the 3 from the bot)
# And then if another user use the yellow reaction, the bot modify his message to for example "{user] said yellow pong!"
# That could be great if the message was edited but keeping what was said before (in that case "pong! n{user} said blue pong! n{user} said yellow pong!")
# And last thing, if possible I want the bot to remove all reactions including his after 30mins
Asked By: Sha_do

||

Answers:

https://discordpy.readthedocs.io/en/stable/api.html?highlight=on_raw_reaction_add#discord.RawReactionActionEvent

The event on_raw_reaction_add returns a payload which is a RawReactionActionEvent class.

From this payload you can get the message_id which can be used to fetch the message (using for example discord.utils) and then you can edit the message using the class method edit

https://discordpy.readthedocs.io/en/latest/api.html#discord.Message.remove_reaction

The discord message object has a method called remove reaction that can remove reactions when you pass it an emoji and member object.

https://discordpy.readthedocs.io/en/latest/api.html#discord.Reaction.remove

You can also use the class method remove from the reaction object itself

These resources should be enough for you to code a solution to your problem 🙂

Answered By: hexxx

Alright I think I’m done!
Now my bot answer to a specific word, and add reactions to his message.
If someone react to it, my bot remove the user reaction, and edit his own message, keep his previous text and add something else at the end (in that case):

"Previous message"

"user" said it’s blue pong

And then after some times, the bot remove all the reactions

Here the code if someone want to do that someday..

blue = '<:blue:1033370324135333898>'
red = '<:red:1033370379663704148>'
yellow = '<:yellow:1033370423078952970>'

@client.event
async def on_message(message):
    if 'ping' in message.content.lower():
        if message.channel.id == 1032275628470308895:
            channel = client.get_channel(1032275628470308895)
            react = await channel.send('pong!')
            await react.add_reaction(blue)
            await react.add_reaction(red)
            await react.add_reaction(yellow)
            await asyncio.sleep(10)
            await react.clear_reactions()
            
@client.event
async def on_raw_reaction_add(payload):
    channel = client.get_channel(payload.channel_id)
    message = await channel.fetch_message(payload.message_id)
    user = client.get_user(payload.user_id)

if str(payload.emoji) == blue:
    if str(payload.user_id) == "bot_ID":  # bot_ID here, don't want the bot trigered by his own reaction
        return
    await message.remove_reaction(payload.emoji, user)
    await message.edit(content=f"{message.content} n{user.name} said it's a blue pong !")

Thanks @hexxx for your help, it’s was my first time programming something ^^

Answered By: Sha_do
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.