Ask Your Question
4

How can I assign a role when an emoji is reacted to in discord.py?

asked 2021-08-03 11:00:00 +0000

lalupa gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-09-08 09:00:00 +0000

huitzilopochtli gravatar image

You can assign a role when an emoji is reacted to by using the on_raw_reaction_add event. Here's an example code:

@client.event
async def on_raw_reaction_add(payload):
    if payload.message_id == MESSAGE_ID:  # Replace MESSAGE_ID with the ID of the message you want to monitor
        if str(payload.emoji) == '๐Ÿ‘':  # Replace ๐Ÿ‘ with the emoji you want to monitor
            guild = client.get_guild(payload.guild_id)
            member = guild.get_member(payload.user_id)
            role = guild.get_role(ROLE_ID)  # Replace ROLE_ID with the ID of the role you want to assign
            await member.add_roles(role)

This code listens for the on_raw_reaction_add event and checks if the message ID and the emoji match the ones you are monitoring. If they do, it gets the guild, member, and role objects and assigns the role to the member.

Note: You'll need to enable gateway intents and have the members intent enabled to get the guild.get_member method to work.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-08-03 11:00:00 +0000

Seen: 10 times

Last updated: Sep 08 '21