diff options
author | 2024-02-18 17:45:17 +0100 | |
---|---|---|
committer | 2024-03-18 11:37:57 +0100 | |
commit | fe6c3b28e3cfa8820ec71e736e0692b5236104a5 (patch) | |
tree | c1abf8a4d6c5bf185666cc21373b332a6f6c07bb | |
parent | register all error handlers in the CommandErrorManager class (diff) |
subclass CommandTree
this subclass will be calling the error manager upon error
-rw-r--r-- | pydis_core/_bot.py | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/pydis_core/_bot.py b/pydis_core/_bot.py index 0427c659..08dca6a4 100644 --- a/pydis_core/_bot.py +++ b/pydis_core/_bot.py @@ -6,12 +6,14 @@ from contextlib import suppress import aiohttp import discord +from discord import app_commands from discord.ext import commands from pydis_core.async_stats import AsyncStatsClient from pydis_core.site_api import APIClient from pydis_core.utils import scheduling from pydis_core.utils._extensions import walk_extensions +from pydis_core.utils.error_handling.commands import CommandErrorManager from pydis_core.utils.logging import get_logger try: @@ -32,6 +34,23 @@ class StartupError(Exception): self.exception = base +class CommandTreeBase(app_commands.CommandTree): + """A sub-class of the Command tree that implements common features that Python Discord bots use.""" + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + # Instance is None since discordpy only passes an instance of the client to the command tree in its constructor. + self.command_error_manager: CommandErrorManager | None = None + + async def on_error(self, interaction: discord.Interaction, error: app_commands.AppCommandError) -> None: + """A callback that is called when any command raises an :exc:`AppCommandError`.""" + if not self.command_error_manager: + log.warning("Command error manager hasn't been loaded in the command tree.") + await super().on_error(interaction, error) + return + await self.command_error_manager.handle_error(error, interaction) + + class BotBase(commands.Bot): """ A sub-class that implements many common features that Python Discord bots use. |