FIX: Fix Playlist issues

FIX: Working on fixing Client issues with PLEX
This commit is contained in:
Dan 2024-06-20 23:50:44 -04:00
parent 100d9af588
commit 4828b8fba4
3 changed files with 64 additions and 24 deletions

View File

@ -8,11 +8,13 @@ class Selena(discord.Client):
self.tree = discord.app_commands.CommandTree(self) self.tree = discord.app_commands.CommandTree(self)
async def setup_hook(self): async def setup_hook(self):
guild = discord.Object(id=config.DISCORD_GUILD_ID)
modules = ["modules.media.spotify_module", modules = ["modules.media.spotify_module",
"modules.media.plex_module"] "modules.media.plex_module"]
for module in modules: for module in modules:
await self.load_extension(module) 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): async def load_extension(self, name):
module = __import__(name, fromlist=["setup"]) module = __import__(name, fromlist=["setup"])

View File

@ -11,6 +11,35 @@ class PlexModule:
self.add_commands() self.add_commands()
def add_commands(self): 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( @app_commands.command(
name="list_libraries", description="List all Plex libraries" name="list_libraries", description="List all Plex libraries"
) )
@ -64,19 +93,20 @@ class PlexModule:
description="Play a movie on a specified Plex client" description="Play a movie on a specified Plex client"
) )
async def play_movie( 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() await interaction.response.defer()
try: try:
client = next( client = next(
(c for c in self.plex.clients() if c.title == client_name), (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: if not client:
await interaction.followup.send( await interaction.followup.send(
embed=discord.Embed( embed=discord.Embed(
title="Play Movie", 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() color=discord.Color.red()
) )
) )
@ -86,7 +116,7 @@ class PlexModule:
client.playMedia(movie) client.playMedia(movie)
embed = discord.Embed( embed = discord.Embed(
title="Playing Movie", title="Playing Movie",
description=f"Playing '{movie_name}' on '{client_name}'", description=f"Playing '{movie_name}' on '{client.title}'",
color=discord.Color.green() color=discord.Color.green()
) )
await interaction.followup.send(embed=embed) await interaction.followup.send(embed=embed)
@ -98,20 +128,20 @@ class PlexModule:
description="Play a TV show on a specified Plex client" description="Play a TV show on a specified Plex client"
) )
async def play_tv_show( async def play_tv_show(
interaction: discord.Interaction, client_name: str, library: str, interaction: discord.Interaction, library: str, show_name: str,
show_name: str, season: int, episode: int season: int, episode: int, client_name: str = None
): ):
await interaction.response.defer() await interaction.response.defer()
try: try:
client = next( client = next(
(c for c in self.plex.clients() if c.title == client_name), (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: if not client:
await interaction.followup.send( await interaction.followup.send(
embed=discord.Embed( embed=discord.Embed(
title="Play TV Show", 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() color=discord.Color.red()
) )
) )
@ -122,13 +152,14 @@ class PlexModule:
client.playMedia(episode) client.playMedia(episode)
embed = discord.Embed( embed = discord.Embed(
title="Playing TV Show", 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() color=discord.Color.green()
) )
await interaction.followup.send(embed=embed) await interaction.followup.send(embed=embed)
except Exception as e: except Exception as e:
await interaction.followup.send(f"An error occurred: {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(list_libraries)
self.bot.tree.add_command(search_library) self.bot.tree.add_command(search_library)
self.bot.tree.add_command(play_movie) self.bot.tree.add_command(play_movie)

View File

@ -103,23 +103,29 @@ class SpotifyModule:
@app_commands.command( @app_commands.command(
name="play_playlist", name="play_playlist",
description="Play a playlist by searching for it" description="Play a playlist by searching for it"
"or providing a link"
) )
async def play_playlist(interaction: discord.Interaction, query: str): async def play_playlist(interaction: discord.Interaction, query: str):
await interaction.response.defer() await interaction.response.defer()
try: try:
results = self.sp.search(q=query, limit=1, type="playlist") # Check if the query is a link
if not results["playlists"]["items"]: if query.startswith("https://open.spotify.com/playlist/"):
await interaction.followup.send( uri = query.split("/")[-1].split("?")[0]
embed=discord.Embed( uri = f"spotify:playlist:{uri}"
title="Play Playlist", else:
description="No results found", # Search for the playlist
color=discord.Color.red() 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
return playlist = results["playlists"]["items"][0]
uri = playlist["uri"]
playlist = results["playlists"]["items"][0]
uri = playlist["uri"]
devices = self.sp.devices() devices = self.sp.devices()
if not devices["devices"]: if not devices["devices"]:
@ -136,10 +142,11 @@ class SpotifyModule:
self.sp.start_playback(context_uri=uri) self.sp.start_playback(context_uri=uri)
embed = discord.Embed( embed = discord.Embed(
title="Now Playing Playlist", 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() 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) await interaction.followup.send(embed=embed)
except Exception as e: except Exception as e:
await interaction.followup.send(f"An error occurred: {e}") await interaction.followup.send(f"An error occurred: {e}")