FEAT: Changed Spotify and Plex to use Embeds to make them look neater

This commit is contained in:
Dan 2024-06-20 23:50:04 -04:00
parent 4f3bc7669d
commit 100d9af588
2 changed files with 147 additions and 32 deletions

View File

@ -19,15 +19,18 @@ class PlexModule:
try: try:
libraries = self.plex.library.sections() libraries = self.plex.library.sections()
library_names = [lib.title for lib in libraries] library_names = [lib.title for lib in libraries]
await interaction.followup.send( embed = discord.Embed(
f"Plex Libraries: {', '.join(library_names)}" title="Plex Libraries",
description=", ".join(library_names),
color=discord.Color.blue()
) )
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}")
@app_commands.command( @app_commands.command(
name="search_library", name="search_library",
description="Search for a movie in a library" description="Search for a movie or TV show in a library"
) )
async def search_library( async def search_library(
interaction: discord.Interaction, library: str, query: str interaction: discord.Interaction, library: str, query: str
@ -38,20 +41,27 @@ class PlexModule:
results = lib.search(query) results = lib.search(query)
if not results: if not results:
await interaction.followup.send( await interaction.followup.send(
f"No results found for '{query}' in '{library}'" embed=discord.Embed(
title="Search Library",
description=f"No results found for '{query}' in '{library}'", # noqa: E501
color=discord.Color.red()
)
) )
return return
result_titles = [result.title for result in results] result_titles = [result.title for result in results]
await interaction.followup.send( embed = discord.Embed(
f"Search results in {library}: {', '.join(result_titles)}" title=f"Search results in {library}",
description=", ".join(result_titles),
color=discord.Color.blue()
) )
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}")
@app_commands.command( @app_commands.command(
name="play_movie", description="Play a movie on a specified Plex " name="play_movie",
"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, client_name: str, movie_name: str
@ -64,21 +74,65 @@ class PlexModule:
) )
if not client: if not client:
await interaction.followup.send( await interaction.followup.send(
f"No client found with the name '{client_name}'" embed=discord.Embed(
title="Play Movie",
description=f"No client found with the name '{client_name}'", # noqa: E501
color=discord.Color.red()
)
) )
return return
movie = self.plex.library.section('Movies').get(movie_name) movie = self.plex.library.section('Movies').get(movie_name)
client.playMedia(movie) client.playMedia(movie)
await interaction.followup.send( embed = discord.Embed(
f"Playing '{movie_name}' on '{client_name}'" title="Playing Movie",
description=f"Playing '{movie_name}' on '{client_name}'",
color=discord.Color.green()
) )
await interaction.followup.send(embed=embed)
except Exception as e:
await interaction.followup.send(f"An error occurred: {e}")
@app_commands.command(
name="play_tv_show",
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
):
await interaction.response.defer()
try:
client = next(
(c for c in self.plex.clients() if c.title == client_name),
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
color=discord.Color.red()
)
)
return
show = self.plex.library.section(library).get(show_name)
episode = show.season(season).episode(episode)
client.playMedia(episode)
embed = discord.Embed(
title="Playing TV Show",
description=f"Playing '{show_name}' S{season}E{episode} on '{client_name}'", # noqa: E501
color=discord.Color.green()
)
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_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)
self.bot.tree.add_command(play_tv_show)
async def setup(bot): async def setup(bot):

View File

