【问题标题】:how to give a role when a user reacts to a message?当用户对消息做出反应时如何赋予角色?
【发布时间】:2020-11-25 22:53:31
【问题描述】:

我正在尝试创建一个命令,当用户对消息做出反应时,该命令本质上会赋予他们一个角色。我尝试了一些代码,但是当我运行机器人并对预先发送的消息做出反应时,没有发生错误,也没有任何反应。代码如下:

@client.command()
@commands.guild_only()
async def reactionrole(ctx, emoji, *, role):
    roleEmbed = discord.Embed(title='Reaction Roles', description='React to this message with the proper emoji to give yourself the role.', color=discord.Colour.from_rgb(255, 107, 33))
    roleEmbed.add_field(name='Roles & Emojis:', value=f'{emoji} = {role}', inline=True)
    message = await ctx.send(embed=roleEmbed)
    await message.add_reaction(f'{emoji}')

@client.event
async def on_reaction_add(reaction, user):
    Channel = client.get_channel('my id')
    if reaction.message.channel.id != Channel:
        return
    if reaction.emoji == "{emoji}":
      Role = discord.utils.get(user.server.roles, name="event pings")
      await client.add_roles(user, Role)

【问题讨论】:

    标签: python-3.x discord discord.py


    【解决方案1】:

    您可能正在寻找 on_raw_reaction_addclient.wait_for,具体取决于您是希望用户对一段时间前已在频道中发送的消息还是在命令中发送的消息做出反应

    @client.command()
    async def reaction_role(ctx):
        def check(reaction, user):
            return user == ctx.author and str(reaction) == '✅'
    
        message = await ctx.send('Please react with ✅ to get the role')
        try:
            # Waiting for the reaction
            await bot.wait_for('reaction_add', check=check, timeout=60.0)
            await ctx.author.add_roles(some_role)
        except asyncio.TimeoutError:
            await message.delete()
            await ctx.send('Time is over')
    
    @client.event
    async def on_raw_reaction_add(payload):
        if payload.message_id == my_message_id: 
            guild = client.get_guild(payload.guild_id)
            member = guild.get_member(payload.member_id)
            await member.add_roles(some_role)
    

    参考:

    【讨论】:

    • 我将代码修改为带引号的消息 ID,并将 some_role 替换为带引号的角色名称。我得到的只是:“'NoneType' 对象没有属性 'add_roles'”
    • 您是否启用了intents.members
    • 另外,消息 id 必须是整数,some_role 变量必须是discord.Role 对象,而不是字符串
    猜你喜欢
    • 1970-01-01
    • 2019-10-18
    • 2019-02-12
    • 2019-09-22
    • 2021-03-23
    • 2021-08-26
    • 2021-08-04
    • 2019-05-31
    • 1970-01-01
    相关资源
    最近更新 更多