How do I give out a role when I click on a reaction? It doesnt work for me?

2024/10/10 4:27:06

I'm trying to make sure that when you enter a command, a message is sent and a reaction is set, and those who click on the reaction get a role, but when you enter a command, nothing happens. Here is the code itself:

 @client.command(pass_context=True)@commands.has_permissions(administrator=True)async def mp(self, ctx, payload):emb = discord.Embed(title=f'Праздник вазилина', description='Нажми на реакцию что бы получить роль', colour=discord.Color.purple())message = await ctx.send(embed=emb) # Возвращаем сообщение после отправкиmessage.add_reaction('✅')member = utils.get(message.guild.members, id=payload.user_id) emoji = str(payload.emoji) roles = utils.get(message.guild.roles, id=config.ROLE[emoji],)await member.add_roles(roles)print('[SUCCESS] Пользователь {0.display_name} получил новую роль {1.name}'.format(member, role))await member.send('test')
Answer

You can indeed make some kind of "event" in a command. However your code contains some errors that we have to take care of first.

First: If you want to add a reaction to a message you always have to prefix it with await, otherwise it won't work and you'll get the following error:

RuntimeWarning: Enable tracemalloc to get the object allocation traceback

Second: member and emoji don't make any sense here, because you didn't include an event. You also cannot use payload.

Have a look at the following code:

@client.command(pass_context=True)
@commands.has_permissions(administrator=True)
async def mp(ctx):emb = discord.Embed(title=f'Праздник вазилина', description='Нажми на реакцию что бы получить роль',colour=discord.Color.purple())message = await ctx.send(embed=emb)  # Send embedawait message.add_reaction('✅') # Add reactionroles = discord.utils.get(message.guild.roles, id=RoleID) # Replace it with the role IDcheck = lambda reaction, user: client.user != user # Excludes the bot reactionwhile True:reaction, user = await client.wait_for('reaction_add', check=check) # Wait for reactionif str(reaction.emoji) == "✅":await user.add_roles(roles) # Add roleprint('[SUCCESS] Пользователь {0.display_name} получил новую роль {1.name}'.format(user, roles)) # Printawait user.send('TEST') # Send message to member

To allow multiple reactions we built in a while True "loop".

https://en.xdnf.cn/q/118496.html

Related Q&A

Python Pandas DataFrame Rounding of big fraction values [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Splitting Data in Python?

it does not work. I want to split data as in code in lines attribute. class movie_analyzer:def __init__(self,s):for c in punctuation:import removiefile = open(s, encoding = "latin-1")movielis…

Use local function variable inside loop [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 7 years ago.Improve…

Groupby Pandas , calculate multiple columns based on date difference

I have a pandas dataframe shown below: CID RefID Date Group MID 100 1 1/01/2021 A 100 2 3/01/2021 A 100 3 4/01/20…

EC2 .bashrc and .bash_profile re-setting

Reason Im asking: pycurl requires both libcurl-devel and openssl-devel. To install these, I have these two lines the my .bash_profile: sudo yum install libcurl-devel sudo yum install -y openssl-develPr…

How to load mutiple PPM files present in a folder as single Numpy ndarray?

The following Python code creates list of numpy array. I want to load by data sets as a numpy array that has dimension K x M x N x 3 , where K is the index of the image and M x N x 3 is the dimension …

Python: Understanding Threading Module

While learning Pythons threading module Ive run a simple test. Interesting that the threads are running sequentially and not parallel. Is it possible to modify this test code so a program executes the …

How the program has control with the break statement [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Python: how to plot data coming from different excel sheets in the same chart

I need to create an interactive chart on python taking data from different sheets of an Excel file. I tried to create a for loop to take data from all the sheets automatically, but I manage to graph on…

Dynamic html table with django [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 6…