Move !help to it's own module and entirely rework its interface

This commit is contained in:
jesopo 2019-06-18 17:34:16 +01:00
parent aa4a15a6dc
commit 6d034c3b42
2 changed files with 82 additions and 83 deletions

View file

@ -335,8 +335,6 @@ class Module(ModuleManager.BaseModule):
args_split, event["tags"], hook, command_prefix="") args_split, event["tags"], hook, command_prefix="")
event["user"].buffer.skip_next() event["user"].buffer.skip_next()
def _get_help(self, hook):
return hook.get_kwarg("help", None) or hook.docstring.description
def _get_usage(self, hook, command, command_prefix=""): def _get_usage(self, hook, command, command_prefix=""):
command = "%s%s" % (command_prefix, command) command = "%s%s" % (command_prefix, command)
usage = hook.get_kwarg("usage", None) usage = hook.get_kwarg("usage", None)
@ -350,92 +348,11 @@ class Module(ModuleManager.BaseModule):
"%s %s" % (command, usage) for usage in usages) "%s %s" % (command, usage) for usage in usages)
return usage return usage
def _all_command_hooks(self):
all_hooks = {}
for child_name in self.events.on("received.command").get_children():
hooks = self.events.on("received.command").on(child_name
).get_hooks()
if hooks:
all_hooks[child_name.lower()] = hooks[0]
return all_hooks
def _get_prefix(self, hook): def _get_prefix(self, hook):
return hook.get_kwarg("prefix", None) return hook.get_kwarg("prefix", None)
def _get_alias_of(self, hook): def _get_alias_of(self, hook):
return hook.get_kwarg("alias_of", None) return hook.get_kwarg("alias_of", None)
@utils.hook("received.command.help")
def help(self, event):
"""
:help: Show help for a given command
:usage: [module [command]]
"""
if event["args"]:
module_name = event["args_split"][0]
module = self.bot.modules.from_name(module_name)
if module == None:
raise utils.EventError("No such module '%s'" % module_name)
if len(event["args_split"]) == 1:
commands = []
for command, command_hook in self._all_command_hooks().items():
if (command_hook.context == module.context and
not self._get_alias_of(command_hook)):
commands.append(command)
event["stdout"].write("Commands for %s module: %s" % (
module.name, ", ".join(commands)))
else:
requested_command = event["args_split"][1].lower()
available_commands = self._all_command_hooks()
if requested_command in available_commands:
command_hook = available_commands[requested_command]
help = self._get_help(command_hook)
if help:
event["stdout"].write("%s: %s" % (
requested_command, help))
else:
event["stderr"].write("No help available for %s" %
requested_command)
else:
event["stderr"].write("Unknown command '%s'" %
requested_command)
else:
contexts = {}
for command, command_hook in self._all_command_hooks().items():
if not command_hook.context in contexts:
module = self.bot.modules.from_context(command_hook.context)
contexts[module.context] = module.name
modules_available = sorted(contexts.values())
event["stdout"].write("Modules: %s" % ", ".join(modules_available))
@utils.hook("received.command.usage", min_args=1)
def usage(self, event):
"""
:help: Show the usage for a given command
:usage: <command>
"""
command_prefix = ""
if event["is_channel"]:
command_prefix = self._command_prefix(event["server"],
event["target"])
command = event["args_split"][0].lower()
if command in self.events.on("received").on(
"command").get_children():
hooks = self.events.on("received.command").on(command).get_hooks()
usage = self._get_usage(hooks[0], command, command_prefix)
if usage:
event["stdout"].write("Usage: %s" % usage)
else:
event["stderr"].write("No usage help available for %s" % command)
else:
event["stderr"].write("Unknown command '%s'" % command)
@utils.hook("received.command.more", skip_out=True) @utils.hook("received.command.more", skip_out=True)
def more(self, event): def more(self, event):
""" """

82
modules/help.py Normal file
View file

@ -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 <module>' to list commands and "
"'%shelp <command' to see help text for a command" %
(IRCBot.URL, event["command_prefix"], event["command_prefix"],
event["command_prefix"]))
def _all_command_hooks(self):
all_hooks = {}
for child_name in self.events.on("received.command").get_children():
hooks = self.events.on("received.command").on(child_name
).get_hooks()
if hooks:
all_hooks[child_name.lower()] = hooks[0]
return all_hooks
@utils.hook("received.command.modules")
def modules(self, event):
contexts = {}
for command, command_hook in self._all_command_hooks().items():
if not command_hook.context in contexts:
module = self.bot.modules.from_context(command_hook.context)
contexts[module.context] = module.name
modules_available = sorted(contexts.values())
event["stdout"].write("Modules: %s" % ", ".join(modules_available))
@utils.hook("received.command.commands", min_args=1)
def commands(self, event):
module_name = event["args_split"][0]
module = self.bot.modules.from_name(module_name)
if module == None:
raise utils.EventError("No such module '%s'" % module_name)
commands = []
for command, command_hook in self._all_command_hooks().items():
if command_hook.context == module.context:
commands.append(command)
event["stdout"].write("Commands for %s module: %s" % (
module.name, ", ".join(commands)))