Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

discordjs return total users in forEach?

I did try this code :

message.client.guilds.cache.forEach((guild) => {
     console.log(guild.memberCount)
})

and it returns like this:

300
200
100
etc

How to get a total in one go like them all together so it shows up like 600 instead all seperate.
I know there is an other code that shows all the users in the guild, but i just want to know how to do it this way, so i can expand the code and filter them on presence.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

i just dont know how to total the value in a forEach or map

and eventually do it this way

const total = guild.members.cache.filter(member => member.presence?.status == "online").size
console.log(total)
})

regards

>Solution :

You can use the Array.reduce() method.

const totalUsers = message.client.guilds.cache.map(el => el.memberCount).reduce((a,b) => a+b)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading