Get a Discord Role by Id

30,340

Solution 1

You can use discord.utils.get to loop through Guild.roles and get the one you're looking for:

from discord.utils import get

role_id = 123
role = get(guild.roles, id=role_id)

Solution 2

You can simply use Guild.get_role(role_id) to get the role if you have the id.

role_id = 2134532534
role = my_server.get_role(role_id)
Share:
30,340
Aimarekin
Author by

Aimarekin

Hi there! Now, what else do you want to know?

Updated on July 09, 2022

Comments

  • Aimarekin
    Aimarekin almost 2 years

    I'm making a Discord bot but just ran into a problem.

    I want to modify a role. A specific role. I know how to do that with edit_role, but I need to get the Role object to edit it. Now, that's the problem.

    How do I get a Role object by the role's id? Or can I use the id in the Role argument?

  • Matthew Miles
    Matthew Miles about 5 years
    is guild.roles the same as message.server.roles or am i making that up?
  • Patrick Haugh
    Patrick Haugh about 5 years
    In the rewrite version, server has been renamed to guild, which is what all of the discord documentation calls them.