From 897db8ab31ce7898215b0fff1392b32dbf76e659 Mon Sep 17 00:00:00 2001 From: timofey Date: Tue, 11 Oct 2022 17:40:32 +0000 Subject: [PATCH] MusicClient --- v6d3music/run-bot.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/v6d3music/run-bot.py b/v6d3music/run-bot.py index fd01039..4f58f03 100644 --- a/v6d3music/run-bot.py +++ b/v6d3music/run-bot.py @@ -3,6 +3,7 @@ import os import subprocess import sys import time +import traceback import discord from v6d1tokens.client import request_token @@ -22,7 +23,18 @@ from v6d3music.utils.entries_effects_for_args import effects_db loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) register_commands() -client = discord.Client( + + +class MusicClient(discord.Client): + async def close(self) -> None: + save_task.cancel() + try: + await save_all(False, False) + except Exception: + traceback.print_exc() + await super().close() + +client = MusicClient( intents=discord.Intents( members=True, guilds=True, @@ -106,16 +118,17 @@ async def save_commit(): await queue_db.set('commit', time.time()) -async def save_all(delay: bool): +async def save_all(delay: bool, save_playing: bool): await save_queues(delay) - await save_vcs(delay) + if save_playing: + await save_vcs(delay) await save_commit() async def save_job(): while True: await asyncio.sleep(1) - await save_all(True) + await save_all(True, True) async def save_daemon(): @@ -155,8 +168,7 @@ async def main(): except FileNotFoundError: print('no tor') await client.connect() - save_task.cancel() - await save_all(False) + print('exited') if __name__ == '__main__':