简体   繁体   中英

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.

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)
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM