From d69807b2e4302febbac55050ee3efbdee08b8e50 Mon Sep 17 00:00:00 2001 From: Parker Date: Sat, 20 Jul 2024 18:10:37 -0500 Subject: Update error handling (mainly `CheckPlayerError`) --- code/bot.py | 2 +- code/cogs/music.py | 11 ++---- code/tree.py | 82 --------------------------------------- code/utils/command_tree.py | 96 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 101 insertions(+), 90 deletions(-) delete mode 100644 code/tree.py create mode 100644 code/utils/command_tree.py (limited to 'code') diff --git a/code/bot.py b/code/bot.py index 1247e1a..5a01fc2 100644 --- a/code/bot.py +++ b/code/bot.py @@ -5,7 +5,7 @@ import requests import openai import config -from tree import Tree +from utils.command_tree import Tree class MyBot(commands.Bot): diff --git a/code/cogs/music.py b/code/cogs/music.py index 9b12c4c..0b32b9b 100644 --- a/code/cogs/music.py +++ b/code/cogs/music.py @@ -1,5 +1,6 @@ import discord from discord.ext import commands +from discord import app_commands import lavalink from lavalink import errors @@ -9,15 +10,10 @@ from config import ( LAVALINK_PORT, LOG, ) +from utils.command_tree import CheckPlayerError from ai_recommendations import add_song_recommendations -class CheckPlayerError(discord.app_commands.AppCommandError): - def __init__(self, info) -> None: - self.info = info - super().__init__() - - class LavalinkVoiceClient(discord.VoiceProtocol): """ This is the preferred way to handle external voice sending @@ -139,9 +135,10 @@ class Music(commands.Cog): raise CheckPlayerError( { "title": "Lavalink Error", - "description": "An error occured when attempting to use lavalink node. Please submit a bug report if this issue persists.", + "description": "An error occured with the Lavalink server. Please submit a bug report with if this issue persists.", } ) + should_connect = interaction.command.name in ("play",) voice_client = interaction.guild.voice_client diff --git a/code/tree.py b/code/tree.py deleted file mode 100644 index eff83b8..0000000 --- a/code/tree.py +++ /dev/null @@ -1,82 +0,0 @@ -import discord -from discord import app_commands -from discord.ext.commands.errors import * -import datetime - -from cogs.music import CheckPlayerError -from config import BOT_COLOR -from custom_sources import LoadError - - -class Tree(app_commands.CommandTree): - async def on_error( - self, interaction: discord.Interaction, error: app_commands.AppCommandError - ): - music_commands = [ - "play", - "clear", - "np", - "pause", - "queue", - "remove", - "resume", - "shuffle", - "skip", - "stop", - ] - - if isinstance(error, CommandNotFound): - return - - # 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( - 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" - ) - await interaction.response.send_message(embed=embed, ephemeral=True) - - # If `create_player` fails to create a player and fails - # to raise a `CheckPlayerError`, this will catch it - elif ( - isinstance(error, app_commands.CheckFailure) - and interaction.command.name in music_commands - ): - embed = discord.Embed( - title="Player Creation Error", - description="An error occured when creating a player. Please try again.", - color=BOT_COLOR, - ) - embed.set_footer( - text=datetime.datetime.now(datetime.timezone.utc).strftime( - "%Y-%m-%d %H:%M:%S" - ) - + " UTC" - ) - await interaction.response.send_message(embed=embed, ephemeral=True) - - # If a Spotify song is linked but cannot be found on a provider (e.g. YouTube) - 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.", - color=BOT_COLOR, - ) - embed.set_footer( - text=datetime.datetime.now(datetime.timezone.utc).strftime( - "%Y-%m-%d %H:%M:%S" - ) - + " UTC" - ) - await interaction.response.send_message(embed=embed, ephemeral=True) - - else: - raise error diff --git a/code/utils/command_tree.py b/code/utils/command_tree.py new file mode 100644 index 0000000..504d3a9 --- /dev/null +++ b/code/utils/command_tree.py @@ -0,0 +1,96 @@ +import discord +from discord import app_commands +from discord.ext.commands.errors import * +import datetime + +from config import BOT_COLOR +from custom_sources import LoadError + + +# Create a custom AppCommandError for the create_player function +class CheckPlayerError(app_commands.AppCommandError): + def __init__(self, info): + self.info = info + + +class Tree(app_commands.CommandTree): + async def on_error( + self, interaction: discord.Interaction, error: app_commands.AppCommandError + ): + music_commands = [ + "play", + "clear", + "np", + "pause", + "queue", + "remove", + "resume", + "shuffle", + "skip", + "stop", + ] + + if isinstance(error, CommandNotFound): + return + + # 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( + 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(embed=embed, ephemeral=True) + except discord.errors.InteractionResponded: + await interaction.followup.send(embed=embed, ephemeral=True) + + # If `create_player` fails to create a player and fails + # to raise a `CheckPlayerError`, this will catch it + elif ( + isinstance(error, app_commands.CheckFailure) + and interaction.command.name in music_commands + ): + embed = discord.Embed( + title="Player Creation Error", + description="An error occured when trying to create a player. Please submit a bug report with 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(embed=embed, ephemeral=True) + except discord.errors.InteractionResponded: + await interaction.followup.send(embed=embed, ephemeral=True) + + # If a Spotify song is linked but cannot be found on a provider (e.g. YouTube) + 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.", + 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(embed=embed, ephemeral=True) + except discord.errors.InteractionResponded: + await interaction.followup.send(embed=embed, ephemeral=True) + + else: + raise error -- cgit v1.2.3-70-g09d2