D
D
Dmitry Kostyuchenko2021-12-23 21:10:05
Bots
Dmitry Kostyuchenko, 2021-12-23 21:10:05

Python | How to get the total number of users in the voice channels of my Discord server?

I need to get the number of users in the voice channels of my Discord server, but I can’t even imagine how to implement this.

From what is available:

@bot.event
async def on_voice_state_update(member, before, after):
    voice = member.guild.voice_channels
    voiceonline = member.guild.voice_channels.members

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-12-23
@LordRubikI

As always, just read the documentation carefully.
Guild.voice_channels : List[VoiceChannel
] this is a list, and you pull some extraneous properties from it.
A separate VoiceChannel indeed has a members property : List[Member].

How next, guess what?

Что-то вроде
sum(len(vc.members) for vc in member.guild.voice_channels)


This approach is handy for initializing the number of members when the bot starts up.
But for tracking, you can not pull the above each time, but do otherwise. If before.channel is None, then the person has entered the voice chat. If after.channel is None, then the person has left the voice chat. If neither, the person simply moved from one voice channel to another. Then you store the number of users, and make him +1/-1 according to these conditions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question