6
6
7
7
import aiohttp
8
8
import discord
9
+ from discord import app_commands
9
10
from discord .ext import commands
10
11
11
12
from pydis_core .async_stats import AsyncStatsClient
12
13
from pydis_core .site_api import APIClient
13
14
from pydis_core .utils import scheduling
14
15
from pydis_core .utils ._extensions import walk_extensions
16
+ from pydis_core .utils .error_handling .commands import CommandErrorManager
15
17
from pydis_core .utils .logging import get_logger
16
18
17
19
try :
@@ -32,6 +34,23 @@ def __init__(self, base: Exception):
32
34
self .exception = base
33
35
34
36
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
+
35
54
class BotBase (commands .Bot ):
36
55
"""
37
56
A sub-class that implements many common features that Python Discord bots use.
0 commit comments