'Filter bots in counting server members using discord.js v13

I'm trying to create a variable that will store the user count of the message's guild id, but with no avail.

This is what I've got so far:

const guild = client.guilds.cache.get(message.guild.id)
var memberCount = guild.memberCount

It outputs the correct number of members but I'm not sure how to filter that amount so only bots will be excluded. I know this kind of question has been asked a many times on SO but the majority of the answers I've found would set the member count variable to 1, which I still don't understand why that happens.



Solution 1:[1]

Please note: You need the GUILD_MEMBERS intent enabled for this to work

const guild = await client.guilds.fetch(message.guild.id) // Fetch guild
guild.members.fetch().then(ms => { // Fetch members
  // Call reducer function, using ternary operator to count non-bots
  const nonBots = ms.reduce(acc, mem => member.user.bot ? acc : acc+1)
})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Compositr