简体   繁体   English

Discord.py | 如何为成员添加角色?

[英]Discord.py | How can i add role to members?

I was wondering how I can set a role for a member, after he reacted to an emoji.在他对表情符号做出反应后,我想知道如何为成员设置角色。 I've tried several ways but it hasn't worked anything yet.我已经尝试了几种方法,但它还没有奏效。

AttributeError: 'Guild' object has no attribute 'add_roles' I always get this error, I tried to replace Guild with User, Payload or Member, but it doesn't find it anyway AttributeError: 'Guild' object has no attribute 'add_roles'我总是收到此错误,我尝试将 Guild 替换为 User、Payload 或 Member,但无论如何都找不到

I have two file in my ' cogs ' folder:我的“ cogs ”文件夹中有两个文件:

• roles.py - Handles the message with reactions (This file works perfectly) • roles.py - 处理带有反应的消息(此文件完美运行)

• reaction.py - 'Listen' to the reactions to the message • react.py - '听' 对消息的反应

import discord
import asyncio
import emoji
from discord.ext import commands
from discord.utils import get

client = commands.Bot(command_prefix='/',preload=True)

class Reaction(commands.Cog):

    def __init__(self, client):
        self.client = client

    @commands.Cog.listener()
    async def on_raw_reaction_add(self, payload):
        guild_id = payload.guild_id
        guild = self.client.get_guild(guild_id)
        user_id = payload.user_id
        user = self.client.get_user(user_id)
        message_id = payload.message_id
        channel = self.client.get_channel(payload.channel_id)
        emoji = payload.emoji.name

        if message_id == 809523588721934336 and emoji == "🐍" and user_id != 799612948213399572:

            message = await channel.fetch_message(message_id)
            await message.remove_reaction("🐍", user)
            dev = get(guild.roles, id=799632281639321632)
            await guild.add_roles(dev)

def setup(client):
    client.add_cog(Reaction(client))

I think it makes sense, how are you supposed to add a role to a guild?我认为有道理,你应该如何为公会添加角色? You must add it to a discord.Member instance, you can get it with payload.member您必须将其添加到discord.Member实例中,您可以使用payload.member获取它

dev = get(guild.roles, id=799632281639321632)
member = payload.member
await member.add_roles(dev)

Also remember that you must have intents.members enabled另请记住,您必须启用intents.members

Reference:参考:

In the API reference you can see that Guild does not have the method add_roles .API 参考资料中,您可以看到 Guild 没有方法add_roles Members do have that method. 成员确实有这种方法。

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

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