Home > database >  How to count all people in all voice channel of server
How to count all people in all voice channel of server

Time:12-13

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

enter image description here

enter image description here

CodePudding user response:

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
  • Related