Merge pull request #10 from RafaelSolVargas/timer-handler

Adding timer control to Players to self disconnect after AFK
This commit is contained in:
Rafael Vargas 2022-01-06 20:55:21 -04:00 committed by GitHub
commit 8b8e9e603b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 0 deletions

View File

@ -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.'

View File

@ -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()

View File

@ -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()