Reputation: 17
I have trouble adding roles to a user in discord, here's my code:
@bot.event
async def on_raw_reaction_add(reaction):
if reaction.message_id == message_id_here:
user = bot.get_user(reaction.user_id)
await user.add_roles(name='Members')
here's the error:
AttributeError: 'User' object has no attribute 'add_roles'
Upvotes: 0
Views: 307
Reputation: 795
Few things wrong with that code.
First off all object user
represents Discord user (not tied to any guilds) while object member
is tied to specific guild.
One user can be in multiple guilds and you will have member
object of it per each guild. If you want data on guild you need the member
object.
To add roles you need to call add_roles on a member object.
So instead of getting the user
object get the member
object by getting guild then getting member from it:
@bot.event
async def on_raw_reaction_add(reaction):
if reaction.message_id == message_id_here:
guild = bot.get_guild(reaction.guild_id)
member = guild.get_member(reaction.user_id)
await member.add_roles(...)
Also note that add_roles takes a role object, you cannot just pass name='Members'
.
If you want to find that role from the guild then use utils:
...
role = find(lambda r: r.name == 'Members', guild.roles)
await member.add_roles(role)
Note that find
is in discord.utils.find
.
Upvotes: 2