Skip to content

Commit

Permalink
Merge pull request #1544 from obi23lipnik/playlist_manipulations
Browse files Browse the repository at this point in the history
Adds 'autoplaylist' command to allow better autoplaylist manipulation
  • Loading branch information
BabyBoySnow committed Sep 28, 2023
2 parents b8c744e + cc769bb commit c9f95d8
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 23 deletions.
6 changes: 6 additions & 0 deletions config/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
"cmd-save-exists": "This song is already in the autoplaylist.",
"cmd-save-invalid": "There is no valid song playing.",
"cmd-save-success": "Added <{0}> to the autoplaylist.",
"cmd-unsave-does-not-exist": "This song is not yet in the autoplaylist.",
"cmd-unsave-success": "Removed <{0}> from the autoplaylist.",
"cmd-autoplaylist-does-not-exist": "This song is not yet in the autoplaylist.",
"cmd-autoplaylist-invalid": "The supplied song link is invalid.",
"cmd-autoplaylist-option-invalid": "Invalid option \"{0}\" specified, use +, -, add, or remove",
"cmd-autoplaylist-success": "Removed <{0}> from the autoplaylist.",
"cmd-joinserver-response": "Click here to add me to a server: \n{}",
"cmd-play-spotify-album-process": "Processing album `{0}` (`{1}`)",
"cmd-play-spotify-album-queued": "Enqueued `{0}` with **{1}** songs.",
Expand Down
100 changes: 77 additions & 23 deletions musicbot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,10 @@ async def on_player_play(self, player, entry):
if self.config.embeds:
url = player.current_entry.url
# Attempt to grab video ID from possible link names
match = re.search(r'(?:youtu\.be/|youtube\.com/watch\?v=|youtube\.com/embed/)([\w-]+)', url)
match = re.search(
r"(?:youtu\.be/|youtube\.com/watch\?v=|youtube\.com/embed/)([\w-]+)",
url,
)

if match:
videoID = match.group(1)
Expand Down Expand Up @@ -1362,6 +1365,28 @@ def _gen_embed(self):
)
return e

@staticmethod
def _get_song_url_or_none(url, player):
"""Return song url if provided or one is currently playing, else returns None"""
if url or (
player.current_entry
and not isinstance(player.current_entry, StreamPlaylistEntry)
):
if not url:
url = player.current_entry.url

return url

def _add_url_to_autoplaylist(self, url):
self.autoplaylist.append(url)
write_file(self.config.auto_playlist_file, self.autoplaylist)
log.debug("Appended {} to autoplaylist".format(url))

def _remove_url_from_autoplaylist(self, url):
self.autoplaylist.remove(url)
write_file(self.config.auto_playlist_file, self.autoplaylist)
log.debug("Removed {} from autoplaylist".format(url))

async def cmd_resetplaylist(self, player, channel):
"""
Usage:
Expand Down Expand Up @@ -1520,38 +1545,64 @@ async def cmd_id(self, author, user_mentions):
delete_after=35,
)

async def cmd_save(self, player, url=None):
async def cmd_autoplaylist(self, player, option, url=None):
"""
Usage:
{command_prefix}save [url]
{command_prefix}autoplaylist [ + | - | add | remove] [url]
Saves the specified song or current song if not specified to the autoplaylist.
Adds or removes the specified song or currently playing song to/from the playlist.
"""
if url or (
player.current_entry
and not isinstance(player.current_entry, StreamPlaylistEntry)
):
if not url:
url = player.current_entry.url
url = self._get_song_url_or_none(url, player)

if url not in self.autoplaylist:
self.autoplaylist.append(url)
write_file(self.config.auto_playlist_file, self.autoplaylist)
log.debug("Appended {} to autoplaylist".format(url))
return Response(
self.str.get(
"cmd-save-success", "Added <{0}> to the autoplaylist."
).format(url)
)
if url:
if option in ["+", "add"]:
if url not in self.autoplaylist:
self._add_url_to_autoplaylist(url)
return Response(
self.str.get(
"cmd-save-success", "Added <{0}> to the autoplaylist."
).format(url),
delete_after=35,
)
else:
raise exceptions.CommandError(
self.str.get(
"cmd-save-exists",
"This song is already in the autoplaylist.",
),
expire_in=20,
)
elif option in ["-", "remove"]:
if url in self.autoplaylist:
self._remove_url_from_autoplaylist(url)
return Response(
self.str.get(
"cmd-unsave-success", "Removed <{0}> from the autoplaylist."
).format(url),
delete_after=35,
)
else:
raise exceptions.CommandError(
self.str.get(
"cmd-unsave-does-not-exist",
"This song is not yet in the autoplaylist.",
),
expire_in=20,
)
else:
raise exceptions.CommandError(
self.str.get(
"cmd-save-exists", "This song is already in the autoplaylist."
)
"cmd-autoplaylist-option-invalid",
'Invalid option "{0}" specified, use +, -, add, or remove',
).format(option),
expire_in=20,
)
else:
raise exceptions.CommandError(
self.str.get("cmd-save-invalid", "There is no valid song playing.")
self.str.get(
"cmd-autoplaylist-invalid", "The supplied song link is invalid"
),
expire_in=20,
)

@owner_only
Expand Down Expand Up @@ -2900,7 +2951,10 @@ async def cmd_np(self, player, channel, guild, message):
if self.config.embeds:
url = player.current_entry.url
# Attempt to grab video ID from possible link names
match = re.search(r'(?:youtu\.be/|youtube\.com/watch\?v=|youtube\.com/embed/)([\w-]+)', url)
match = re.search(
r"(?:youtu\.be/|youtube\.com/watch\?v=|youtube\.com/embed/)([\w-]+)",
url,
)

if match:
videoID = match.group(1)
Expand Down

0 comments on commit c9f95d8

Please sign in to comment.