0

I tried doing a role count for a specific role with a role ID. My problem now is that the count isn't working properly. I gave 4 people the role and the bot is only counting one person with that role and I don't know why.

My Code:

case 'new':
   let roleID = "877984124483400480";
   let membersWithRole = message.guild.roles.cache.get(roleID).members;
   message.channel.send(`There are ${membersWithRole.size} People with the new Group on our discord`)
break;
MrMythical
  • 8,908
  • 2
  • 17
  • 45
I need Help
  • 204
  • 3
  • 13
  • 1
    Try fetching all the members – Elitezen Oct 22 '21 at 12:57
  • Can you show me where to fetch the members? I don't really understand what to do – I need Help Oct 22 '21 at 13:19
  • Before declaring `membersWithRole`, use `await message.guild.members.fetch()` – MrMythical Oct 22 '21 at 13:27
  • I tried that but now it's giving me an error after a while. `Error [GUILD_MEMBERS_TIMEOUT]: Members didn't arrive in time.` – I need Help Oct 22 '21 at 13:38
  • Does this answer your question? [None of my discord.js guildmember events are emitting, my user caches are basically empty, and my functions are timing out?](https://stackoverflow.com/questions/64559390/none-of-my-discord-js-guildmember-events-are-emitting-my-user-caches-are-basica) – MrMythical Oct 22 '21 at 14:43

1 Answers1

0

I recommend fetching it because cache is unreliable.

case 'new':
   let roleID = "877984124483400480";
   const rolesCache = await message.guild.roles.cache.fetch()
   let membersWithRole(roleID).members;
   message.channel.send(`There are ${membersWithRole.size} People with the new Group on our discord`)
break;

Or it can be the intents. Check if you have server members intents and have GUILD_MEMBERS intent in the client options.

Keilorus
  • 60
  • 2
  • 12