Wrong user count

const { EmbedBuilder, ApplicationCommandType, time, version } = require('discord.js');
const botJSON = require('../../package.json');
const config = require("../../config.json");
const superagent = require("superagent");

const apiURL = "https://api.wolvesville.com/";

async function getVerifiedGuildsCount() {
return new Promise(function(resolve, reject) {
superagent
.get(apiURL + `clans/authorized`)
.set("Authorization", `Bot ${config.wolvesvilleKey}`)
.end(async (err, res) => {
resolve(res.body.length)
})
})
}

module.exports = {
name: 'bot-info',
description: "» Shows all important information about the bot",
cooldown: 3000,
type: ApplicationCommandType.ChatInput,
options: [],
run: async (client, interaction) => {

const authorizedGuilds = await getVerifiedGuildsCount();
const guilds = client.guilds.cache.size.toLocaleString("de-DE");
const users = client.users.cache.size.toLocaleString("de-DE");

const inviteUrl = `https://discord.com/api/oauth2/authorize?client_id=${config.clientId}&permissions=8&scope=bot%20applications.commands`;
const embed = new EmbedBuilder()
.setTitle("🤖 » **__BOT INFO__**")
.setColor("#FF4181")
.setDescription("Here you find all information about the bot.")
.setThumbnail(client.user.displayAvatarURL())
.addFields(
{ name: "Bot version", value: ":pink_bot: `" + botJSON.version + "`", inline: true },
{ name: "Discord.js version", value: ":pink_dev: `" + version + "`", inline: true },
{ name: "Uptime", value: ":pink_time: " + time(Math.floor((Date.now() - client.uptime) / 1000), "R"), inline: true },
{ name: "Servers", value: ":pink_navigation: `" + guilds + "`", inline: true },
{ name: "Users", value: ":pink_users: `" + users + "`", inline: true },
{ name: "Authorized clans", value: ":shield: `" + authorizedGuilds.toString() + "`", inline: true },
{
name: "Invite Link:",
value: `:pink_link: [Click here](${inviteUrl})`,
inline: true
},
{
name: "Support server",
value: `:pink_link: [Click here](https://discord.gg/QzyyXzr8Up)`,
inline: true
}
)
.setFooter({ text: "Created by @black_wither" });

interaction.reply({ embeds: [embed] })
}
};
const { EmbedBuilder, ApplicationCommandType, time, version } = require('discord.js');
const botJSON = require('../../package.json');
const config = require("../../config.json");
const superagent = require("superagent");

const apiURL = "https://api.wolvesville.com/";

async function getVerifiedGuildsCount() {
return new Promise(function(resolve, reject) {
superagent
.get(apiURL + `clans/authorized`)
.set("Authorization", `Bot ${config.wolvesvilleKey}`)
.end(async (err, res) => {
resolve(res.body.length)
})
})
}

module.exports = {
name: 'bot-info',
description: "» Shows all important information about the bot",
cooldown: 3000,
type: ApplicationCommandType.ChatInput,
options: [],
run: async (client, interaction) => {

const authorizedGuilds = await getVerifiedGuildsCount();
const guilds = client.guilds.cache.size.toLocaleString("de-DE");
const users = client.users.cache.size.toLocaleString("de-DE");

const inviteUrl = `https://discord.com/api/oauth2/authorize?client_id=${config.clientId}&permissions=8&scope=bot%20applications.commands`;
const embed = new EmbedBuilder()
.setTitle("🤖 » **__BOT INFO__**")
.setColor("#FF4181")
.setDescription("Here you find all information about the bot.")
.setThumbnail(client.user.displayAvatarURL())
.addFields(
{ name: "Bot version", value: ":pink_bot: `" + botJSON.version + "`", inline: true },
{ name: "Discord.js version", value: ":pink_dev: `" + version + "`", inline: true },
{ name: "Uptime", value: ":pink_time: " + time(Math.floor((Date.now() - client.uptime) / 1000), "R"), inline: true },
{ name: "Servers", value: ":pink_navigation: `" + guilds + "`", inline: true },
{ name: "Users", value: ":pink_users: `" + users + "`", inline: true },
{ name: "Authorized clans", value: ":shield: `" + authorizedGuilds.toString() + "`", inline: true },
{
name: "Invite Link:",
value: `:pink_link: [Click here](${inviteUrl})`,
inline: true
},
{
name: "Support server",
value: `:pink_link: [Click here](https://discord.gg/QzyyXzr8Up)`,
inline: true
}
)
.setFooter({ text: "Created by @black_wither" });

interaction.reply({ embeds: [embed] })
}
};
Every time that I run the command again, the users var will increase insane, for example every second +10 members. The first where I use the command after every restart, it will give me the correct userCount. Can anyone tell me how to fix that?
9 Replies
d.js toolkit
d.js toolkit6mo 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!
I'm 12 years old
${client.guilds.cache.map((guild) => guild.memberCount).reduce((p, c) => p + c, 0).toLocaleString()} this is what i use
Black_Wither
Black_WitherOP6mo ago
It's constant now, but it returns me 90.000+ instead of 30.000+ I think it counts the duplicated members also
Squid
Squid6mo ago
Yes, member counts are for members, and a user can be a member of multiple guilds It's still better than relying on cached users who will (and should) generally be a fraction of the total amount of unique users in servers with your bot
FeroxNotMonday
FeroxNotMonday6mo ago
i'm not sure, but you can try this for unique members:
<Client>.guilds.cache.map(x => x.members.cache).reduce((prev, current) => [...new Set([...prev, current])], []).length
<Client>.guilds.cache.map(x => x.members.cache).reduce((prev, current) => [...new Set([...prev, current])], []).length
Black_Wither
Black_WitherOP6mo ago
It shows me the amount of guilds 😅
FeroxNotMonday
FeroxNotMonday6mo ago
one second wait
const users = new Set();
<Client>.guilds.cache.forEach(guild => guild.members.cache.forEach(member => users.add(member.id)));

users.size
const users = new Set();
<Client>.guilds.cache.forEach(guild => guild.members.cache.forEach(member => users.add(member.id)));

users.size
try it
Squid
Squid6mo ago
again though, you're relying on cached members, not all members
FeroxNotMonday
FeroxNotMonday6mo ago
Then try with fetch, but I’m not sure that the bot will fetch a large count of servers without problems in a short period of time.
Want results from more Discord servers?
Add your server