Skip to content

Commit fe6c3b2

Browse files
committed
subclass CommandTree
this subclass will be calling the error manager upon error
1 parent 7e7b3ce commit fe6c3b2

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

pydis_core/_bot.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66

77
import aiohttp
88
import discord
9+
from discord import app_commands
910
from discord.ext import commands
1011

1112
from pydis_core.async_stats import AsyncStatsClient
1213
from pydis_core.site_api import APIClient
1314
from pydis_core.utils import scheduling
1415
from pydis_core.utils._extensions import walk_extensions
16+
from pydis_core.utils.error_handling.commands import CommandErrorManager
1517
from pydis_core.utils.logging import get_logger
1618

1719
try:
@@ -32,6 +34,23 @@ def __init__(self, base: Exception):
3234
self.exception = base
3335

3436

37+
class CommandTreeBase(app_commands.CommandTree):
38+
"""A sub-class of the Command tree that implements common features that Python Discord bots use."""
39+
40+
def __init__(self, *args, **kwargs):
41+
super().__init__(*args, **kwargs)
42+
# Instance is None since discordpy only passes an instance of the client to the command tree in its constructor.
43+
self.command_error_manager: CommandErrorManager | None = None
44+
45+
async def on_error(self, interaction: discord.Interaction, error: app_commands.AppCommandError) -> None:
46+
"""A callback that is called when any command raises an :exc:`AppCommandError`."""
47+
if not self.command_error_manager:
48+
log.warning("Command error manager hasn't been loaded in the command tree.")
49+
await super().on_error(interaction, error)
50+
return
51+
await self.command_error_manager.handle_error(error, interaction)
52+
53+
3554
class BotBase(commands.Bot):
3655
"""
3756
A sub-class that implements many common features that Python Discord bots use.

0 commit comments

Comments
 (0)