From 4ba60208bfa74ab5e0170ddfaa429a3ec6272742 Mon Sep 17 00:00:00 2001 From: itsTheFae Date: Wed, 31 Jan 2024 11:31:55 -0800 Subject: [PATCH] fix incorrect file paths for autoplaylist files. tidy up autoplaylist management, remove dead code. add reason to removed URL through autoplaylist command. --- musicbot/bot.py | 58 +++++++++++++++++++++++++--------------------- musicbot/config.py | 5 ++-- 2 files changed, 34 insertions(+), 29 deletions(-) diff --git a/musicbot/bot.py b/musicbot/bot.py index 3e7b6771f..ee1c1db38 100644 --- a/musicbot/bot.py +++ b/musicbot/bot.py @@ -95,6 +95,8 @@ # TODO: fix perms command to send in channel if DM fails. # TODO: fix current blacklist to be more clear. # TODO: add a proper blacklist for song-related data, not just users. +# TODO: review duration related code, make it less spamy if nothing else. +# TODO: review timedelta usage to make sure time is formated as desired. no MS, no empty hours. class MusicBot(discord.Client): @@ -421,20 +423,28 @@ async def remove_url_from_autoplaylist( song_url, ) - with open( - self.config.auto_playlist_removed_file, "a", encoding="utf8" - ) as f: - ctime = time.ctime() - # add 10 spaces to line up with # Reason: - e_str = str(ex).replace("\n", "\n#" + " " * 10) - url = (song_url,) - sep = ("#" * 32,) - f.write( - f"# Entry removed {ctime}\n" - f"# URL: {url}\n" - f"# Reason: {e_str}\n" - f"\n{sep}\n\n" - ) + if not self.config.auto_playlist_removed_file.is_file(): + self.config.auto_playlist_removed_file.touch(exist_ok=True) + + try: + with open( + self.config.auto_playlist_removed_file, "a", encoding="utf8" + ) as f: + ctime = time.ctime() + # add 10 spaces to line up with # Reason: + e_str = str(ex).replace("\n", "\n#" + " " * 10) + url = song_url + sep = "#" * 32 + f.write( + f"# Entry removed {ctime}\n" + f"# URL: {url}\n" + f"# Reason: {e_str}\n" + f"\n{sep}\n\n" + ) + except ( + OSError, PermissionError, FileNotFoundError, IsADirectoryError + ): + log.exception("Could not log information about the playlist URL removal.") if delete_from_ap: log.info("Updating autoplaylist file...") @@ -1765,16 +1775,6 @@ def _get_song_url_or_none( return None - def _add_url_to_autoplaylist(self, url: str) -> None: - self.autoplaylist.append(url) - write_file(self.config.auto_playlist_file, self.autoplaylist) - log.debug("Appended %s to autoplaylist", url) - - def _remove_url_from_autoplaylist(self, url: str) -> None: - self.autoplaylist.remove(url) - write_file(self.config.auto_playlist_file, self.autoplaylist) - log.debug("Removed %s from autoplaylist", url) - async def handle_vc_inactivity(self, guild: discord.Guild) -> None: if not guild.me.voice or not guild.me.voice.channel: log.warning( @@ -2093,7 +2093,7 @@ async def cmd_id( ) async def cmd_autoplaylist( - self, _player: Optional[MusicPlayer], option: str, opt_url: str = "" + self, author: discord.Member, _player: Optional[MusicPlayer], option: str, opt_url: str = "" ) -> CommandResponse: """ Usage: @@ -2130,7 +2130,13 @@ async def cmd_autoplaylist( if option in ["-", "remove"]: if url in self.autoplaylist: - await self.remove_url_from_autoplaylist(url, delete_from_ap=True) + await self.remove_url_from_autoplaylist( + url, + ex=UserWarning( + f"Removed by command from user: {author.id}/{author.name}#{author.discriminator}" + ), + delete_from_ap=True + ) return Response( self.str.get( "cmd-unsave-success", "Removed <{0}> from the autoplaylist." diff --git a/musicbot/config.py b/musicbot/config.py index b1702b299..6a387b091 100644 --- a/musicbot/config.py +++ b/musicbot/config.py @@ -500,15 +500,14 @@ def setup_autoplaylist(self) -> None: ) # ensure cache map and removed files have values based on the configured file. - path = self.auto_playlist_file.parent stem = self.auto_playlist_file.stem ext = self.auto_playlist_file.suffix ap_removed_file = self.auto_playlist_file.with_name(f"{stem}_removed{ext}") ap_cachemap_file = self.auto_playlist_file.with_name(f"{stem}.cachemap.json") - self.auto_playlist_removed_file = path.joinpath(ap_removed_file) - self.auto_playlist_cachemap_file = path.joinpath(ap_cachemap_file) + self.auto_playlist_removed_file = ap_removed_file + self.auto_playlist_cachemap_file = ap_cachemap_file class ConfigDefaults: