diff options
Diffstat (limited to 'code/utils')
-rw-r--r-- | code/utils/command_tree.py | 24 | ||||
-rw-r--r-- | code/utils/config.py | 28 | ||||
-rw-r--r-- | code/utils/source_helpers/apple/album.py | 13 | ||||
-rw-r--r-- | code/utils/source_helpers/apple/playlist.py | 10 | ||||
-rw-r--r-- | code/utils/source_helpers/apple/song.py | 16 | ||||
-rw-r--r-- | code/utils/source_helpers/spotify/album.py | 13 | ||||
-rw-r--r-- | code/utils/source_helpers/spotify/playlist.py | 13 | ||||
-rw-r--r-- | code/utils/source_helpers/spotify/song.py | 13 |
8 files changed, 57 insertions, 73 deletions
diff --git a/code/utils/command_tree.py b/code/utils/command_tree.py index 7ccb8b1..03ecfe4 100644 --- a/code/utils/command_tree.py +++ b/code/utils/command_tree.py @@ -1,9 +1,8 @@ import discord from discord import app_commands from discord.ext.commands.errors import * -import datetime -from utils.config import BOT_COLOR +from utils.config import create_embed from utils.custom_sources import LoadError @@ -38,16 +37,9 @@ class Tree(app_commands.CommandTree): # Custom Error class for the `create_player` function # Issues that arise may be user not in vc, user not in correct vc, missing perms, etc. elif isinstance(error, CheckPlayerError): - embed = discord.Embed( + embed = create_embed( title=error.info["title"], description=error.info["description"], - color=BOT_COLOR, - ) - embed.set_footer( - text=datetime.datetime.now(datetime.timezone.utc).strftime( - "%Y-%m-%d %H:%M:%S" - ) - + " UTC" ) try: await interaction.response.send_message( @@ -62,20 +54,13 @@ class Tree(app_commands.CommandTree): isinstance(error, app_commands.CheckFailure) and interaction.command.name in music_commands ): - embed = discord.Embed( + embed = create_embed( title="Player Creation Error", description=( "An error occured when trying to create a player. Please" " submit a bug report with </bug:1224840889906499626> if" " this issue persists." ), - color=BOT_COLOR, - ) - embed.set_footer( - text=datetime.datetime.now(datetime.timezone.utc).strftime( - "%Y-%m-%d %H:%M:%S" - ) - + " UTC" ) try: await interaction.response.send_message( @@ -85,14 +70,13 @@ class Tree(app_commands.CommandTree): await interaction.followup.send(embed=embed, ephemeral=True) elif (error, LoadError): - embed = discord.Embed( + embed = create_embed( title="Load Error", description=( "Apple Music and Spotify do not allow direct playing from" " their websites, and I was unable to load a track on a" " valid source. Please try again." ), - color=BOT_COLOR, ) # Only send the error if the interaction is still valid try: diff --git a/code/utils/config.py b/code/utils/config.py index 4569cd4..a54617c 100644 --- a/code/utils/config.py +++ b/code/utils/config.py @@ -8,6 +8,7 @@ import sys import discord import logging import requests +from datetime import datetime from colorlog import ColoredFormatter log_level = logging.DEBUG @@ -274,3 +275,30 @@ def validate_config(file_contents): LAVALINK_HOST = config["lavalink"]["host"] LAVALINK_PORT = config["lavalink"]["port"] LAVALINK_PASSWORD = config["lavalink"]["password"] + + +""" +Template for embeds +""" + + +def create_embed( + title: str, description: str, color=None, footer=None, thumbnail=None +): + embed = discord.Embed( + title=title, + description=description, + color=color if color else BOT_COLOR, + ) + + if footer: + embed.set_footer(text=footer) + else: + embed.set_footer( + text=datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S") + " UTC" + ) + + if thumbnail: + embed.set_thumbnail(url=thumbnail) + + return embed diff --git a/code/utils/source_helpers/apple/album.py b/code/utils/source_helpers/apple/album.py index 3195e2d..aa4ea0d 100644 --- a/code/utils/source_helpers/apple/album.py +++ b/code/utils/source_helpers/apple/album.py @@ -4,7 +4,7 @@ import requests from typing import Tuple, Optional from requests.exceptions import JSONDecodeError -from utils.config import BOT_COLOR, LOG +from utils.config import create_embed, LOG async def load( @@ -25,13 +25,12 @@ async def load( ) if response.status_code == 404: - embed = discord.Embed( + embed = create_embed( title="Album Not Found", description=( "The album could not be found as the provided link is" " invalid. Please try again." ), - color=BOT_COLOR, ) return None, embed @@ -62,18 +61,14 @@ async def load( if artwork_url: artwork_url = artwork_url.replace("{w}x{h}", "300x300") - embed = discord.Embed( + embed = create_embed( title="Album Queued", description=( f"**{name}** by **{artist}**\n" f"` {num_tracks} ` tracks\n\n" f"Queued by: {user.mention}" ), - color=BOT_COLOR, - ) - embed.set_thumbnail(url=artwork_url) - embed.set_footer( - text=datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S") + " UTC" + thumbnail=artwork_url, ) return album, embed diff --git a/code/utils/source_helpers/apple/playlist.py b/code/utils/source_helpers/apple/playlist.py index 8a27315..65dfbf8 100644 --- a/code/utils/source_helpers/apple/playlist.py +++ b/code/utils/source_helpers/apple/playlist.py @@ -1,10 +1,9 @@ -import datetime import discord import requests from typing import Tuple, Optional from requests.exceptions import JSONDecodeError -from utils.config import BOT_COLOR, LOG +from utils.config import create_embed, LOG async def load( @@ -24,13 +23,12 @@ async def load( ) if response.status_code == 404: - embed = discord.Embed( + embed = create_embed( title="Playlist Not Found", description=( "The playlist could not be found as the provided link is" " invalid. Please try again." ), - color=BOT_COLOR, ) return None, embed @@ -71,12 +69,12 @@ async def load( if artwork_url: artwork_url = artwork_url.replace("{w}x{h}", "300x300") - embed = discord.Embed( + embed = create_embed( title="Playlist Queued", description=( f"**{name}**\n` {num_tracks} ` tracks\n\nQueued by: {user.mention}" ), - color=BOT_COLOR, + thumbnail=artwork_url, ) # Add small alert if the playlist is the max size diff --git a/code/utils/source_helpers/apple/song.py b/code/utils/source_helpers/apple/song.py index 55db003..4190b63 100644 --- a/code/utils/source_helpers/apple/song.py +++ b/code/utils/source_helpers/apple/song.py @@ -1,10 +1,9 @@ -import datetime import discord import requests from typing import Tuple, Optional from requests.exceptions import JSONDecodeError -from utils.config import BOT_COLOR, LOG +from utils.config import create_embed, LOG async def load( @@ -25,13 +24,12 @@ async def load( ) if response.status_code == 404: - embed = discord.Embed( + embed = create_embed( title="Song Not Found", description=( "The song could not be found as the provided link is" " invalid. Please try again." ), - color=BOT_COLOR, ) return None, embed @@ -61,14 +59,10 @@ async def load( if artwork_url: artwork_url = artwork_url.replace("{w}x{h}", "300x300") - embed = discord.Embed( + embed = create_embed( title="Song Queued", - description=f"**{name}** by **{artist}**\n\nQueued by: {user.mention}", - color=BOT_COLOR, - ) - embed.set_thumbnail(url=artwork_url) - embed.set_footer( - text=datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S") + " UTC" + description=f"**{name}** by **{artist}**\n\nQueued by {user.mention}", + thumbnail=artwork_url, ) return song, embed diff --git a/code/utils/source_helpers/spotify/album.py b/code/utils/source_helpers/spotify/album.py index cf527ed..0ebc7d5 100644 --- a/code/utils/source_helpers/spotify/album.py +++ b/code/utils/source_helpers/spotify/album.py @@ -4,7 +4,7 @@ import requests from typing import Tuple, Optional from requests.exceptions import JSONDecodeError -from utils.config import BOT_COLOR, LOG +from utils.config import create_embed, LOG async def load( @@ -25,13 +25,12 @@ async def load( ) if response.status_code == 404: - embed = discord.Embed( + embed = create_embed( title="Album Not Found", description=( "The album could not be found as the provided link is" " invalid. Please try again." ), - color=BOT_COLOR, ) return None, embed @@ -56,18 +55,14 @@ async def load( LOG.error("Failed making request to Spotify API") return None, None - embed = discord.Embed( + embed = create_embed( title="Album Queued", description=( f"**{name}** by **{artist}**\n" f"` {num_tracks} ` tracks\n\n" f"Queued by: {user.mention}" ), - color=BOT_COLOR, - ) - embed.set_thumbnail(url=artwork_url) - embed.set_footer( - text=datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S") + " UTC" + thumbnail=artwork_url, ) return album, embed diff --git a/code/utils/source_helpers/spotify/playlist.py b/code/utils/source_helpers/spotify/playlist.py index c806e39..c7313a4 100644 --- a/code/utils/source_helpers/spotify/playlist.py +++ b/code/utils/source_helpers/spotify/playlist.py @@ -4,7 +4,7 @@ import requests from typing import Tuple, Optional from requests.exceptions import JSONDecodeError -from utils.config import BOT_COLOR, LOG +from utils.config import create_embed, LOG async def load( @@ -25,13 +25,12 @@ async def load( ) if response.status_code == 404: - embed = discord.Embed( + embed = create_embed( title="Playlist Not Found", description=( "The playlist could not be found as the provided link is" " invalid. Please try again." ), - color=BOT_COLOR, ) return None, embed @@ -56,18 +55,14 @@ async def load( LOG.error("Failed making request to Spotify API") return None, None - embed = discord.Embed( + embed = create_embed( title="Playlist Queued", description=( f"**{name}** from **{owner}**\n" f"` {num_tracks} ` tracks\n\n" f"Queued by {user.mention}" ), - color=BOT_COLOR, - ) - embed.set_thumbnail(url=artwork_url) - embed.set_footer( - text=datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S") + " UTC" + thumbnail=artwork_url, ) return playlist, embed diff --git a/code/utils/source_helpers/spotify/song.py b/code/utils/source_helpers/spotify/song.py index 258b652..b0c7379 100644 --- a/code/utils/source_helpers/spotify/song.py +++ b/code/utils/source_helpers/spotify/song.py @@ -4,7 +4,7 @@ import requests from typing import Tuple, Optional from requests.exceptions import JSONDecodeError -from utils.config import BOT_COLOR, LOG +from utils.config import create_embed, LOG async def load( @@ -25,13 +25,12 @@ async def load( ) if response.status_code == 404: - embed = discord.Embed( + embed = create_embed( title="Song Not Found", description=( "The song could not be found as the provided link is" " invalid. Please try again." ), - color=BOT_COLOR, ) return None, embed @@ -55,14 +54,10 @@ async def load( LOG.error("Failed making request to Spotify API") return None, None - embed = discord.Embed( + embed = create_embed( title="Song Queued", description=f"**{name}** by **{artist}**\n\nQueued by {user.mention}", - color=BOT_COLOR, - ) - embed.set_thumbnail(url=artwork_url) - embed.set_footer( - text=datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S") + " UTC" + thumbnail=artwork_url, ) return song, embed |