简体   繁体   中英

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

I have two file in my ' cogs ' folder:

• roles.py - Handles the message with reactions (This file works perfectly)

• reaction.py - 'Listen' to the reactions to the message

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

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

Also remember that you must have intents.members enabled

Reference:

In the API reference you can see that Guild does not have the method add_roles . Members do have that method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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