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')
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".