Update error handling (mainly CheckPlayerError)

This commit is contained in:
Parker M. 2024-07-20 18:10:37 -05:00
parent 1614fa2dfa
commit d69807b2e4
No known key found for this signature in database
GPG Key ID: 95CD2E0C7E329F2A
3 changed files with 24 additions and 13 deletions

View File

@ -5,7 +5,7 @@ import requests
import openai
import config
from tree import Tree
from utils.command_tree import Tree
class MyBot(commands.Bot):

View File

@ -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 </bug:1224840889906499626> if this issue persists.",
}
)
should_connect = interaction.command.name in ("play",)
voice_client = interaction.guild.voice_client

View File

@ -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 </bug:1224840889906499626> 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