1

Trying to fetch the list of users on a discord guild(server) but whenever I fetch the list it always returns 1 user that is the bot itself. Tried to execute

console.log(client.guilds.cache.get(config.SERVER_ID).members.cache.size)
console.log(client.guilds.cache.get(config.SERVER_ID).memberCount)

The first line returns 1 and second-line returns 420(number of users on discord server). I am not able to update the client's cache. Any help would be appreciated

Yash Lotan
  • 378
  • 1
  • 5
  • 21

1 Answers1

2

Your first line is returning 1 since your bot is the only cached user on the server. Your second line returns the correct amount because Guild.memberCount doesn't need any users to be cached, it takes the data directly from the guild.

If you want to cache every member of a guild, you can use GuildMemberManager.fetch().

client.guilds.cache
 .get(config.SERVER_ID)
 .members.fetch()
 .then((members) => {
  console.log(members.size); // 420
  // code...
 });
Lioness100
  • 8,260
  • 6
  • 18
  • 49
  • It throws (node:4652) UnhandledPromiseRejectionWarning: Error [GUILD_MEMBERS_TIMEOUT]: Members didn't arrive in time. – Yash Lotan Oct 28 '20 at 05:17
  • 1
    [None of my guilmember events are emitting and my functions are timing out](https://stackoverflow.com/questions/64559390/none-of-my-discord-js-guildmember-events-are-emitting-and-my-functions-are-timin) – Lioness100 Oct 28 '20 at 11:35
  • Thanks! it worked. I was missing the required intents – Yash Lotan Oct 28 '20 at 13:36