[Python] How to get User roles on discord.py rewrite

Discussão em 'Python' iniciado por Stack, Julho 11, 2025.

  1. Stack

    Stack Membro Participativo

    New discord.py-rewrite user here.

    I'm coding a bot that has a discord interface: It is connected to more than one server (guild) and I currently need an auth system to limit its use. I thought I could get all users roles and parse them later when needed. So I did:

    @client.event
    async def on_ready():
    ...
    for guild in client.guilds:
    for member in guild.members:
    for role in member.roles:
    if role.name == "Test":
    print("USER_ID: %d - ROLE: %s" % (member.id, role.name))


    but I don't like it. I have to store these and its not efficient. Also I have to refresh with a background co-routine to check if new members join/changed roles. So my question: is there a simply way to check on the fly user roles on mutual guilds when receiving a message? Scrolling the official API the only way to get user's mutual guilds is profile() but as a bot I get a Forbidden Error, like API says.

    @client.event
    async def on_message(message):
    ...
    profile = await message.author.profile()


    discord.errors.Forbidden: FORBIDDEN (status code: 403): Bots cannot use this endpoint


    (update) ADDENDUM:

    I need to check user's roles even in private messages so the need to get mutual_guilds

    Continue reading...

Compartilhe esta Página