From 52099fa794af469d8bdaaa1b2965bb7a4641a3b1 Mon Sep 17 00:00:00 2001 From: Carsten Burgard Date: Sun, 13 Feb 2022 13:12:35 +0100 Subject: [PATCH] added option to show available playlists --- PlexBot/bot.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/PlexBot/bot.py b/PlexBot/bot.py index b1e5bce..d077981 100644 --- a/PlexBot/bot.py +++ b/PlexBot/bot.py @@ -31,6 +31,7 @@ Plex: play - Play a song from the plex server. album - Queue an entire album to play. playlist - Queue an entire playlist to play. + show_playlists - Query for playlists with a name matching any of the arguments. lyrics - Print the lyrics of the song (Requires Genius API) np - Print the current playing song. stop - Halt playback and leave vc. @@ -267,6 +268,15 @@ class Plex(commands.Cog): except NotFound: raise MediaNotFoundError("Playlist cannot be found") + def _get_playlists(self): + """ + Search the Plex music db for playlist + + Returns: + List of plexapi.playlist + """ + return self.pms.playlists() + async def _play(self): """ Heavy lifting of playing songs @@ -627,6 +637,41 @@ class Plex(commands.Cog): except MediaNotFoundError: await ctx.send(message="Playlist "+title+" seems to be empty!") bot_log.debug("Playlist empty - %s", title) + @command() + async def show_playlists(self, ctx, *args): + """ + User command to show playlists + + Searchs plex db and shows playlists matching. + + Args: + ctx: discord.ext.commands.Context message context from command + *args: String filter for playlist names + + Returns: + None + + Raises: + None + """ + # Save the context to use with async callbacks + self.ctx = ctx + + playlists = self._get_playlists() + + try: + await self._validate(ctx) + except VoiceChannelError: + pass + + for playlist in playlists: + if args and not any(arg in playlist.title for arg in args): + continue + from datetime import timedelta + if playlist.duration: + seconds = playlist.duration / 1000 + embed, img = self._build_embed_playlist(self, playlist, playlist.title, "{:0>8}".format(str(timedelta(seconds=seconds)))) + await ctx.send(embed=embed, file=img) @command() async def stop(self, ctx):