How to remove a specific reaction from message

Question:

I’m making a simple game in discord.py/pycord and I would like my bot to be able to delete a specific reaction on click. Is there a way to do this?

some screenshot

Here is the excepted result:

another image

Here is my actual code (I’m using pycord):

import discord
from discord.ext import commands

intents = discord.Intents().all()

bot = commands.Bot(intents=intents)

@bot.event
async def on_message(message):
    if message.author == bot.user:
        return
    if message.content == 'test':
        me = await message.reply('Is this cool?')
        await me.add_reaction(" ")
        await me.add_reaction(" ")
        try:
            reaction, user = await bot.wait_for("reaction_add", check=lambda reaction, user: 
user == message.author and reaction.emoji in [" ", " "], timeout=30.0)


        except asyncio.TimeoutError:
            await message.reply("Tmieout bro")
        else:
            if reaction.emoji == " ":
                await message.reply('Like it.')
                await reaction.delete()

            else:
                await message.reply("NO like")
                await reaction.delete()

Thank you in advance

Asked By: BatteTarte

||

Answers:

You first get the reaction object, then you remove it. Docs

Code:

@bot.slash_command(name='removereaction', description="I'm helping someone with their Stack post")
async def removereaction(ctx, message: discord.Option(discord.Message)):
    print(message)
    for i in message.reactions:
        async for user in i.users():
            await i.remove(user)
    await ctx.respond(message.reactions)

How this works is it gets the message from the parameter message that has type discord.Option. The discord.Option is set so that you can use a message from a link or ID. Then, it uses this to cycle through all of the message’s reactions. Then it cycles through each user that reacted with said reaction. It must be async, because i.users() is async (See here).

The full code that may help you:

import discord
from discord.ext import commands

intents = discord.Intents().all()

bot = commands.Bot(intents=intents)

@bot.event

async def on_message(message):
    if message.author == bot.user:
        return
    if message.content == 'test':
        me = await message.reply('Is this cool?')
        await me.add_reaction(" ")
        await me.add_reaction(" ")
        try:
            reaction, user = await bot.wait_for("reaction_add", check=lambda reaction, user: 
user == message.author and reaction.emoji in [" ", " "], timeout=30.0)


        except asyncio.TimeoutError:
            await message.reply("Tmieout bro")
        else:
            if reaction.emoji == " ":
                await message.reply('Like it.')
                async for user in reaction.users():
                    await reaction.remove(user)

            else:
                await message.reply("NO like")
                async for user in reaction.users():
                    await reaction.remove(user)
                

If you want to remove JUST the bot’s reactions change the following line:

async for user in reaction.users():
                        await reaction.remove(user)

To:

    await reaction.remove(bot.user)
Answered By: Blue Robin
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.