#[solved] Get member count while filtering out bots

17 messages · Page 1 of 1 (latest)

shadow zenith
#
const memberCount = interaction.guild.members.filter(member => !member.user.bot).size;
```this apparently isn't a function, so how do I filter out the bots when getting the member count?
hollow iron
#

• What's your exact discord.js npm list discord.js and node node -v version?
• Post the full error stack trace, not just the top part!
• Show your code!
• Explain what exactly your issue is.
• Not a discord.js issue? Check out #useful-servers.

safe blaze
#

members**.cache**.filter

#

If you fetched all members before

shadow zenith
#

ok so I have to pull from the cache to get a list of members to filter through. oh ok. gotcha. thank you

shadow zenith
safe blaze
shadow zenith
#

ok so how do I fetch them before?

safe blaze
#

<Guild>.members.fetch() and wait for the promise to resolve

shadow zenith
#
const guildMembers = interaction.guild.members.fetch();
const memberCount = guildMembers.filter(member => !member.user.bot).size;
```I guess I'm lost. I did that, but it's telling me it's not a function...
safe blaze
#

and wait for the promise to resolve

#

Fetch returns a Promise

shadow zenith
#

yep...lost

sinful treeBOT
shadow zenith
#
const promise = new Promise(resolve => resolve(interaction.guild.members.fetch()))
const guildMembers = await promise;
const memberCount = guildMembers.filter(member => !member.user.bot).size;
```this was modeled from https://bobbyhadz.com/blog/javascript-wait-promise-resolve-before-returning#:~:text=async%20function%20getNum()%20%7B%0A%20%20const%20promise%20%3D%20new%20Promise(resolve%20%3D%3E%20resolve(42))%3B%0A%0A%20%20const%20num%20%3D%20await%20promise%3B%0A%0A%20%20console.log(num)%3B%20//%20%F0%9F%91%89%EF%B8%8F%2042%0A%0A%20%20return%20num%3B%0A%7D%0A%0AgetNum()%0A%20%20.then(num%20%3D%3E%20%7B%0A%20%20%20%20console.log(num)%3B%20//%20%F0%9F%91%89%EF%B8%8F%2042%0A%20%20%7D)%0A%20%20.catch(err%20%3D%3E%20%7B%0A%20%20%20%20console.log(err)%3B%0A%20%20%7D)%3B
safe blaze
#

Don’t search for code involving those two words. Actually learn how they work/what they are. Else you‘ll be lost within djs, as 80% of its methods are promise based

shadow zenith
#

ok. I've pinned the tab for the js info link you gave. I'll read over it after work. thank you