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.
This commit is contained in:
nemunaire 2015-09-23 07:50:16 +02:00
parent 4f7d89a3a1
commit 4cb8b0f1a6
2 changed files with 27 additions and 9 deletions

View File

@ -84,21 +84,28 @@ class Bot(threading.Thread):
res = Response(channel=msg.to_response) res = Response(channel=msg.to_response)
if len(msg.args) >= 1: if len(msg.args) >= 1:
if msg.args[0] in self.modules: if msg.args[0] in self.modules:
if len(msg.args) >= 2: if hasattr(self.modules[msg.args[0]], "help_full"):
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"):
hlp = self.modules[msg.args[0]].help_full() hlp = self.modules[msg.args[0]].help_full()
if isinstance(hlp, Response): if isinstance(hlp, Response):
return hlp return hlp
else: else:
res.append_message(hlp) res.append_message(hlp)
else: 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: 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: else:
res.append_message("Pour me demander quelque chose, commencez " res.append_message("Pour me demander quelque chose, commencez "
"votre message par mon nom ; je réagis " "votre message par mon nom ; je réagis "

View File

@ -25,18 +25,29 @@ class Message(Abstract):
"""Class storing hook information, specialized for a generic Message""" """Class storing hook information, specialized for a generic Message"""
def __init__(self, call, name=None, regexp=None, channels=list(), 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) Abstract.__init__(self, call=call, **kargs)
assert regexp is None or type(regexp) is str, regexp assert regexp is None or type(regexp) is str, regexp
assert channels is None or type(channels) is list, channels assert channels is None or type(channels) is list, channels
assert server is None or type(server) is str, server 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.name = str(name) if name is not None else None
self.regexp = regexp self.regexp = regexp
self.server = server self.server = server
self.channels = channels 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): def match(self, msg, server=None):