Stats command returning wrong member count after X hours

const promises = [
client.shard.broadcastEval(c => c.guilds.cache.size)
.catch(error => {
console.error(new Date(), `Error in guild count for shard:`, error);
return 0;
}),
client.shard.broadcastEval(c => c.guilds.cache.reduce((acc, guild) => acc + guild.memberCount, 0))
.catch(error => {
console.error(new Date(), `Error in member count for shard:`, error);
return 0;
}),
client.shard.broadcastEval(c => c.ws.status === 0 ? true : false)
.catch(error => {
console.error(new Date(), `Error in shard connection status:`, error);
return false;
})
];

let totalGuilds;
let totalMembers;
let responsiveShards;
await Promise.all(promises.map(promise =>
Promise.resolve(promise)
))
.then(results => {
totalGuilds = results[0].reduce((acc, guildCount) => acc + guildCount, 0);
totalMembers = results[1].reduce((acc, memberCount) => acc + memberCount, 0);
responsiveShards = results[2].filter(responsive => responsive).length;
})
.catch(error => {
console.error(new Date(), `Error fetching shard data:`, error);
});
const promises = [
client.shard.broadcastEval(c => c.guilds.cache.size)
.catch(error => {
console.error(new Date(), `Error in guild count for shard:`, error);
return 0;
}),
client.shard.broadcastEval(c => c.guilds.cache.reduce((acc, guild) => acc + guild.memberCount, 0))
.catch(error => {
console.error(new Date(), `Error in member count for shard:`, error);
return 0;
}),
client.shard.broadcastEval(c => c.ws.status === 0 ? true : false)
.catch(error => {
console.error(new Date(), `Error in shard connection status:`, error);
return false;
})
];

let totalGuilds;
let totalMembers;
let responsiveShards;
await Promise.all(promises.map(promise =>
Promise.resolve(promise)
))
.then(results => {
totalGuilds = results[0].reduce((acc, guildCount) => acc + guildCount, 0);
totalMembers = results[1].reduce((acc, memberCount) => acc + memberCount, 0);
responsiveShards = results[2].filter(responsive => responsive).length;
})
.catch(error => {
console.error(new Date(), `Error fetching shard data:`, error);
});
For the first hours, sometimes even a day it returns the correct member count, but sometimes randomly it starts returning the wrong member count. Then if I restart the bot, it shows the correct count again. Before restart: Server Count: 34,726 Member Count: 21,783 After restart: Server Count: 34,697 Member Count: 1,367,868 It has even said "Member Count: 0" before.
15 Replies
d.js toolkit
d.js toolkit3w ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
bolts
bolts3w ago
You can use guild.members.fetch() to refresh your cache.
Iced Queen
Iced QueenOP3w ago
You think it's a cache issue? But doesn't it take kinda long to complete that fetch if there are like 1.3M members? So what else could it be? Cause I have been clueless about it for really long 😅
bolts
bolts3w ago
if your shard disconnects it could desync calm down Im just trying to provide some insight
Iced Queen
Iced QueenOP3w ago
That the total shards is the same as responsiveShards Well I can't check all 35 shards just like that Well no one is, so I assume that all shards are responsive. But I can't know with a 100% certainty as users could technically just not report it But if we go with if users reporting downtime, then all shards are responsive as no one reports it
Iced Queen
Iced QueenOP3w ago
Do you mean replacing the responsiveShards with this then?
No description
Iced Queen
Iced QueenOP3w ago
(c.user, not client.user)
Iced Queen
Iced QueenOP3w ago
So replace it with this?
No description
Iced Queen
Iced QueenOP3w ago
Righttt
Iced Queen
Iced QueenOP3w ago
So now 😅 Replace it with this?
No description
Iced Queen
Iced QueenOP3w ago
Oh you're right... adding .then
const promises = [
client.shard.broadcastEval(c => c.guilds.cache.size)
.catch(error => {
console.error(new Date(), `Error in guild count for shard:`, error);
return 0;
}),
client.shard.broadcastEval(c => c.guilds.cache.reduce((acc, guild) => acc + guild.memberCount, 0))
.catch(error => {
console.error(new Date(), `Error in member count for shard:`, error);
return 0;
}),
client.shard.broadcastEval(async c => await c.guilds.cache.first().members.fetch({ user: c.user, limit: 1 }))
.then(() => true)
.catch(() => false)
];

