diff --git a/v6d3music/core/queueaudio.py b/v6d3music/core/queueaudio.py index 15240de..4c1d259 100644 --- a/v6d3music/core/queueaudio.py +++ b/v6d3music/core/queueaudio.py @@ -47,10 +47,11 @@ class QueueAudio(discord.AudioSource): async def create(cls, guild: discord.Guild): return cls(guild, await QueueAudio.respawned(guild)) - async def save(self): + async def save(self, delay: bool): hybernated = [] for audio in list(self.queue): - await asyncio.sleep(0.01) + if delay: + await asyncio.sleep(0.01) hybernated.append(audio.hybernate()) queue_db.set_nowait(self.guild.id, hybernated) diff --git a/v6d3music/run-bot.py b/v6d3music/run-bot.py index 4130dd4..fd01039 100644 --- a/v6d3music/run-bot.py +++ b/v6d3music/run-bot.py @@ -6,7 +6,6 @@ import time import discord from v6d1tokens.client import request_token -from v6d2ctx.context import Benchmark, monitor from v6d2ctx.handle_content import handle_content from v6d2ctx.lock_for import lock_for from v6d2ctx.serve import serve @@ -83,18 +82,20 @@ async def on_message(message: discord.Message) -> None: await handle_content(message, message.content, prefix) -async def save_queues(): +async def save_queues(delay: bool): for mainasrc in list(mainasrcs.values()): - await asyncio.sleep(0.01) - await mainasrc.queue.save() + if delay: + await asyncio.sleep(0.01) + await mainasrc.queue.save(delay) -async def save_vcs(): +async def save_vcs(delay: bool): if vcs_restored: vcs = [] vc: discord.VoiceClient for vc in list(client.voice_clients): - await asyncio.sleep(0.01) + if delay: + await asyncio.sleep(0.01) if vc.is_playing(): if vc.guild is not None and vc.channel is not None: vcs.append((vc.guild.id, vc.channel.id, vc.is_paused())) @@ -105,15 +106,23 @@ async def save_commit(): await queue_db.set('commit', time.time()) +async def save_all(delay: bool): + await save_queues(delay) + await save_vcs(delay) + await save_commit() + + async def save_job(): while True: await asyncio.sleep(1) - with Benchmark('SVQ'): - await save_queues() - with Benchmark('SVV'): - await save_vcs() - with Benchmark('SVC'): - await save_commit() + await save_all(True) + + +async def save_daemon(): + try: + await save_job() + except asyncio.CancelledError: + pass async def start_app(): @@ -121,7 +130,8 @@ async def start_app(): async def setup_tasks(): - loop.create_task(save_job()) + global save_task + save_task = loop.create_task(save_daemon()) loop.create_task(start_app()) @@ -139,14 +149,14 @@ async def main(): token = await request_token('music', 'token') await client.login(token) loop.create_task(setup_tasks()) - if os.getenv('v6monitor'): - loop.create_task(monitor()) if os.getenv('v6tor', None) is None: try: subprocess.Popen('tor') except FileNotFoundError: print('no tor') await client.connect() + save_task.cancel() + await save_all(False) if __name__ == '__main__':