Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

483
Views
Problem at getting the memberCount of every guilds + excepting the bots in the guilds (Discord.js)

Making a bot in Discord.js, and i wanna see how many users the are in the servers that the bot is in(excepting the bots of the servers)

I've been trying for weeks but when i wanna get the value, it gives me something else..

Here's my code:

var AllUsers = 0
client.on('ready', () => {
    client.guilds.cache.forEach( guild => {
        var memners = 0
        mems = mems + guild.memberCount
        var Bcount = 0
        Bcount = Bcount + guild.members.cache.filter(member => !member.user.bot).size
        var users = mems - Bcount
        AllUsers = AllUsers + users
    })
    console.log(`the bot is ready and dealing with ${AllUsers} users`)
}

In this code i'm actually excepting the count of the bots from the memberCount itself, but my problem is that it just doesn't work! the code guild.members.cache.filter(*member* => !member.user.bot).size doesn't work! it doesn't give me an error..

It actually gives me this(i made some console.logs for that):

in my Bots server: 
members: 5    
Bcount: 1  
users: 4   
AllUsers: 4
---------  
in Code server:      
members: 4    
Bcount: 1  
users: 3   
AllUsers: 7
---------  
in Games server:  
members: 3    
Bcount: 1
users: 2
AllUsers: 9
---------

Its like the value of the Bcount is "1" while it's not!

I've tried the: guild.members.cache.filter(member => member.user.bot).size instead of: guild.members.cache.filter(member => !member.user.bot).size and this time the value of the Bcount was "0".

It's really ridiculous! cause all those three servers have 2 bots in it but it gave me 1 and 0!

Please if you know how to solve this problem help me.

thank you.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Your issue here is that you're only using the users from the member cache, which is not guaranteed to contain all users in a server. You will need to fetch the users (request them from the API) instead of using the cache.

Here is code taken directly from the Discord.js guide here:

// First use guild.members.fetch to make sure all members are cached
guild.members.fetch({ withPresences: true }).then(fetchedMembers => {
    const totalOnline = fetchedMembers.filter(member => member.presence?.status === 'online');
    // Now you have a collection with all online member objects in the totalOnline variable
    console.log(`There are currently ${totalOnline.size} members online in this guild!`);
});

However, you can't fetch members if your bot doesn't have the GUILD_MEMBERS intent along with this same thing being enabled in your discord application page (explained in the above link). Here is the official guide page for intents for reference.

We can adapt the above code to your specific purposes with the below code:

let AllUsers = 0;
// async is added here so we can use await in the function
client.on('ready', async () => {
    const guilds = client.guilds.cache;
    // Loop through all guilds
    for (let [guildId, guild] of guilds) {
        // Wait for the members to be fetched, then add to AllUsers
        const members = await guild.members?.fetch();
        // If members is defined, then add the count
        AllUsers += (members !== undefined) ? members?.filter(member => !member.user.bot).size : 0;
    }

    console.log(`the bot is ready and dealing with ${AllUsers} users`);
}

Note: Do not use var unless you have a very specific reason. Here is a decent explaination

The above code isn't very efficient (nor does it handle potential errors), it could be rewritten to fetch all members of all guilds at the same time using Promise.all(); but considering that this code only runs on startup right now, it's best not to send all of those requests to the API at once.

Do be considerate with this code though, you shouldn't be fetching all members often. In fact, Discord.js v12 used to have a .fetchAllMembers() function, but this was removed due to going against the intentions of data scraping users.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!