diff options
| author | 2019-05-10 16:11:53 +0100 | |
|---|---|---|
| committer | 2019-05-10 16:11:53 +0100 | |
| commit | 81bb0242fe76ca89c5480a8f48d2786817cf86ef (patch) | |
| tree | 2a13f41219f402fa6f8c1a9e7f9c418f5db51f01 | |
| parent | Hold context in EventCallback objects, add name<->context translation in (diff) | |
| signature | ||
Change !help to be `!help <module> <command>`
| -rw-r--r-- | modules/commands/__init__.py | 64 |
1 files changed, 45 insertions, 19 deletions
diff --git a/modules/commands/__init__.py b/modules/commands/__init__.py index d738fbf3..2808d759 100644 --- a/modules/commands/__init__.py +++ b/modules/commands/__init__.py @@ -254,6 +254,15 @@ class Module(ModuleManager.BaseModule): "%s %s" % (command, usage) for usage in usages) 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): return hook.get_kwarg("prefix", None) def _get_alias_of(self, hook): @@ -266,29 +275,46 @@ class Module(ModuleManager.BaseModule): :usage: [command] """ if event["args"]: - 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() - help = self._get_help(hooks[0]) + 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 help: - event["stdout"].write("%s: %s" % (command, help)) - else: - event["stderr"].write("No help available for %s" % command) + 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: - event["stderr"].write("Unknown command '%s'" % command) - else: - help_available = [] - for child in self.events.on("received.command").get_children(): - hooks = self.events.on("received.command").on(child).get_hooks() + 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 hooks and self._get_help(hooks[0] - ) and not self._get_alias_of(hooks[0]): - help_available.append(child) + 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 - help_available = sorted(help_available) - event["stdout"].write("Commands: %s" % ", ".join(help_available)) + 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): |
