John doe
John doe

Reputation: 3880

How to get all guilds where a member is in?

I know how to get all members from a guild but I need to do the opposite operation : Getting all the guilds (IDs) where a specified member is registered in.

When fetching user like this client.users.cache.get(memberID); I don't see anything in the result that can allow me to see all the member's guilds :

User {
  id: '706498754712807398',
   system: null,
   locale: null,
 flags: UserFlags { bitfield: 0 },
   username: 'johndoe',
   bot: false,
  discriminator: '1023',
   avatar: null,
   lastMessageID: null,
   lastMessageChannelID: null
 }

Any suggestion ?

Upvotes: 0

Views: 1858

Answers (1)

Androz2091
Androz2091

Reputation: 3005

You can do this by using the following code:

const userID = '3383083830389'; // the ID of the user
const guilds = client.guilds.cache.filter((guild) => guild.members.cache.has(userID));

and guilds is a Collection of guilds where the user is in. This has two limitations :

  • you can only get the guilds where the bot is in too
  • this won't work if the member is not cached (this can be resolved by trying to fetch the member in each guild)

Upvotes: 2

Related Questions