简体   繁体   English

当我点击一个反应时,我如何分配一个角色? 它对我不起作用?

[英]How do I give out a role when I click on a reaction? It doesn't work for me?

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:第一:如果你想给消息添加一个反应,你总是必须在它前面加上await ,否则它将不起作用,你会得到以下错误:

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 .第二: memberemoji在这里没有任何意义,因为您没有包含event You also cannot use payload .您也不能使用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 embed
    await message.add_reaction('✅') # Add reaction

    roles = discord.utils.get(message.guild.roles, id=RoleID) # Replace it with the role ID

    check = lambda reaction, user: client.user != user # Excludes the bot reaction

    while True:
        reaction, user = await client.wait_for('reaction_add', check=check) # Wait for reaction
        if str(reaction.emoji) == "✅":
            await user.add_roles(roles) # Add role
            print('[SUCCESS] Пользователь {0.display_name} получил новую роль {1.name}'.format(user, roles)) # Print

            await user.send('TEST') # Send message to member

To allow multiple reactions we built in a while True "loop".为了允许多重反应,我们内置了一个while True “循环”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM