Yulius
Yulius

Reputation: 23

Role member count discord JS

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

Upvotes: 0

Views: 1170

Answers (1)

Ahsoka
Ahsoka

Reputation: 184

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

Upvotes: 2

Related Questions