Home > database >  How to have my Discord Bot (Python API) leave a voice channel in an event?
How to have my Discord Bot (Python API) leave a voice channel in an event?

Time:12-28

I need help having my discord bot leave a voice channel through an event with the Python API. I've created an event that makes my bot join a voice channel if there is only 1 person present in said channel after 5 seconds, this part works. I then need my bot to leave the channel if another person joins or the one person leaves. I have not been able to figure out how to get it to leave. Any help would be much appreciated! Here is what my method looks like right now, need help at the "#Leave voice channel" comment:

@client.event
async def on_voice_state_update(member, before, after):
if member == client.user:  #CATCH
    return

if after.channel is None: #User has left a voice channel
    print("User left voice channel")
    return

else:
    if before.channel is not after.channel:
        memids = []
        VC = member.voice.channel

        for mem in VC.members:
            memids.append(mem.id)

        if len(memids) == 1:
            await asyncio.sleep(5)  #to be 10
            print(len(memids))
            if len(memids) == 1:
                await VC.connect()
                print("Joined Channel")
            else:
                print("Not Alone Anymore...")
                return
        else:
            print("!=1")
            #Leave voice channel

    else:
        return
    return
return

CodePudding user response:

You can use await ctx.voice_client.disconnect() to disconnect.

CodePudding user response:

hope i helped

 @client.command()
       async def leave(ctx):
           voice = get(client.voice_clients, guild=ctx.guild)
    
           if voice.is_playing():
               voice.stop()
               await ctx.send('Stopping...')

   
  • Related