I have this code, it counts all the online people of all guilds, it shows up like this for example 5 guilds:
12 13 14 15 16
i use this code, but i want it to return as 1 total and not 5 seperate value's. Regards
client.guilds.cache.forEach((guild) => {
const total = guild.members.cache.filter(member => member.presence?.status == "online").size
console.log("per 1", total)
const guilds = [
{ memberCount: total }
];
const totalMembers = guilds
.map((guild) => guild.memberCount)
.reduce((prev, curr) => prev + curr, 0);
console.log("alles",totalMembers);
})
As @cmdchess said, you can use this solution using the addition assignment operator (+=). For example, this should work:
let totalMembers = 0;
client.guilds.cache.forEach((guild) => {
const total = guild.members.cache.filter(member => member.presence?.status === 'online').size;
totalMembers += total;
});
console.log('alles', totalMembers);
You can also achieve this using the increment operator (++). See my answer here for that solution. This should also work using that:
let userCount = 0;
client.guilds.cache.forEach((guild) => {
Array.from(guild.members.cache.filter(member => member.presence?.status === 'online').values()).forEach(() => userCount++);
});
console.log(userCount);
Hoped this helped!