From 4828b8fba4e13e225b3b3a771eb44902115b8ef3 Mon Sep 17 00:00:00 2001 From: Dan Date: Thu, 20 Jun 2024 23:50:44 -0400 Subject: [PATCH] FIX: Fix Playlist issues FIX: Working on fixing Client issues with PLEX --- main.py | 4 ++- modules/media/plex_module.py | 49 +++++++++++++++++++++++++++------ modules/media/spotify_module.py | 35 +++++++++++++---------- 3 files changed, 64 insertions(+), 24 deletions(-) diff --git a/main.py b/main.py index a70fbda..809f285 100644 --- a/main.py +++ b/main.py @@ -8,11 +8,13 @@ class Selena(discord.Client): self.tree = discord.app_commands.CommandTree(self) async def setup_hook(self): + guild = discord.Object(id=config.DISCORD_GUILD_ID) modules = ["modules.media.spotify_module", "modules.media.plex_module"] for module in modules: await self.load_extension(module) - await self.tree.sync() + self.tree.copy_global_to(guild=guild) + await self.tree.sync(guild=guild) async def load_extension(self, name): module = __import__(name, fromlist=["setup"]) diff --git a/modules/media/plex_module.py b/modules/media/plex_module.py index 5646213..b66da10 100644 --- a/modules/media/plex_module.py +++ b/modules/media/plex_module.py @@ -11,6 +11,35 @@ class PlexModule: self.add_commands() def add_commands(self): + @app_commands.command( + name="list_clients", description="List all available Plex clients" + ) + async def list_clients(interaction: discord.Interaction): + await interaction.response.defer() + try: + clients = self.plex.clients() + if not clients: + await interaction.followup.send( + embed=discord.Embed( + title="Plex Clients", + description="No clients found", + color=discord.Color.red() + ) + ) + return + + client_info = "\n".join( + [f"{client.title} (ID: {client.machineIdentifier})" for client in clients] # noqa: E501 + ) + embed = discord.Embed( + title="Plex Clients", + description=client_info, + color=discord.Color.blue() + ) + await interaction.followup.send(embed=embed) + except Exception as e: + await interaction.followup.send(f"An error occurred: {e}") + @app_commands.command( name="list_libraries", description="List all Plex libraries" ) @@ -64,19 +93,20 @@ class PlexModule: description="Play a movie on a specified Plex client" ) async def play_movie( - interaction: discord.Interaction, client_name: str, movie_name: str + interaction: discord.Interaction, movie_name: str, + client_name: str = None ): await interaction.response.defer() try: client = next( (c for c in self.plex.clients() if c.title == client_name), - None + self.plex.clients()[0] if self.plex.clients() else None ) if not client: await interaction.followup.send( embed=discord.Embed( title="Play Movie", - description=f"No client found with the name '{client_name}'", # noqa: E501 + description="No clients available to play the movie", # noqa: E501 color=discord.Color.red() ) ) @@ -86,7 +116,7 @@ class PlexModule: client.playMedia(movie) embed = discord.Embed( title="Playing Movie", - description=f"Playing '{movie_name}' on '{client_name}'", + description=f"Playing '{movie_name}' on '{client.title}'", color=discord.Color.green() ) await interaction.followup.send(embed=embed) @@ -98,20 +128,20 @@ class PlexModule: description="Play a TV show on a specified Plex client" ) async def play_tv_show( - interaction: discord.Interaction, client_name: str, library: str, - show_name: str, season: int, episode: int + interaction: discord.Interaction, library: str, show_name: str, + season: int, episode: int, client_name: str = None ): await interaction.response.defer() try: client = next( (c for c in self.plex.clients() if c.title == client_name), - None + self.plex.clients()[0] if self.plex.clients() else None ) if not client: await interaction.followup.send( embed=discord.Embed( title="Play TV Show", - description=f"No client found with the name '{client_name}'", # noqa: E501 + description="No clients available to play the TV show", # noqa: E501 color=discord.Color.red() ) ) @@ -122,13 +152,14 @@ class PlexModule: client.playMedia(episode) embed = discord.Embed( title="Playing TV Show", - description=f"Playing '{show_name}' S{season}E{episode} on '{client_name}'", # noqa: E501 + description=f"Playing '{show_name}' S{season}E{episode} on '{client.title}'", # noqa: E501 color=discord.Color.green() ) await interaction.followup.send(embed=embed) except Exception as e: await interaction.followup.send(f"An error occurred: {e}") + self.bot.tree.add_command(list_clients) self.bot.tree.add_command(list_libraries) self.bot.tree.add_command(search_library) self.bot.tree.add_command(play_movie) diff --git a/modules/media/spotify_module.py b/modules/media/spotify_module.py index eed9484..a116008 100644 --- a/modules/media/spotify_module.py +++ b/modules/media/spotify_module.py @@ -103,23 +103,29 @@ class SpotifyModule: @app_commands.command( name="play_playlist", description="Play a playlist by searching for it" + "or providing a link" ) async def play_playlist(interaction: discord.Interaction, query: str): await interaction.response.defer() try: - results = self.sp.search(q=query, limit=1, type="playlist") - if not results["playlists"]["items"]: - await interaction.followup.send( - embed=discord.Embed( - title="Play Playlist", - description="No results found", - color=discord.Color.red() + # Check if the query is a link + if query.startswith("https://open.spotify.com/playlist/"): + uri = query.split("/")[-1].split("?")[0] + uri = f"spotify:playlist:{uri}" + else: + # Search for the playlist + results = self.sp.search(q=query, limit=1, type="playlist") + if not results["playlists"]["items"]: + await interaction.followup.send( + embed=discord.Embed( + title="Play Playlist", + description="No results found", + color=discord.Color.red() + ) ) - ) - return - - playlist = results["playlists"]["items"][0] - uri = playlist["uri"] + return + playlist = results["playlists"]["items"][0] + uri = playlist["uri"] devices = self.sp.devices() if not devices["devices"]: @@ -136,10 +142,11 @@ class SpotifyModule: self.sp.start_playback(context_uri=uri) embed = discord.Embed( title="Now Playing Playlist", - description=f"{playlist['name']} by {playlist['owner']['display_name']}", # noqa: E501 + description=f"{playlist['name']} by {playlist['owner']['display_name']}" if not query.startswith("https://open.spotify.com/playlist/") else "Playing playlist", # noqa: E501 color=discord.Color.green() ) - embed.set_thumbnail(url=playlist['images'][0]['url']) + if not query.startswith("https://open.spotify.com/playlist/"): + embed.set_thumbnail(url=playlist['images'][0]['url']) await interaction.followup.send(embed=embed) except Exception as e: await interaction.followup.send(f"An error occurred: {e}")