Warm tip: This article is reproduced from serverfault.com, please click

其他-discord.py如何为成员添加角色

(其他 - discord.py how add role to member)

发布于 2020-12-15 19:07:53

我无法解决问题。如何为调用!role命令的用户添加角色。请帮忙。

import discord
from discord.ext import commands
from apex_legends import ApexLegends
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix='!', intents = intents)

@client.command()
async def rank(ctx, user_name,):
    rank = get_apex_rank(user_name) #return str role name
    await ctx.send(f"{rank}")# successfully receiving a response from the bot 
    member = ctx.author
    role = discord.utils.get(member.guild.roles, name=rank)
    await member.add_roles(role)
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'User' object has no attribute 'guild'
def get_apex_rank(name):
    try:
        player = apex.player(name)
        a = player.__dict__
        return a['_data']['metadata']["rankName"]
    except:
        return "Wrong name"
Questioner
Denis
Viewed
0
NOT kar1m yt 2020-12-16 18:17:53
@client.command():
async def role(ctx):
    role = discord.utils.get(ctx.guild.roles, name="enter role name") #enter role name here
    user = ctx.message.author
    await user.add_roles(role)