From 6d034c3b42ef94e6b1a2d2487bf9596a558c614c Mon Sep 17 00:00:00 2001 From: jesopo Date: Tue, 18 Jun 2019 17:34:16 +0100 Subject: Move !help to it's own module and entirely rework its interface --- modules/help.py | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 modules/help.py (limited to 'modules/help.py') diff --git a/modules/help.py b/modules/help.py new file mode 100644 index 00000000..0800a0e1 --- /dev/null +++ b/modules/help.py @@ -0,0 +1,82 @@ +#--depends-on commands +from src import IRCBot, ModuleManager, utils + +class Module(ModuleManager.BaseModule): + def _get_help(self, hook): + return hook.get_kwarg("help", None) or hook.docstring.description + def _get_usage(self, hook, command, command_prefix=""): + command = "%s%s" % (command_prefix, command) + usage = hook.get_kwarg("usage", None) + if usage: + usages = [usage] + else: + usages = hook.docstring.var_items.get("usage", None) + + if usages: + return " | ".join( + "%s %s" % (command, usage) for usage in usages) + return usage + + def _get_hook(self, command): + hooks = self.events.on("received.command").on(command).get_hooks() + if hooks: + return hooks[0] + else: + return None + + @utils.hook("received.command.help") + def help(self, event): + if event["args"]: + command = event["args_split"][0].lower() + hook = self._get_hook(command) + + if hook == None: + raise utils.EventError("Unknown command '%s'" % command) + help = self._get_help(hook) + usage = self._get_usage(hook, command, event["command_prefix"]) + + out = help + if usage: + out += ". Usage: %s" % usage + event["stdout"].write("%s: %s" % (command, out)) + else: + event["stdout"].write("I'm %s. use '%smodules' to list modules, " + "'%scommands ' to list commands and " + "'%shelp