async def on_raw_reaction_add(payload): reaction = str(payload.emoji) msg_id = payload.message_id ch_id = payload.channel_id user_id = payload.user_id guild_id = payload.guild_id exists = rldb.exists(msg_id) if exists: # Checks that the message that was reacted to is a reaction-role message managed by the bot reactions = rldb.get_reactions(msg_id) ch = bot.get_channel(ch_id) msg = await ch.fetch_message(msg_id) user = bot.get_user(user_id) if reaction not in reactions: # Removes reactions added to the reaction-role message that are not connected to any role await msg.remove_reaction(reaction, user) else: # Gives role if it has permissions, else 403 error is raised role_id = reactions[reaction] server = bot.get_guild(guild_id) member = server.get_member(user_id) role = discord.utils.get(server.roles, id=role_id) if user_id != bot.user.id: try: await member.add_roles(role) except discord.Forbidden: if system_channel: channel = bot.get_channel(system_channel) await channel.send( "Someone tried to add a role to themselves but I do not have permissions to add it. " "Ensure that I have a role that is hierarchically higher than the role I have to assign, " "and that I have the `Manage Roles` permission." )
async def on_raw_reaction_remove(payload): reaction = str(payload.emoji) msg_id = payload.message_id user_id = payload.user_id guild_id = payload.guild_id exists = rldb.exists(msg_id) if exists: # Checks that the message that was unreacted to is a reaction-role message managed by the bot reactions = rldb.get_reactions(msg_id) if reaction in reactions: role_id = reactions[reaction] # Removes role if it has permissions, else 403 error is raised server = bot.get_guild(guild_id) member = server.get_member(user_id) role = discord.utils.get(server.roles, id=role_id) try: await member.remove_roles(role) except discord.Forbidden: if system_channel: channel = bot.get_channel(system_channel) await channel.send( "Someone tried to remove a role from themselves but I do not have permissions to remove it. " "Ensure that I have a role that is hierarchically higher than the role I have to remove, " "and that I have the `Manage Roles` permission." )
async def on_raw_reaction_remove(payload): reaction = str(payload.emoji) msg_id = payload.message_id user_id = payload.user_id guild_id = payload.guild_id exists = rldb.exists(msg_id) if isinstance(exists, Exception): await system_notification( f"Database error after a user removed a reaction:\n```\n{exists}\n```" ) return elif exists: # Checks that the message that was unreacted to is a reaction-role message managed by the bot reactions = rldb.get_reactions(msg_id) if isinstance(reactions, Exception): await system_notification( f"Database error when getting reactions:\n```\n{reactions}\n```" ) return if reaction in reactions: role_id = reactions[reaction] # Removes role if it has permissions, else 403 error is raised server = bot.get_guild(guild_id) member = server.get_member(user_id) role = discord.utils.get(server.roles, id=role_id) try: await member.remove_roles(role) except discord.Forbidden: await system_notification( "Someone tried to remove a role from themselves but I do not have permissions to remove it. " "Ensure that I have a role that is hierarchically higher than the role I have to remove, " "and that I have the `Manage Roles` permission." )