From 4ed802ff43ce1e0500b0ce4bd81967d277385738 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sat, 20 Apr 2019 02:19:40 +0200 Subject: [PATCH] Do some stuff --- royalgames.py | 2 +- royalnet/audio/royalpcmaudio.py | 7 ++++--- royalnet/audio/royalpcmfile.py | 13 ++++++++----- royalnet/bots/discord.py | 4 +++- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/royalgames.py b/royalgames.py index 660ad22a..70f7cebe 100644 --- a/royalgames.py +++ b/royalgames.py @@ -39,7 +39,7 @@ tg_bot = TelegramBot(telegram_config=TelegramConfig(os.environ["TG_AK"]), database_config=DatabaseConfig(os.environ["DB_PATH"], Royal, Telegram, "tg_id"), commands=commands, error_command=ErrorHandlerCommand) -loop.run_until_complete(master.run()) +loop.run_until_complete(master.start()) loop.create_task(tg_bot.run()) loop.create_task(ds_bot.run()) print("Starting loop...") diff --git a/royalnet/audio/royalpcmaudio.py b/royalnet/audio/royalpcmaudio.py index 5ba95064..3fca7711 100644 --- a/royalnet/audio/royalpcmaudio.py +++ b/royalnet/audio/royalpcmaudio.py @@ -14,9 +14,6 @@ class RoyalPCMAudio(AudioSource): rpf_list = RoyalPCMFile.create_from_url(url) return [RoyalPCMAudio(rpf) for rpf in rpf_list] - def cleanup(self): - self._file.close() - def is_opus(self): return False @@ -28,3 +25,7 @@ class RoyalPCMAudio(AudioSource): def __repr__(self): return f"" + + def __del__(self): + self._file.close() + del self.rpf diff --git a/royalnet/audio/royalpcmfile.py b/royalnet/audio/royalpcmfile.py index b230e53b..df1ad0f7 100644 --- a/royalnet/audio/royalpcmfile.py +++ b/royalnet/audio/royalpcmfile.py @@ -2,6 +2,7 @@ import ffmpeg import os import typing import logging as _logging +import time from .youtubedl import YtdlFile, YtdlInfo log = _logging.getLogger(__name__) @@ -16,6 +17,8 @@ class RoyalPCMFile(YtdlFile): def __init__(self, info: "YtdlInfo", **ytdl_args): # Preemptively initialize info to be able to generate the filename self.info = info + # Set the time to generate the filename + self._time = time.time() # Ensure the file doesn't already exist if os.path.exists(self._ytdl_filename) or os.path.exists(self.audio_filename): raise FileExistsError("Can't overwrite file") @@ -29,10 +32,10 @@ class RoyalPCMFile(YtdlFile): ffmpeg.input(f"./{self.video_filename}") \ .output(self.audio_filename, format="s16le", ac=2, ar="48000") \ .overwrite_output() \ - .run(quiet=True) + .run(quiet=not __debug__) # Delete the video file - log.info(f"Deleting {self.video_filename}") - self.delete_video_file() + # log.info(f"Deleting {self.video_filename}") + # self.delete_video_file() def __repr__(self): return f"" @@ -44,11 +47,11 @@ class RoyalPCMFile(YtdlFile): @property def _ytdl_filename(self): - return f"./downloads/{self.info.title}-{self.info.id}.ytdl" + return f"./downloads/{self.info.title}-{str(int(self._time))}.ytdl" @property def audio_filename(self): - return f"./downloads/{self.info.title}-{self.info.id}.pcm" + return f"./downloads/{self.info.title}-{str(int(self._time))}.pcm" def __del__(self): log.info(f"Deleting {self.audio_filename}") diff --git a/royalnet/bots/discord.py b/royalnet/bots/discord.py index 98dfcfc5..9b17f0c6 100644 --- a/royalnet/bots/discord.py +++ b/royalnet/bots/discord.py @@ -206,11 +206,11 @@ class DiscordBot(GenericBot): log.debug(f"Adding {audio_source} to music_data") guild_music_data.add(audio_source) if guild_music_data.now_playing is None: - log.debug(f"Starting playback chain") await self.advance_music_data(guild) async def advance_music_data(self, guild: discord.Guild): """Try to play the next song, while it exists. Otherwise, just return.""" + log.debug(f"Starting playback chain") guild_music_data = self.music_data[guild] voice_client = self.client.find_voice_client_by_guild(guild) next_source: RoyalPCMAudio = await guild_music_data.next() @@ -219,6 +219,8 @@ class DiscordBot(GenericBot): return def advance(error=None): + if error: + raise Exception(f"Error while advancing music_data: {error}") loop.create_task(self.advance_music_data(guild)) log.debug(f"Starting playback of {next_source}")