From 1a3abc22f8f4ce0b985e9db4c301009253460642 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABlle=20Nicolas-Robardet?= Date: Wed, 2 Feb 2022 20:03:39 +0100 Subject: [PATCH] Add a reply on failed commands --- maubot/handlers/command.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/maubot/handlers/command.py b/maubot/handlers/command.py index 37f8174..4789fd7 100644 --- a/maubot/handlers/command.py +++ b/maubot/handlers/command.py @@ -26,6 +26,15 @@ from mautrix.types import MessageType, EventType from ..matrix import MaubotMessageEvent from . import event + +class CommandError(Exception): + pass + + +class CommandFailure(CommandError): + pass + + PrefixType = Optional[Union[str, Callable[[], str], Callable[[Any], str]]] AliasesType = Union[List[str], Tuple[str, ...], Set[str], Callable[[str], bool], Callable[[Any, str], bool]] @@ -116,9 +125,18 @@ class CommandHandler: await evt.reply(self.__mb_full_help__) return - if self.__bound_instance__: - return await self.__mb_func__(self.__bound_instance__, evt, **call_args) - return await self.__mb_func__(evt, **call_args) + try: + if self.__bound_instance__: + return await self.__mb_func__(self.__bound_instance__, evt, **call_args) + return await self.__mb_func__(evt, **call_args) + except CommandFailure as e: + await evt.reply(f"Error: {e}") + except CommandError as e: + await evt.reply(f"Error: {e}") + raise e + except Exception as e: + await evt.reply("An error happened while running the command") + raise e async def __call_subcommand__(self, evt: MaubotMessageEvent, call_args: Dict[str, Any], remaining_val: str) -> Tuple[bool, Any]: