From 77d30c6a82df5ba7dbf5ceea663dc810fa960f0f Mon Sep 17 00:00:00 2001 From: gammafn Date: Mon, 29 Mar 2021 15:46:59 -0500 Subject: [PATCH] Add !help [modulename] --- modules/common/module.py | 13 ++++++++++++- modules/help.py | 32 +++++++++++++++++++++++--------- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/modules/common/module.py b/modules/common/module.py index 79a4342..4aafeb3 100644 --- a/modules/common/module.py +++ b/modules/common/module.py @@ -75,7 +75,18 @@ class BotModule(ABC): @abstractmethod def help(self): """Return one-liner help text""" - pass + return 'A cool hemppa module' + + def long_help(self, bot=None, room=None, event=None, args=[]): + """Return longer help text + + Used by help module as !help modulename [args ...] + bot, room, and event are passed to allow a module + to give contextual help + + If not defined, falls back to short help + """ + return self.help() def get_settings(self): """Must return a dict object that can be converted to JSON and sent to server diff --git a/modules/help.py b/modules/help.py index b87af0d..efabb13 100644 --- a/modules/help.py +++ b/modules/help.py @@ -4,16 +4,30 @@ from modules.common.module import BotModule class MatrixModule(BotModule): async def matrix_message(self, bot, room, event): - msg = f'This is Hemppa {bot.version}, a generic Matrix bot. Known commands:\n\n' - for modulename, moduleobject in bot.modules.items(): - if moduleobject.enabled: - msg = msg + '!' + modulename - try: - msg = msg + ' - ' + moduleobject.help() + '\n' - except AttributeError: - pass - msg = msg + "\nMore information at https://github.com/vranki/hemppa" + args = event.body.split()[1:] + if len(args) > 0: + msg = '' + modulename = args.pop(0) + moduleobject = bot.modules.get(modulename) + if not moduleobject.enabled: + msg += f'{modulename} is disabled\n' + try: + msg += moduleobject.long_help(bot=bot, room=room, event=event, args=args) + except AttributeError: + msg += f'{modulename} has no help' + + else: + msg = f'This is Hemppa {bot.version}, a generic Matrix bot. Known commands:\n\n' + + for modulename, moduleobject in bot.modules.items(): + if moduleobject.enabled: + msg = msg + '!' + modulename + try: + msg = msg + ' - ' + moduleobject.help() + '\n' + except AttributeError: + pass + msg = msg + "\nMore information at https://github.com/vranki/hemppa" await bot.send_text(room, msg) def help(self):