I cannot solve the problem. How to add a role to the user who called the !role command. Please, help.
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"
@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)
While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply. From Review