diff options
author | Parker <contact@pkrm.dev> | 2024-10-31 00:26:00 -0500 |
---|---|---|
committer | Parker <contact@pkrm.dev> | 2024-10-31 00:26:00 -0500 |
commit | 70e612882d1093b133d13302dccfba6aa2778474 (patch) | |
tree | 6b26a4dc377ccf209c37eabf673d6031fdc10987 /code/utils | |
parent | c1b229f34c010108b0e7eb92de2102dfc07ae70c (diff) |
`black --line-length 79`
Diffstat (limited to 'code/utils')
-rw-r--r-- | code/utils/command_tree.py | 30 | ||||
-rw-r--r-- | code/utils/config.py | 27 | ||||
-rw-r--r-- | code/utils/custom_sources.py | 78 | ||||
-rw-r--r-- | code/utils/media_api_key.py | 7 |
4 files changed, 102 insertions, 40 deletions
diff --git a/code/utils/command_tree.py b/code/utils/command_tree.py index 947a58c..3d9214d 100644 --- a/code/utils/command_tree.py +++ b/code/utils/command_tree.py @@ -15,7 +15,9 @@ class CheckPlayerError(app_commands.AppCommandError): class Tree(app_commands.CommandTree): async def on_error( - self, interaction: discord.Interaction, error: app_commands.AppCommandError + self, + interaction: discord.Interaction, + error: app_commands.AppCommandError, ): music_commands = [ "play", @@ -48,7 +50,9 @@ class Tree(app_commands.CommandTree): + " UTC" ) try: - await interaction.response.send_message(embed=embed, ephemeral=True) + await interaction.response.send_message( + embed=embed, ephemeral=True + ) except discord.errors.InteractionResponded: await interaction.followup.send(embed=embed, ephemeral=True) @@ -60,7 +64,11 @@ class Tree(app_commands.CommandTree): ): embed = discord.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.", + 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( @@ -70,7 +78,9 @@ class Tree(app_commands.CommandTree): + " UTC" ) try: - await interaction.response.send_message(embed=embed, ephemeral=True) + await interaction.response.send_message( + embed=embed, ephemeral=True + ) except discord.errors.InteractionResponded: await interaction.followup.send(embed=embed, ephemeral=True) @@ -78,7 +88,13 @@ class Tree(app_commands.CommandTree): elif isinstance(error, LoadError): embed = discord.Embed( title="Nothing Found", - description="Spotify does not allow direct play, meaning songs have to be found on a supported provider. In this case, the song couldn't be found. Please try again with a different song, or try searching for just the name and artist manually rather than sending a link.", + description=( + "Spotify does not allow direct play, meaning songs have to" + " be found on a supported provider. In this case, the song" + " couldn't be found. Please try again with a different" + " song, or try searching for just the name and artist" + " manually rather than sending a link." + ), color=BOT_COLOR, ) embed.set_footer( @@ -88,7 +104,9 @@ class Tree(app_commands.CommandTree): + " UTC" ) try: - await interaction.response.send_message(embed=embed, ephemeral=True) + await interaction.response.send_message( + embed=embed, ephemeral=True + ) except discord.errors.InteractionResponded: await interaction.followup.send(embed=embed, ephemeral=True) diff --git a/code/utils/config.py b/code/utils/config.py index 1d3c349..4569cd4 100644 --- a/code/utils/config.py +++ b/code/utils/config.py @@ -12,7 +12,8 @@ from colorlog import ColoredFormatter log_level = logging.DEBUG log_format = ( - " %(log_color)s%(levelname)-8s%(reset)s | %(log_color)s%(message)s%(reset)s" + " %(log_color)s%(levelname)-8s%(reset)s |" + " %(log_color)s%(message)s%(reset)s" ) logging.root.setLevel(log_level) @@ -138,7 +139,9 @@ lavalink: sys.exit( LOG.critical( - "Configuration file `config.yaml` has been generated. Please fill out all of the necessary information. Refer to the docs for information on what a specific configuration option is." + "Configuration file `config.yaml` has been generated. Please" + " fill out all of the necessary information. Refer to the docs" + " for information on what a specific configuration option is." ) ) @@ -173,7 +176,9 @@ def validate_config(file_contents): if "bot_color" in config["bot_info"]: if not bool( re.match(hex_pattern_one, config["bot_info"]["bot_color"]) - ) and not bool(re.match(hex_pattern_two, config["bot_info"]["bot_color"])): + ) and not bool( + re.match(hex_pattern_two, config["bot_info"]["bot_color"]) + ): LOG.critical( "Error in config.yaml file: bot_color is not a valid hex color" ) @@ -187,7 +192,8 @@ def validate_config(file_contents): if len(str(config["bot_info"]["feedback_channel_id"])) != 0: if len(str(config["bot_info"]["feedback_channel_id"])) != 19: LOG.critical( - "Error in config.yaml file: feedback_channel_id is not a valid Discord channel ID" + "Error in config.yaml file: feedback_channel_id is not a" + " valid Discord channel ID" ) else: FEEDBACK_CHANNEL_ID = config["bot_info"]["feedback_channel_id"] @@ -196,7 +202,8 @@ def validate_config(file_contents): if len(str(config["bot_info"]["bug_channel_id"])) != 0: if len(str(config["bot_info"]["bug_channel_id"])) != 19: LOG.critical( - "Error in config.yaml file: bug_channel_id is not a valid Discord channel ID" + "Error in config.yaml file: bug_channel_id is not a valid" + " Discord channel ID" ) else: BUG_CHANNEL_ID = config["bot_info"]["bug_channel_id"] @@ -223,7 +230,8 @@ def validate_config(file_contents): SPOTIFY_CLIENT_SECRET = config["spotify"]["spotify_client_secret"] else: LOG.critical( - "Error in config.yaml file: Spotify client ID or secret is invalid" + "Error in config.yaml file: Spotify client ID or secret is" + " invalid" ) # @@ -243,7 +251,8 @@ def validate_config(file_contents): GENIUS_CLIENT_SECRET = config["genius"]["genius_client_secret"] else: LOG.critical( - "Error in config.yaml file: Genius client ID or secret is invalid" + "Error in config.yaml file: Genius client ID or secret is" + " invalid" ) # @@ -256,7 +265,9 @@ def validate_config(file_contents): client.models.list() OPENAI_API_KEY = config["openai"]["openai_api_key"] except openai.AuthenticationError: - LOG.critical("Error in config.yaml file: OpenAI API key is invalid") + LOG.critical( + "Error in config.yaml file: OpenAI API key is invalid" + ) # Set appropriate values for all non-optional variables TOKEN = config["bot_info"]["token"] diff --git a/code/utils/custom_sources.py b/code/utils/custom_sources.py index aea74eb..40544c1 100644 --- a/code/utils/custom_sources.py +++ b/code/utils/custom_sources.py @@ -1,7 +1,15 @@ -from lavalink import LoadResult, LoadType, Source, DeferredAudioTrack, PlaylistInfo - - -class LoadError(Exception): # We'll raise this if we have trouble loading our track. +from lavalink import ( + LoadResult, + LoadType, + Source, + DeferredAudioTrack, + PlaylistInfo, +) + + +class LoadError( + Exception +): # We'll raise this if we have trouble loading our track. pass @@ -40,7 +48,9 @@ class CustomAudioTrack(DeferredAudioTrack): ): raise LoadError - first_track = results.tracks[0] # Grab the first track from the results. + first_track = results.tracks[ + 0 + ] # Grab the first track from the results. base64 = first_track.track # Extract the base64 string from the track. self.track = base64 # We'll store this for later, as it allows us to save making network requests # if this track is re-used (e.g. repeat). @@ -76,7 +86,9 @@ class SpotifySource(Source): }, requester=user, ) - return LoadResult(LoadType.TRACK, [track], playlist_info=PlaylistInfo.none()) + return LoadResult( + LoadType.TRACK, [track], playlist_info=PlaylistInfo.none() + ) async def load_album(self, user, metadata): tracks = [] @@ -102,7 +114,9 @@ class SpotifySource(Source): ) ) - return LoadResult(LoadType.PLAYLIST, tracks, playlist_info=PlaylistInfo.none()) + return LoadResult( + LoadType.PLAYLIST, tracks, playlist_info=PlaylistInfo.none() + ) async def load_playlist(self, user, metadata): tracks = [] @@ -122,13 +136,17 @@ class SpotifySource(Source): "title": track["track"]["name"], "uri": track["track"]["external_urls"]["spotify"], "duration": track["track"]["duration_ms"], - "artworkUrl": track["track"]["album"]["images"][0]["url"], + "artworkUrl": track["track"]["album"]["images"][0][ + "url" + ], }, requster=user, ) ) - return LoadResult(LoadType.PLAYLIST, tracks, playlist_info=PlaylistInfo.none()) + return LoadResult( + LoadType.PLAYLIST, tracks, playlist_info=PlaylistInfo.none() + ) """ @@ -146,18 +164,24 @@ class AppleSource(Source): "identifier": metadata["data"][0]["id"], "isSeekable": True, "author": metadata["data"][0]["attributes"]["artistName"], - "length": metadata["data"][0]["attributes"]["durationInMillis"], + "length": metadata["data"][0]["attributes"][ + "durationInMillis" + ], "isStream": False, "title": metadata["data"][0]["attributes"]["name"], "uri": metadata["data"][0]["attributes"]["url"], - "duration": metadata["data"][0]["attributes"]["durationInMillis"], - "artworkUrl": metadata["data"][0]["attributes"]["artwork"]["url"].replace( - "{w}x{h}", "300x300" - ), + "duration": metadata["data"][0]["attributes"][ + "durationInMillis" + ], + "artworkUrl": metadata["data"][0]["attributes"]["artwork"][ + "url" + ].replace("{w}x{h}", "300x300"), }, requester=user, ) - return LoadResult(LoadType.TRACK, [track], playlist_info=PlaylistInfo.none()) + return LoadResult( + LoadType.TRACK, [track], playlist_info=PlaylistInfo.none() + ) async def load_album(self, user, metadata): tracks = [] @@ -175,19 +199,23 @@ class AppleSource(Source): "title": track["attributes"]["name"], "uri": track["attributes"]["url"], "duration": track["attributes"]["durationInMillis"], - "artworkUrl": track["attributes"]["artwork"]["url"].replace( - "{w}x{h}", "300x300" - ), + "artworkUrl": track["attributes"]["artwork"][ + "url" + ].replace("{w}x{h}", "300x300"), }, requster=user, ) ) - return LoadResult(LoadType.PLAYLIST, tracks, playlist_info=PlaylistInfo.none()) + return LoadResult( + LoadType.PLAYLIST, tracks, playlist_info=PlaylistInfo.none() + ) async def load_playlist(self, user, metadata): tracks = [] - for track in metadata["data"]: # Loop through each track in the playlist. + for track in metadata[ + "data" + ]: # Loop through each track in the playlist. tracks.append( CustomAudioTrack( { # Create an instance of our CustomAudioTrack. @@ -199,12 +227,14 @@ class AppleSource(Source): "title": track["attributes"]["name"], "uri": track["attributes"]["url"], "duration": track["attributes"]["durationInMillis"], - "artworkUrl": track["attributes"]["artwork"]["url"].replace( - "{w}x{h}", "300x300" - ), + "artworkUrl": track["attributes"]["artwork"][ + "url" + ].replace("{w}x{h}", "300x300"), }, requster=user, ) ) - return LoadResult(LoadType.PLAYLIST, tracks, playlist_info=PlaylistInfo.none()) + return LoadResult( + LoadType.PLAYLIST, tracks, playlist_info=PlaylistInfo.none() + ) diff --git a/code/utils/media_api_key.py b/code/utils/media_api_key.py index 8479010..0184c1f 100644 --- a/code/utils/media_api_key.py +++ b/code/utils/media_api_key.py @@ -19,11 +19,14 @@ def get_media_api_key(): for js_file in js_files: response = requests.get(f"{url}/assets/{js_file}") # "(?<token>(ey[\w-]+)\.([\w-]+)\.([\w-]+))" - Credit to https://github.com/topi314/LavaSrc - match = re.search(r'"(?P<token>ey[\w-]+\.[\w-]+\.[\w-]+)"', response.text) + match = re.search( + r'"(?P<token>ey[\w-]+\.[\w-]+\.[\w-]+)"', response.text + ) if match: return match.group(1) LOG.error( - "Failed to find media API key. Apple Music support will not work until a key is found or manually set." + "Failed to find media API key. Apple Music support will not work until" + " a key is found or manually set." ) return None |