66
77import aiohttp
88import discord
9+ from discord import app_commands
910from discord .ext import commands
1011
1112from pydis_core .async_stats import AsyncStatsClient
1213from pydis_core .site_api import APIClient
1314from pydis_core .utils import scheduling
1415from pydis_core .utils ._extensions import walk_extensions
16+ from pydis_core .utils .error_handling .commands import CommandErrorManager
1517from pydis_core .utils .logging import get_logger
1618
1719try :
@@ -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+
3554class BotBase (commands .Bot ):
3655 """
3756 A sub-class that implements many common features that Python Discord bots use.
0 commit comments