From 4a0c0161bde5bd860d8a5a475687b2aaa5c7c5f7 Mon Sep 17 00:00:00 2001 From: Lukas Blacha Date: Wed, 8 Feb 2023 18:30:03 +0100 Subject: [PATCH] Added auto-disconnect feature --- Music-Bot.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/Music-Bot.py b/Music-Bot.py index d2e5652..c168b46 100644 --- a/Music-Bot.py +++ b/Music-Bot.py @@ -188,7 +188,8 @@ else: try: player = await YTDLSource.from_url(url, loop=bot.loop) ctx.voice_client.play( - player, after=await ctx.voice_client.disconnect()) + player, after=lambda e: print(f"Player error: {e}") if e else None + ) except discord.HTTPException as err: raise commands.CommandError(err) finally: @@ -200,7 +201,8 @@ else: async with ctx.typing(): player = await YTDLSource.from_url(url, loop=bot.loop, stream=True) ctx.voice_client.play( - player, after=await ctx.voice_client.disconnect()) + player, after=lambda e: print(f"Player error: {e}") if e else None + ) await ctx.respond(f"{success}Now playing: `{player.title}`\nRequested by {ctx.author.mention}") @@ -239,7 +241,8 @@ else: try: player = await YTDLSource.from_url(url, loop=bot.loop) ctx.voice_client.play( - player, after=await ctx.voice_client.disconnect()) + player, after=lambda e: print(f"Player error: {e}") if e else None + ) except discord.HTTPException as err: raise commands.CommandError(err) finally: @@ -258,6 +261,16 @@ else: elif ctx.voice_client.is_playing(): ctx.voice_client.stop() + @bot.event() + async def on_voice_state_update(ctx: commands.Context): + while True: + await asyncio.sleep(1) + if ctx.voice_client.is_playing() and not ctx.voice_client.is_paused(): + pass + else: + await ctx.voice_client.disconnect() + + @bot.event