diff --git a/config/config.py b/config/config.py index 5583310..1b39e20 100644 --- a/config/config.py +++ b/config/config.py @@ -10,6 +10,7 @@ SECRET_MESSAGE = config('SECRET_MESSAGE') PHRASES_API = config('PHRASES_API') BOT_PREFIX = '!' +VC_TIMEOUT = 600 STARTUP_MESSAGE = 'Starting Vulkan...' STARTUP_COMPLETE_MESSAGE = 'Vulkan is now operating.' diff --git a/vulkan/music/Player.py b/vulkan/music/Player.py index 69f127e..18682a5 100644 --- a/vulkan/music/Player.py +++ b/vulkan/music/Player.py @@ -18,6 +18,7 @@ class Player(commands.Cog): self.__bot: discord.Client = bot self.__guild: discord.Guild = guild + self.__timer = Timer(self.__timeout_handler) self.__playing = False self.YDL_OPTIONS = {'format': 'bestaudio', 'noplaylist': 'True'} @@ -47,6 +48,9 @@ class Player(commands.Cog): self.__guild.voice_client.play( player, after=lambda e: self.__play_next(e, ctx)) + self.__timer.cancel() + self.__timer = Timer(self.__timeout_handler) + await ctx.invoke(self.__bot.get_command('np')) songs = self.__playlist.songs_to_preload @@ -292,3 +296,15 @@ class Player(commands.Cog): inline=False) return embedvc + + async def __timeout_handler(self) -> None: + if self.__guild.voice_client == None: + return + + if self.__guild.voice_client.is_playing() or self.__guild.voice_client.is_paused(): + self.__timer = Timer(self.__timeout_handler) + + elif self.__guild.voice_client.is_connected(): + self.__playlist.clear() + self.__playlist.loop_off() + await self.__guild.voice_client.disconnect() diff --git a/vulkan/music/utils.py b/vulkan/music/utils.py index a57efe4..2f1cf17 100644 --- a/vulkan/music/utils.py +++ b/vulkan/music/utils.py @@ -1,4 +1,6 @@ import re +import asyncio +from config import config def is_connected(ctx): @@ -34,3 +36,16 @@ def is_url(string) -> bool: return True else: return False + + +class Timer: + def __init__(self, callback): + self.__callback = callback + self.__task = asyncio.create_task(self.__executor()) + + async def __executor(self): + await asyncio.sleep(config.VC_TIMEOUT) + await self.__callback() + + def cancel(self): + self.__task.cancel()