E
E
Egor Knyazev2021-05-14 19:40:20
Python
Egor Knyazev, 2021-05-14 19:40:20

How to issue a role when clicking on the reaction? Does not work?

Good evening, I'm trying to make it so that when a command is entered, a message is sent and a reaction is set, and those who click on the reaction get a role, but nothing happens when the command is entered.
Here is the code itself)
Thanks in advance for your help

@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 the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Nevzorov, 2021-05-15
@fixator10

Your command takes the string "payload" as an argument. Since you are trying to get a reaction in the team, you need to wait for it there.
Or catch all reactions on the message (messages) created by the team.

@client.command()  # Прекратите. Насиловать. Труп.
@commands.has_permsissions(administrator=True)
async def mp(ctx):  # self если мы находимся в модуле или подклассе Bot'а
    embed = discord.Embed(title="something here")
    msg = await ctx.send(embed=embed)
    client.react_role_msg = msg  # Что лучше - "monkey patch"'ить объект бота или использовать global - решайте сами. 
    # В идеале - лучше создать модуль (cog/extension) или создать подкласс commands.Bot и использовать его, с объявленной переменной на уровне класса

@client.listen()
async def on_raw_reaction_add(payload):
    if not getattr(client, "react_role_msg", None):  # react_role_msg еще не объявлен у объекта бота
        return
    if payload.message_id != client.react_role_msg.id:  # реакция проставлена на другом сообщении
        return
    if payload.emoji != "\N{WHITE HEAVY CHECK MARK}":  # Реакция не "✅"
        return
    if not payload.member:  # На тот случай если реакция пройдя все проверки выше окажется в личных сообщениях
        return
    await payload.member.add_roles(client.get_guild(398353602260405500).get_role(707803004268002512))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question