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/utils/command_tree.py similarity index 71% rename from code/tree.py rename to code/utils/command_tree.py index eff83b8..504d3a9 100644 --- a/code/tree.py +++ b/code/utils/command_tree.py @@ -3,11 +3,16 @@ 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 +# 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 @@ -42,7 +47,10 @@ class Tree(app_commands.CommandTree): ) + " UTC" ) - await interaction.response.send_message(embed=embed, ephemeral=True) + 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 @@ -52,7 +60,7 @@ class Tree(app_commands.CommandTree): ): embed = discord.Embed( title="Player Creation Error", - description="An error occured when creating a player. Please try again.", + 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( @@ -61,7 +69,10 @@ class Tree(app_commands.CommandTree): ) + " UTC" ) - await interaction.response.send_message(embed=embed, ephemeral=True) + 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): @@ -76,7 +87,10 @@ class Tree(app_commands.CommandTree): ) + " UTC" ) - await interaction.response.send_message(embed=embed, ephemeral=True) + 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