From 4cb8b0f1a6dd5da67414cce50d4ef80f061c9d6f Mon Sep 17 00:00:00 2001 From: nemunaire Date: Wed, 23 Sep 2015 07:50:16 +0200 Subject: [PATCH] Improve help On hook declaration, we can now add a help and/or a help_usage argument to provide a simple way to the user to be informed. For example: ```python @hook("cmd_hook", "news", help_usage={"URL": "Display the latests news from a given URL"}) def cmd_news(msg): [...] ``` will be displayed on !help !news as: > Usage for command !news from module news: !news URL: Display the latests news from a given URL Or for module commands help: ```python @hook("cmd_hook", "news", help="display latests news") def cmd_news(msg): [...] ``` will be displayed on !help mymodule (assuming this hook is in the module named mymodule) as: > Available commands for module news: news: display latests news Obviously, both `help` and `help_usage` can be present. If `help_usage` doesn't exist, help on usage will display the content of help. --- nemubot/bot.py | 23 +++++++++++++++-------- nemubot/hooks/message.py | 13 ++++++++++++- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/nemubot/bot.py b/nemubot/bot.py index 94425c4..15abba4 100644 --- a/nemubot/bot.py +++ b/nemubot/bot.py @@ -84,21 +84,28 @@ class Bot(threading.Thread): res = Response(channel=msg.to_response) if len(msg.args) >= 1: if msg.args[0] in self.modules: - if len(msg.args) >= 2: - if hasattr(self.modules[msg.args[0]], "HELP_cmd"): - return self.modules[msg.args[0]].HELP_cmd(msg.args[1]) - else: - res.append_message("No help for command %s in module %s" % (msg.args[1], msg.args[0])) - elif hasattr(self.modules[msg.args[0]], "help_full"): + if hasattr(self.modules[msg.args[0]], "help_full"): hlp = self.modules[msg.args[0]].help_full() if isinstance(hlp, Response): return hlp else: res.append_message(hlp) else: - res.append_message("No help for module %s" % msg.args[0]) + res.append_message([str(h) for s,h in self.modules[msg.args[0]].__nemubot_context__.hooks], title="Available commands for module " + msg.args[0]) + elif msg.args[0][0] == "!": + for module in self.modules: + for (s, h) in self.modules[module].__nemubot_context__.hooks: + if s == "in_Command" and h.is_matching(msg.args[0][1:]): + if h.help_usage: + return res.append_message(["\x03\x02%s%s\x03\x02: %s" % (msg.args[0], " " + k if k is not None else "", h.help_usage[k]) for k in h.help_usage], title="Usage for command %s from module %s" % (msg.args[0], module)) + elif h.help: + return res.append_message("Command %s from module %s: %s" % (msg.args[0], module, h.help)) + else: + return res.append_message("Sorry, there is currently no help for the command %s. Feel free to make a pull request at https://github.com/nemunaire/nemubot/compare" % msg.args[0]) + else: + res.append_message("Sorry, there is no command %s" % msg.args[0]) else: - res.append_message("No module named %s" % msg.args[0]) + res.append_message("Sorry, there is no module named %s" % msg.args[0]) else: res.append_message("Pour me demander quelque chose, commencez " "votre message par mon nom ; je réagis " diff --git a/nemubot/hooks/message.py b/nemubot/hooks/message.py index 52d9138..5f092ad 100644 --- a/nemubot/hooks/message.py +++ b/nemubot/hooks/message.py @@ -25,18 +25,29 @@ class Message(Abstract): """Class storing hook information, specialized for a generic Message""" def __init__(self, call, name=None, regexp=None, channels=list(), - server=None, **kargs): + server=None, help=None, help_usage=dict(), **kargs): Abstract.__init__(self, call=call, **kargs) assert regexp is None or type(regexp) is str, regexp assert channels is None or type(channels) is list, channels assert server is None or type(server) is str, server + assert type(help_usage) is dict, help_usage self.name = str(name) if name is not None else None self.regexp = regexp self.server = server self.channels = channels + self.help = help + self.help_usage = help_usage + + + def __str__(self): + return "\x03\x02%s\x03\x02%s%s" % ( + self.name if self.name is not None else "\x03\x1f" + self.regexp + "\x03\x1f" if self.regexp is not None else "", + " (restricted to %s)" % (self.server + ":" if self.server is not None else "") + (self.channels if self.channels else "*") if len(self.channels) or self.server else "", + ": %s" % self.help if self.help is not None else "" + ) def match(self, msg, server=None):