简体   繁体   English

Discord Python 反应作用

[英]Discord Python Reaction Role

I want that my bot add the role "User" to a user who react with a heart.我希望我的机器人将角色“用户”添加到一个用心做出反应的用户。 But it doesn't work.但它不起作用。
My code:我的代码:

@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)

I hope you can help me:)我希望你能帮帮我:)

I would use a different event here called on_raw_reaction_add .我会在这里使用另一个名为on_raw_reaction_add的事件。 You can find the documentation for it here .您可以在此处找到它的文档。

With that new event you would have to rewrite your code a bit and add/remove some things.有了这个新事件,您将不得不稍微重写您的代码并添加/删除一些东西。

Your new code:您的新代码:

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}")

What did we do?我们做了什么?

  • Use payload使用payload
  • Used the role ID instead of the name so you can always change that使用角色 ID 而不是名称,因此您可以随时更改它
  • Limited the reaction to one channel, in all the others it will not work将反应限制在一个渠道,在所有其他渠道都不起作用
  • Turned the emoji into a str把 emoji 变成了str

Make sure to turn your Intents in the DDPortal on and import them!确保打开DDPortal中的 Intent 并导入它们!

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

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