0

I want to get number of members with a role, but always give me "1":

const server_roles = client.guilds.cache.get('server ID').roles.cache.get('role ID').members.size;

console.log(server_roles)

Log:

Image

Real role member count = 4, not 1

Dominik
  • 6,078
  • 8
  • 37
  • 61
Yulius
  • 23
  • 7
  • 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) – Lioness100 Nov 03 '20 at 21:36

1 Answers1

1

The issue isn't within your code, it's to do with the information bots can access after a recent update. In order to fix this:

  • Go to your Discord Developer Application page https://discord.com/developers/applications/
  • Open your Discord bot application
  • In the left side menu, select Bot
  • Scroll down to Privileged Gateway Intents
  • Turn both toggles on next to PRESENCE INTENT and SERVER MEMBERS INTENT

Then, in the code, when initialising your Discord client, add this:

Discord.Client({ ws: { intents: Discord.Intents.ALL } });

That code assumes you've imported the module as Discord

It's quite complex, but after the recent update it's the only way to achieve what you want

Ahsoka
  • 149
  • 7