Skip to content

Commit

Permalink
fix incorrect file paths for autoplaylist files.
Browse files Browse the repository at this point in the history
tidy up autoplaylist management, remove dead code.
add reason to removed URL through autoplaylist command.
  • Loading branch information
itsTheFae committed Jan 31, 2024
1 parent 02c3961 commit 4ba6020
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 29 deletions.
58 changes: 32 additions & 26 deletions musicbot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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...")
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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."
Expand Down
5 changes: 2 additions & 3 deletions musicbot/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 4ba6020

Please sign in to comment.