I'm using
let real_members = interaction.guild.members.cache.filter((member) => !member.user.bot).size;
to get all members excluding the bots on a server, but this somehow always returns 1. (Slash command)
Fetch all the GuildMember's first and access the returning collection, then sweep (remove) all the members who are bots.
let real_members;
interaction.guild.members.fetch().then(all_members => {
real_members = all_members.sweep(member => member.user.bot);
});
console.log(real_members.size);
Ensure you have Intents.FLAGS.GUILD_MEMBERS included in your client's intents aswell as enabled in your Discord developer portal
Discord.JS Collection Utility Functions
Maybe try using ".fetch" so:
interaction.guild.members.fetch()
.then ((members) => {
const AmountOfMembers = members.filter((member) => !member.user.bot).size
})
CODE IS NOT TESTED
To be able to access it outside either define variable outside or turn it into a function and return the value.
function getMembers (interaction) {
interaction.guild.members.fetch()
.then ((members) => {
members.filter((member) => !member.user.bot).size
.then((Amount) => {
return Amount
})
})
};
const AmountOfMembers = getMembers(interaction);
Just pass it your interaction.