Skip to content Skip to sidebar Skip to footer

Make Bot Disconnect If It's Alone In A Voice Channel Discord.py

Is there any way to disconnect the bot if it's alone on a voice channel? Is there any event that triggers each time a person leaves the vc or something similar that would do the jo

Solution 1:

I don't know the voice channels well but you can check the member count with VoiceChannel.members and make a task loop like

asyncdefcheck():
    # define the voice channel
    member_count = len(voice_channel.members)
    if member_count == 1:
        # leave the channelawait asyncio.sleep(30)
client.loop.create_task(check())

There might be better answers but this can also solve your problem.

Solution 2:

To someone who might be having the same issue and want an answer

GUILD_VC_TIMER = {}
# this event runs when user leave / join / defen / mute @bot.eventasyncdefon_voice_state_update(member, before, after):
    # if event is triggered by the bot? returnif member.id == bot.user.id:
        return# when before.channel != None that means user has left a channelif before.channel != None:
        voice = discord.utils.get(bot.voice_clients , channel__guild__id = before.channel.guild.id)

        # voice is voiceClient and if it's none? that means the bot is not in an y VC of the Guild that triggerd this event if voice == None:
            return# if VC left by the user is not equal to the VC that bot is in? then returnif voice.channel.id != before.channel.id:
            return# if VC has only 1 member (including the bot)iflen(voice.channel.members) <= 1:

            GUILD_VC_TIMER[before.channel.guild.id] = 0whileTrue:
                print("Time" , str(GUILD_VC_TIMER[before.channel.guild.id]) , "Total Members" , str(len(voice.channel.members)))

                await asyncio.sleep(1)

                GUILD_VC_TIMER[before.channel.guild.id] += 1# if vc has more than 1 member or bot is already disconnectd ? breakiflen(voice.channel.members) >= 2ornot voice.is_connected():
                    break# if bot has been alone in the VC for more than 60 seconds ? disconnectif GUILD_VC_TIMER[before.channel.guild.id] >= 60:
                    await voice.disconnect()
                    return

Post a Comment for "Make Bot Disconnect If It's Alone In A Voice Channel Discord.py"