Anna w.t
Anna w.t

Reputation: 27

How to count all people in all voice channel of server

I am using typescript and I used this code: but it doesn't work

let count = 0;
const userCount = client.guilds.cache.get("MY Server_ID")?.channels.cache.filter(ch => ch.type === "GUILD_VOICE").map((ch) => count  +=  ch.members.size);

ERROR:

Property 'size' does not exist on type 'Collection<string, GuildMember> | ThreadMemberManager'.
  Property 'size' does not exist on type 'ThreadMemberManager'.

docs said that ch.members have said but it gives an error

https://discord.js.org/#/docs/main/stable/class/GuildChannel?scrollTo=members https://discord.js.org/#/docs/collection/main/class/Collection

enter image description here

enter image description here

Upvotes: 0

Views: 470

Answers (1)

MrMythical
MrMythical

Reputation: 9041

You can use the .isVoice() type guard and Array#reduce()

let channels = client.guilds.cache.get("MY Server_ID")?.channels.cache.filter(ch => ch.type === "GUILD_VOICE")
let userCount = channels.reduce((a, c) => {
  if (!c.isVoice()) return;
  return a + c.members.size
}, 0) // should be member count in all channels

But, a better way to check the amount of people who are in a VC is by filtering members

await client.guilds.cache.get("MY Server_ID")?.members.fetch()
let userCount = client.guilds.cache.get("MY Server_ID")?.members.cache.filter(m => m.voice.channel).size

Upvotes: 1

Related Questions