'How do you assign a role to a user in pycord?
I have a discord bot that needs to add someone to the ban watch list, which is a role. But so far, all I can find are discord.py solutions, and since discord.py is discontinued, I use pycord instead. The bot has full admin privileges, so those are no worry.
dmchannel = await user.create_dm()
dmchannel.send(f"<@{user.id}> You have been put on the ban watch list! Be careful, you could be banned soon.")
# Add the role here
How would I manage to do this?
Solution 1:[1]
There are two ways to add a role
1. member.add_roles(role)
Where member is discord.Member and role is discord.Role and we are using the add_roles() method to add the role which takes the role as a parameter.
You can get the role from the guild.get_role() method which takes the role ID as a parameter or u can use discord.utils.get(guild.roles , name="name") to get it from its name
then use that role to add to the member
2. member.edit(roles=roles)
You need to get all roles first with the member.roles attribute which returns a list of all roles the member has then u can append the role to the list. roles.append(role) the use member.edit(roles) this will add that role to the member
Solution 2:[2]
You can get the role using discord utils, then using member.add_roles():
member = #member
role = discord.utils.get(ctx.guild.roles, name=#role name)
member.add_roles(role)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Yash Verma |
| Solution 2 | RiveN |