@ -31,15 +31,26 @@ class SpotifyModule:
current = self.sp.currently_playing() current = self.sp.currently_playing()
if current is None or current["item"] is None: if current is None or current["item"] is None:
await interaction.followup.send( await interaction.followup.send(
"No song is currently playing" embed=discord.Embed(
title="Current Track",
description="No song is currently playing",
color=discord.Color.red()
)
) )
else: else:
track = current["item"] track = current["item"]
artist = ", ".join([a["name"] for a in track["artists"]]) artist = ", ".join([a["name"] for a in track["artists"]])
await interaction.followup.send( embed = discord.Embed(
f"Currently playing: {track['name']} by {artist} " title="Current Track",
f"({track['album']['name']})" description=f"{track['name']} by {artist}",
color=discord.Color.green()
) )
embed.add_field(
name="Album", value=track['album']['name'],
inline=False
)
embed.set_thumbnail(url=track['album']['images'][0]['url'])
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}")
@ -51,7 +62,13 @@ class SpotifyModule:
try: try:
results = self.sp.search(q=query, limit=1, type="track") results = self.sp.search(q=query, limit=1, type="track")
if not results["tracks"]["items"]: if not results["tracks"]["items"]:
await interaction.followup.send("No results found") await interaction.followup.send(
embed=discord.Embed(
title="Play Track",
description="No results found",
color=discord.Color.red()
)
)
return return
track = results["tracks"]["items"][0] track = results["tracks"]["items"][0]
@ -60,17 +77,26 @@ class SpotifyModule:
devices = self.sp.devices() devices = self.sp.devices()
if not devices["devices"]: if not devices["devices"]:
await interaction.followup.send( await interaction.followup.send(
"No active devices found. Please open Spotify on a " embed=discord.Embed(
"device." title="Play Track",
description="No active devices found."
"Please open Spotify on a device.",
color=discord.Color.red()
)
) )
return return
self.sp.start_playback(uris=[uri]) self.sp.start_playback(uris=[uri])
await interaction.followup.send( embed = discord.Embed(
f"Now playing: {track['name']} by " title="Now Playing",
f"{', '.join([a['name'] for a in track['artists']])} " description=f"{track['name']} by {', '.join([a['name'] for a in track['artists']])}", # noqa: E501
f"({track['album']['name']})" color=discord.Color.green()
) )
embed.add_field(
name="Album", value=track['album']['name'], inline=False
)
embed.set_thumbnail(url=track['album']['images'][0]['url'])
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}")
@ -83,7 +109,13 @@ class SpotifyModule:
try: try:
results = self.sp.search(q=query, limit=1, type="playlist") results = self.sp.search(q=query, limit=1, type="playlist")
if not results["playlists"]["items"]: if not results["playlists"]["items"]:
await interaction.followup.send("No results found") 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] playlist = results["playlists"]["items"][0]
@ -92,16 +124,23 @@ class SpotifyModule:
devices = self.sp.devices() devices = self.sp.devices()
if not devices["devices"]: if not devices["devices"]:
await interaction.followup.send( await interaction.followup.send(
"No active devices found. Please open Spotify on a " embed=discord.Embed(
"device." title="Play Playlist",
description="No active devices found."
"Please open Spotify on a device.",
color=discord.Color.red()
)
) )
return return
self.sp.start_playback(context_uri=uri) self.sp.start_playback(context_uri=uri)
await interaction.followup.send( embed = discord.Embed(
f"Now playing playlist: {playlist['name']} by " title="Now Playing Playlist",
f"{playlist['owner']['display_name']}" description=f"{playlist['name']} by {playlist['owner']['display_name']}", # noqa: E501
color=discord.Color.green()
) )
embed.set_thumbnail(url=playlist['images'][0]['url'])
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}")
@ -112,7 +151,13 @@ class SpotifyModule:
await interaction.response.defer() await interaction.response.defer()
try: try:
self.sp.pause_playback() self.sp.pause_playback()
await interaction.followup.send("Playback paused.") await interaction.followup.send(
embed=discord.Embed(
title="Pause",
description="Playback paused.",
color=discord.Color.green()
)
)
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}")
@ -123,7 +168,13 @@ class SpotifyModule:
await interaction.response.defer() await interaction.response.defer()
try: try:
self.sp.start_playback() self.sp.start_playback()
await interaction.followup.send("Playback resumed.") await interaction.followup.send(
embed=discord.Embed(
title="Resume",
description="Playback resumed.",
color=discord.Color.green()
)
)
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}")
@ -134,7 +185,13 @@ class SpotifyModule:
await interaction.response.defer() await interaction.response.defer()
try: try:
self.sp.next_track() self.sp.next_track()
await interaction.followup.send("Skipped to the next track.") await interaction.followup.send(
embed=discord.Embed(
title="Next Track",
description="Skipped to the next track.",
color=discord.Color.green()
)
)
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}")
@ -146,7 +203,11 @@ class SpotifyModule:
try: try:
self.sp.previous_track() self.sp.previous_track()
await interaction.followup.send( await interaction.followup.send(
"Returned to the previous track." embed=discord.Embed(
title="Previous Track",
description="Returned to the previous track.",
color=discord.Color.green()
)
) )
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}")