Skip to content

Commit 999de6b

Browse files
committed
Added on_addon_command_error and more
on_addon_command_error addon_check Command.on_error
1 parent 0c8d8aa commit 999de6b

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

dico_command/addon.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
if typing.TYPE_CHECKING:
1010
from .bot import Bot
11+
from .context import Context
1112

1213

1314
class Listener:
@@ -57,3 +58,9 @@ def on_load(self):
5758

5859
def on_unload(self):
5960
pass
61+
62+
async def addon_check(self, ctx): # noqa
63+
return True
64+
65+
async def on_addon_command_error(self, ctx, ex): # noqa
66+
return False

dico_command/bot.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ async def execute_handler(self, message: dico.Message):
6767
self.logger.debug(f"Command {name} executed.")
6868
await cmd.invoke(context, *args, **kwargs)
6969
except Exception as ex:
70-
self.handle_command_error(context, ex)
70+
await self.handle_command_error(context, ex)
7171

7272
def add_command(self, command: Command):
7373
if command.name in self.commands:
@@ -94,7 +94,11 @@ def wrap(func):
9494
return cmd
9595
return wrap
9696

97-
def handle_command_error(self, context, ex):
97+
async def handle_command_error(self, context, ex):
98+
if await context.command.execute_error_handler(context, ex):
99+
return
100+
if context.command.addon and await context.command.addon.on_addon_command_error(context, ex):
101+
return
98102
if not self.events.get("COMMAND_ERROR"):
99103
self.logger.error(f"Error while executing command '{context.command.name}':\n"+''.join(traceback.format_exception(type(ex), ex, ex.__traceback__)))
100104
else:

dico_command/command.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def __init__(self,
1515
self.checks = checks or []
1616
self.aliases = aliases or []
1717
self.subcommands = {}
18+
self.error_handler = None
1819

1920
self.args_data = read_function(self.func)
2021
if hasattr(func, "_checks"):
@@ -31,7 +32,19 @@ def wrap(coro):
3132
def register_addon(self, addon):
3233
self.addon = addon
3334

35+
async def execute_error_handler(self, ctx, ex):
36+
if not self.error_handler:
37+
return False
38+
args = (self.addon, ctx, ex) if self.addon else (ctx, ex)
39+
return await self.error_handler(*args)
40+
41+
def on_error(self, coro):
42+
self.error_handler = coro
43+
return coro
44+
3445
async def evaluate_checks(self, ctx: Context):
46+
if self.addon and not await self.addon.addon_check(ctx):
47+
return False
3548
resp = [n for n in [(await x(ctx)) if is_coro(x) else x(ctx) for x in self.checks] if not n]
3649
return not resp
3750

0 commit comments

Comments
 (0)