WasteofSpaceYT
WasteofSpaceYT

Reputation: 62

Issue getting user by id

I am having issues with discord.js and using id's to get a user in a guild. I have used two different methods of getting users in a guild: message.guild.members.cache.get(id) and client.guilds.cache.get(message.guild.id).members.cache.get(id). Both get a list of users, however they only give me the bot and myself. All other users are undefined. Any help is appreciated.

Upvotes: 0

Views: 543

Answers (2)

KaspianR
KaspianR

Reputation: 188

That's because you're only searching the cache for the user you're looking for. The cache will only contain users that the bot has seen since it started (it's more complicated than that but that's an easy way to think about it). Try fetching the user using the following instead:

message.guild.members.fetch(id);

Please note that this function returns a Promise though so you will need to either use .then() or await (provided you're in an async function) to wait until it has fetched the user from the API.

Upvotes: 4

Zero
Zero

Reputation: 2286

From what I understand you are trying to fetch the users in a guild ( cached ) so you may do

message.guild.members.cache.forEach((x) => { console.log(x.user.id) });
// or alternatively 
client.guilds.cache.get(message.guild.id).members.cache.forEach((x) => { console.log(x.user.id) });

This would loop through the whole collection of cached users and log the IDs of whole member list in your console.

Upvotes: 0

Related Questions