Discord.Py | Add Role to Someone

Discord.py Add Role to member

Here's what I came up with:

member = message.author
role = get(message.guild.roles, name = "test role")
await member.add_roles(role, atomic=True)

How to get a role to add to a user discord.py

You have reactor = await bot.fetch_user(payload.member) in your code, which is wrong. You provided a member object, instead of a member id, this is what the argument needs.

So you need to change it to reactor = await bot.fetch_user(payload.member.id)

Aside from that, you don't assign a role to anyone and your await is wrong at the role definition.

@bot.event
async def on_raw_reaction_add(payload):
channel_id = 888832144607166505
post_cid = payload.channel_id

# Get reactor with id, without an API request

reactor = payload.guild.get_member(payload.member.id)

if channel_id == post_cid:
print('Reaction added.', reactor)

# get guild by id

guild = bot.get_guild(int(payload.guild_id))


# get role

role = discord.utils.get(guild.roles, name="Member")
print(role)

# add role to reactor

await reactor.add_roles(role, reason="self-role reaction")



Sources

  • fetch_user
  • member.add_roles
  • discord.utils.get

discord.py how add role to member

@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)

Add role by ID discord.py

@bot.command()
async def addrole(ctx, user: discord.Member):
# Add the customer role to the user

guild = ctx.guild # You can remove this if you don't need it for something other
role = ctx.guild.get_role(810264985258164255)
await user.add_roles(role)

Hope this should now work :)

Discord.py | How to assign someone a role?

The answer is within the bot Forbidden: 403 Forbidden (error code: 50013): Missing Permissions should instantly tell you it's a permissions issue.

The bot fails on await member.edit(roles=[role]) # 848410518154117130 which tells you the code works but it can't assign the role.

Make sure the bot:

  • Is higher than you in the role hierarchy
  • Has Manage Roles or the Administrator permission

If this continues to not work then consider going to my previous answer about intents.

How can I add a role to someone if i don't want use discord.ext.commands? (discord.py)

You just need to use discord.Member.edit or discord.Member.add_roles. As far as I know ext.commands has nothing to do with editing members and such. It's just for making commands.

server = client.get_guild(server_ID) # Replace with the server ID
role_to_append = server.get_role(role_ID) # Replace with the role ID
user = server.get_member(member_ID) # Replace with the member ID you want to edit roles from

# user.roles is the user's current roles, you just edit it with what you want the new roles to be.
await user.edit(roles=user.roles + [role_to_append])

# or to just append a role(s)
await user.add_roles(role_to_append)


Related Topics



Leave a reply



Submit