User counter on the voice channel

Question:

I’m trying to make a bot for discord. Need to write a message to the chat if there are three people on the voice channel. The bot understands when a person enters or exits the channel, but the members variable takes the values 1 and -1 when entering and exiting the channel, respectively. Also, the bot does not see that the user has left if the admin moves him to another channel.

@bot.event
async def on_voice_state_update(member, before, after):
    members = 0
    if before.channel != "id" and after.channel is not None: #channel id
        if after.channel.id == "id": #channel id
            members += 1
    else:
        if before.channel == "id" or after.channel is None: #channel id
            if before.channel.id == "id": #channel id
                members -= 1
    if members == 3:
        c = bot.get_channel("id") #txt channel id
        await c.send("text")

How to make the counter work and so that its value also changes if the user moves the admin?

P.S. I apologize for the errors in the description, I use a translator

Asked By: ginta_caro

||

Answers:

I ended up doing what I wanted to do.

intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix="!", case_insensitive=True, intents=intents)

@bot.event
    async def on_voice_state_update(members, before, after):
        voice_channel = bot.get_channel(id).members #channel id
        voice_channel = len(voice_channel)
        if before.channel is None:
            if after.channel.id == id and voice_channel == 3: #channel id and 3 - the number of users required to perform actions
                c = bot.get_channel(id) #txt channel id
                await c.send("text")
        elif after.channel is None:
            pass
        else:
            if after.channel.id == id and before.channel.id != id and voice_channel == 3: #channel id and 3 - the number of users required to perform actions
                 c = bot.get_channel(id) #txt channel id
                 await c.send("text")
bot.run("token")

The bot sends a message to the text channel when the required number of people is on the voice channel (in my case, it is 3).

If there is an opportunity to somehow shorten the code or make it better, I will be glad of your advice.

P.S. again, I apologize if the description is difficult to understand, I use a translator.

Answered By: ginta_caro
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.