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 theAdministrator
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
Remove Last Few Characters in Pyspark Dataframe Column
Reading a Text File and Converting String to Float
How to Create a for Loop That Goes Through All Diagonal Possibilities of a List
Python: How to Read and Load an Excel File from Aws S3
Sklearn: Typeerror: Fit() Missing 1 Required Positional Argument: 'X"
Python/Pandas: How to Match List of Strings With a Dataframe Column
Formatting Datetimefield in Django
How to Determine If Current Time Is Within a Specified Range Using Python'S Datetime Module
Convenient Way to Handle Deeply Nested Dictionary in Python
Python Turning 2 Dimensional Strings on My List into Floats
Running an Excel Macro Via Python
How to Create String With Line Breaks in Python
Selenium Python Send_Key Error: List Object Has No Attribute
How to Handle Multiple Keys for a Dictionary in Python
Python While Loop for Finding Prime Numbers
How to Do This Horizontally Instead of Vertically in Python
How to Add Thousand Separator to Numbers in Python Pandas Dataframe