How to use add_roles method from python discord api
So, I was trying to figure out how to use the add_roles () method, but I ran into the problem of not being able to find out how to get a specific role object, and also wondered if the member argument allows as a user id or username. I'm new to this, so an explanation with an example would be the preferred means of answering if possible.
source to share
add_roles()
You must pass a member object when used . To get a specific role object, you must use a function discord.utils.get()
.
In the example below, we first retrieve the role. Notice how we need a server instance for this. Also note that this is an attribute lookup, so it name
could be, for example id
, or even both. See the documentation for details . We then use a function add_roles()
that passes first, first the object member
, and then the role
one we just got.
Example:
role = discord.utils.get(server.roles, name="admin")
await client.add_roles(member, role)
If you are having problems with discord.py, I would recommend reading the documentation and / or reading other bots' code using discord.py on GitHub.
source to share