#is there a way to get all users in a role from the interactions.Client() object?

1 messages · Page 1 of 1 (latest)

void thicket
#

I have the following method which takes the guild and role IDs:

async def getGuildRoleUsers(guildId : int, roleId : int):
   foundUsers = []
   logging.info(f"Getting guild for {guildId=}...")
   guild = await bot.fetch_guild(guildId)
   logging.info(f"Result: {guild=}.")
   if (guild is None):
       logging.warn("Found no guild, so returning.")
       return foundUsers
   logging.info(f"Getting role from {guild=} using {roleId=}...")
   role = await guild.fetch_role(roleId, force=True)
   logging.info(f"Result: {role=}.")
   if (role is None):
       logging.warn("Found no role, so returning.")
       return foundUsers
   # get users from role
   logging.info(f"{role.members=}") # this only prints cached members in the role
   return foundUsers

I have reviewed https://interactions-py.github.io/interactions.py/API Reference/API Reference/models/Discord/role/ and eventually ended up here: https://github.com/interactions-py/interactions.py/blob/stable/interactions/client/smart_cache.py#L680

But I can't seem to get the current non cached list of all members in the guild.

junior wadiBOT
#

Hey! Once your issue is solved, press the button below to close this thread!

junior wadiBOT
wraith olive
#

to get all members in a guild, you need to do set fetch_members=True in your Client object declaration, which loads a complete list of guild members.

note that this requires privileged intent GUILD_MEMBERS to be enabled, which you must enable both in your Client declaration and your Discord app dashboard.

#

Client(intents=Intents.DEFAULT | Intents.GUILD_MEMBERS, fetch_members=True, ...)

void thicket
#

from interactions.models.discord.enums import (
ComponentType,
Intents,
InteractionType,
Status,
MessageFlags,
)

#

is this where the import is from?

#

I am getting Intents not defined, so I need to import it from somewhere

#

Ok, I added that import, now it is showing that it is updating the user cache on bot start up:

INFO:interactions:Cached 944 members for 384577282171469829 in 3.48 seconds
INFO:interactions:Cached 2014 members for 310484980247822346 in 21.11 seconds

but then I still only 1 user in the role list instead of the 2 in the role.

#

oh, wait, nvm, the other user doesn't have a nick

#

they both show up now

#

can I tag a user by their ID?

void thicket
#

Ok, so I ended up with this code, that is working, but I notice it is caching the users on init, so if new users add themselves while the bot is running to the role, I am assuming it won't find them in that role until I restart?

async def getGuildRoleUsers(guildId : int, roleId : int):
   foundUsers = []
   logging.info(f"Getting guild for {guildId=}...")
   guild = await bot.fetch_guild(guildId)
   logging.info(f"Result: {guild=}.")
   if (guild is None):
       logging.warn("Found no guild, so returning.")
       return foundUsers
   logging.info(f"Getting role from {guild=} using {roleId=}...")
   role = await guild.fetch_role(roleId, force=True)
   logging.info(f"Result: {role=}.")
   if (role is None):
       logging.warn("Found no role, so returning.")
       return foundUsers
   # get users from role
   logging.info(f"{role.members=}")
   foundUsers = [{'id':member.id, 'user':member.user, 'tag':member.user.tag} for member in role.members]
   logging.info(f"{foundUsers=}")
   return foundUsers
wraith olive
#

the bot will receive the role update event and update its member cache accordingly, so the bot should be able to find these users after