I am creating a bot for my server but someone is removing the reaction on my server, so I want to detect that who is removing the reaction. But I have no idea how to do that or even if it is possible or not. Your help is highly appreciated
I am creating a bot for my server but someone is removing the reaction on my server, so I want to detect that who is removing the reaction. But I have no idea how to do that or even if it is possible or not. Your help is highly appreciated
Firstly, this answer assumes that reactions are not being removed through the "Manage Messages" permission, which would allow members with this permission to remove reactions made by others. If that is the case, you will need to audit the permissions settings for channels, roles, and users to ensure distrusted users do not have this permission. If reactions are deleted by a member who does not own the reaction via either direct removal or the "Remove All Reactions" menu option, discord.py does not have a way to see who removed it. Although the on_raw_reaction_remove event will be triggered, your bot won't know by whom. This is likely the case for the asker, but I will answer the question that brought me here for those of us who do have a use case for identifying the member who removed their emoji.
Note that this was reported as a discord.py issue no way to determine who removed the reaction:
Discord does not expose who removed a reaction in the MESSAGE_REACTION_REMOVE event, so discord.py cannot provide this information. It does provide the ID of the user whose reaction was removed, which discord.py exposes.
Fortunately, you don't need to directly know who removed the reaction. Presuming you do not have distrusted moderators removing the reaction, the member can be inferred by user_id because, by default, members can only remove their own reactions. If a reaction was removed, it was by the person who added it.
From the description of [RawReactionActionEvent.user_id attribute](https://discordpy.readthedocs.io/en/stable/api.html#discord.RawReactionActionEvent user_id):
The user ID who added the reaction or whose reaction was removed.
You can get this from the payload with something like:
@commands.Cog.listener()
async def on_raw_reaction_remove(self, payload):
# This will get the object for the member who owns the reaction that was removed.
member = discord.utils.get(message.guild.members, id=payload.user_id)
message = await channel.fetch_message(payload.message_id)
await message.reply(f"A reaction was removed by {member.mention}!\n"
f"Or perhaps a moderator has gone rogue ...")
# Yes. I do use emoji in discord.py code.
You can check if someone has removed a reaction using on_raw_reaction_remove which gives you a payload, that you can then accsess the user_id and an optional member if found. For full details check the RawReactionActionEvent documentation.