Discord Python 反应角色

时间:2021-03-29 00:16:10

标签: python discord

我希望我的机器人将角色“用户”添加到一个用心做出反应的用户。但它不起作用。
我的代码:

@client.event
async def on_reaction_add(reaction, user):
    if reaction.emoji == "❤️":
        user = discord.utils.get(user.server.roles, name="User")
        await user.add_roles(user)

我希望你能帮助我:)

1 个答案:

答案 0 :(得分:0)

我会在这里使用一个不同的事件,称为 on_raw_reaction_add。您可以在 here 中找到它的文档。

对于这个新事件,您将不得不稍微重写代码并添加/删除一些内容。

您的新代码:

intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix="YourPrefix", intents=intents) #Import Intents

@client.event
async def on_raw_reaction_add(payload):
    guild = client.get_guild(payload.guild_id) # Get guild
    member = get(guild.members, id=payload.user_id) # Get the member out of the guild
    # The channel ID should be an integer:
    if payload.channel_id == 812510632360149066: # Only channel where it will work
        if str(payload.emoji) == "❌": # Your emoji
            role = get(payload.member.guild.roles, id=YourRoleID) # Role ID
        else:
            role = get(guild.roles, name=payload.emoji)
        if role is not None: # If role exists
            await payload.member.add_roles(role)
            print(f"Added {role}")

我们做了什么?

  • 使用payload
  • 使用角色 ID 而不是名称,因此您可以随时更改它
  • 将反应限制在一个通道上,在所有其他通道中都不起作用
  • 将表情符号变成了 str

确保打开 DDPortal 中的 Intent 并导入它们!