'Removing role when reaction is removed
I am trying to remove a role when a reaction is removed. I already have the 'add reaction' roles that work perfectly. However, whenever I try to run this, it ends up printing "removed None" (none as the role) which means it doesn't remove the role. I think it has something to do with getting the roles. Here is the code:
@commands.Cog.listener()
async def on_raw_reaction_remove(self, payload):
messageID=962844879044628510
if messageID== payload.message_id:
guild= self.bot.get_guild(payload.guild_id)
member = guild.get_member (payload.user_id)
if str(payload.emoji) == "<a:black_drip:961304023401652274>":
role = get(payload.member.guild.roles, name='evnt')
elif str(payload.emoji) == "<a:Uzi_spin:962831415114879056>":
role = get(guild.roles, name='chat revive')
elif str(payload.emoji) == "<a:BlackFire:934621316298964992>":
role = get(guild.roles, name='gw')
elif str(payload.emoji) == "<a:Black_LV:961428631803027466>":
role = get(guild.roles, name='vc')
else:
role = discord.utils.get(guild.roles, name=payload.emoji)
if role is not None:
await payload.member.remove_roles(role)
print(f"Removed {role} from {member}.")
Solution 1:[1]
Raw reaction events return PartialEmoji
. Never rely on the rendering behavior. You can compare them by using their IDs, such as my_part_emoji.id == 123456
.
Also, using raw events may not be a good idea - you need access to the messages. It would be better to use the normal reaction event instead.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Eric Jin |