Reputation: 498
I have made a command to set someone ROLE, However, it's throwing errors.
Command raised an exception: AttributeError: 'str' object has no attribute 'add_roles'
Is there anything wrong I have did? I am using the latest discord py version.
@bot.command()
async def set_role(ctx,member = None,val: int = None):
ab = member
ab = ab.replace("<","")
ab = ab.replace(">","")
ab = ab.replace("@","")
ab = ab.replace("!","")
user = bot.get_user(int(ab))
if val == 1:
role = discord.utils.get(ctx.guild.roles, name="Test")
await user.add_roles(role)
await ctx.send('Updated')
Upvotes: 1
Views: 311
Reputation: 21
to give a role to a member u need to give the member and the name of the role as inputs. for example:
" ?set_role @clay#9871 example "
the bot gonna search for a role name 'example' from the guild roles if the bot didn't find the role it will send "role not found" that's what the "try" and "except" for
@bot.command()
async def set_role(ctx,member :discord.Member,role):
try:
add_role= discord.utils.get(ctx.guild.roles, name=role)
await member.add_roles(add_role)
await ctx.send('Updated')
except:
await ctx.send("role not found!")
Upvotes: 0
Reputation: 5330
user = bot.get_user(int(ab))
This creates a user object. It is not affiliated to a guild / discord server. By design you cannot do add_roles
.
Reason being your bot might not share a guild with this user. Or your bot might share multiple guilds with this user. But how does it know which guild you are actually adressing?
You need to create a member object. This could be done with:
member = ctx.guild.get_member(int(ab))
Now you have a member object and you can await add_roles.
await member.add_roles(role)
https://discordpy.readthedocs.io/en/latest/api.html#discord.Member.add_roles
Upvotes: 2