My discord.py bot adds the role and emoji to the name when I react to the embed, but it doesn’t remove the role and emoji when I remove the reaction.
My script:
# Event when the user reacts to a message
@bot.event
async def on_reaction_add(reaction, user):
if user.bot:
return
emoji = reaction.emoji
if emoji in emojis:
index = emojis.index(emoji)
guild = reaction.message.guild
cargo = nextcord.utils.get(guild.roles, name=cargos[index])
if cargo:
await user.add_roles(cargo)
apelido_atual = user.display_name
# Verifique se o emoji já está no apelido
if emoji not in apelido_atual:
novo_apelido = f'{apelido_atual} {emoji}' # Adicione o emoji ao apelido atual
try:
await user.edit(nick=novo_apelido)
except nextcord.errors.Forbidden:
print(f'Não foi possível alterar o apelido de {user.display_name}')
# Event when the user removes the reaction from a message
@bot.event
async def on_raw_reaction_remove(payload):
emoji = payload.emoji.name
if emoji in emojis:
index = emojis.index(emoji)
guild = bot.get_guild(payload.guild_id)
cargo = nextcord.utils.get(guild.roles, name=cargos[index])
user = guild.get_member(payload.user_id)
if cargo and user:
await user.remove_roles(cargo)
apelido_atual = user.display_name
novo_apelido = apelido_atual.replace(f' {emoji}', '')
try:
await user.edit(nick=novo_apelido)
except nextcord.errors.Forbidden:
print(f'Não foi possível alterar o apelido de {user.display_name}')
bot.run(TOKEN)
I tried to make the bot remove the role when I remove the reaction from the message, as expected it adds the role when I react, but it is not removing the role when I remove my reaction.
You need the members intent to get reaction removed events