update aliases.py to use command specs

This commit is contained in:
jesopo 2020-01-29 17:56:42 +00:00
parent 338e1864d7
commit e6d0cba63b

View file

@ -56,10 +56,10 @@ class Module(ModuleManager.BaseModule):
permission="balias") permission="balias")
@utils.hook("received.command.calias", @utils.hook("received.command.calias",
require_mode="o", require_access="alias") require_mode="o", require_access="alias")
@utils.kwarg("min_args", 1)
@utils.kwarg("usage", "list") @utils.spec("!'list ?<alias>wordlower")
@utils.kwarg("usage", "add <alias> <command> [arg1 [arg2 ...]]") @utils.spec("!'add !<alias>wordlower !<command>wordlower ?<args>string")
@utils.kwarg("usage", "remove <alias>") @utils.spec("!'remove !<alias>wordlower")
@utils.kwarg("remove_empty", False) @utils.kwarg("remove_empty", False)
def alias(self, event): def alias(self, event):
target = event["server"] target = event["server"]
@ -71,41 +71,32 @@ class Module(ModuleManager.BaseModule):
elif event["command"] == "balias": elif event["command"] == "balias":
target = self.bot target = self.bot
subcommand = event["args_split"][0].lower() subcommand = event["spec"][0]
alias = event["spec"][1]
if subcommand == "list": if subcommand == "list":
aliases = self._get_aliases([target]) if alias:
event["stdout"].write("Available aliases: %s" % setting = target.get_setting(f"{SETTING_PREFIX}{alias}", None)
", ".join(sorted(aliases.keys())))
elif subcommand == "show": if setting == None:
if not len(event["args_split"]) > 1: raise utils.EventError("I don't have an '%s' alias" % alias)
raise utils.EventError("Please provide an alias to remove") prefix = event["command_prefix"]
event["stdout"].write(f"{prefix}{alias}: {prefix}{setting}")
alias = event["args_split"][1].lower() else:
setting = target.get_setting("%s%s" % (SETTING_PREFIX, alias), None) aliases = self._get_aliases([target])
event["stdout"].write("Available aliases: %s" %
if setting == None: ", ".join(sorted(aliases.keys())))
raise utils.EventError("I don't have an '%s' alias" % alias)
prefix = event["command_prefix"]
event["stdout"].write(f"{prefix}{alias}: {prefix}{setting}")
elif subcommand == "add": elif subcommand == "add":
if not len(event["args_split"]) > 2: command = event["spec"][2].lower()
raise utils.EventError("Please provide an alias and a command") args = event["spec"][3]
if args:
command = f"{command} {args}"
alias = event["args_split"][1].lower()
command = event["args_split"][2].lower()
command = " ".join([command]+event["args_split"][3:])
target.set_setting("%s%s" % (SETTING_PREFIX, alias), command) target.set_setting("%s%s" % (SETTING_PREFIX, alias), command)
event["stdout"].write("Added '%s' alias" % alias) event["stdout"].write("Added '%s' alias" % alias)
elif subcommand == "remove": elif subcommand == "remove":
if not len(event["args_split"]) > 1: setting = f"{SETTING_PREFIX}{alias}"
raise utils.EventError("Please provide an alias to remove")
alias = event["args_split"][1].lower()
setting = "%s%s" % (SETTING_PREFIX, alias)
if target.get_setting(setting, None) == None: if target.get_setting(setting, None) == None:
raise utils.EventError("I don't have an '%s' alias" % alias) raise utils.EventError("I don't have an '%s' alias" % alias)