let totalGuilds;
let totalMembers;
let responsiveShards = 0;
await Promise.all(promises.map(promise =>
Promise.resolve(promise)
))
.then(results => {
totalGuilds = results[0].reduce((acc, guildCount) => acc + guildCount, 0);
totalMembers = results[1].reduce((acc, memberCount) => acc + memberCount, 0);
if (results[2]) responsiveShards++;
})
.catch(error => {
console.error(new Date(), `Error fetching shard data:`, error);
});
const promises = [
client.shard.broadcastEval(c => c.guilds.cache.size)
.catch(error => {
console.error(new Date(), `Error in guild count for shard:`, error);
return 0;
}),
client.shard.broadcastEval(c => c.guilds.cache.reduce((acc, guild) => acc + guild.memberCount, 0))
.catch(error => {
console.error(new Date(), `Error in member count for shard:`, error);
return 0;
}),
client.shard.broadcastEval(async c => await c.guilds.cache.first().members.fetch({ user: c.user, limit: 1 }))
.then(() => true)
.catch(() => false)
];

let totalGuilds;
let totalMembers;
let responsiveShards = 0;
await Promise.all(promises.map(promise =>
Promise.resolve(promise)
))
.then(results => {
totalGuilds = results[0].reduce((acc, guildCount) => acc + guildCount, 0);
totalMembers = results[1].reduce((acc, memberCount) => acc + memberCount, 0);
if (results[2]) responsiveShards++;
})
.catch(error => {
console.error(new Date(), `Error fetching shard data:`, error);
});
Shard Count: 35 Shards Online: 1 Why's only one shard showing as online? Because results[2] would be either "true" or "false" It errors if I use filter tho TypeError: results[2].filter is not a function I do that inside the .then? Before I reassign responsiveShards Oooh, so
client.shard.broadcastEval(async c => await c.guilds.cache.first().members.fetch({ user: c.user, limit: 1 })
.then(() => true)
.catch(() => false))
client.shard.broadcastEval(async c => await c.guilds.cache.first().members.fetch({ user: c.user, limit: 1 })
.then(() => true)
.catch(() => false))
Ahh yeah, on my test bot it returned [ true ] Good catch, thank you 😄 Looks correct on prod bot as well :)) Shard Count: 35 Shards Online: 35 I'll come back to this post when it gives the wrong member count again and be able to tell if a shard is offline Server Count: 34,792 Member Count: 26,592 Shard Count: 35 Shards Online: 35 Ruling out offline shards Could it be a caching issue? Do I need to client.members.fetch() every 2 hours or something to keep the cache up to date? Or could it be something else
bolts
bolts3w ago
you could add a check. if totalMembers drops significantly or returns unusually low (like 0), trigger a refetch for all shards to refresh member data.
Iced Queen
Iced QueenOP2w ago
Actually error logs do go to a separate file and there isn’t anything there .log goes to my log file, .error goes to my error file 😅 Goes when I get errors, they go in my error file? I’m not sure what you’re asking 😅 I run my bot on a Linux server and use pm2, and there are 2 logging files, one for just logs and one for error logs. Console.error throws them in the error log file. And it’s been doing so correctly for many many months 😅 Uh I’m not sure. I wasn’t the one to set up the Linux server and pm2. But whenever I get an error, it always goes in the error file. Like whether it’s a TypeError, ReferenceError or a Discord error Any ideas? @Qjuh sorry for the ping but do you have any ideas for it? I mean it's kinda common politeness to then say you don't instead of ignoring, but okay I replied to your message 🤦‍♀️
Steve
Steve2w ago
Shut the friggle up
Want results from more Discord servers?
Add your server