support multiple specs per command

This commit is contained in:
jesopo 2020-01-26 02:41:24 +00:00
parent 41e1115999
commit 2ae47364c0

View file

@ -90,44 +90,48 @@ class Module(ModuleManager.BaseModule):
@utils.hook("preprocess.command") @utils.hook("preprocess.command")
@utils.kwarg("priority", EventManager.PRIORITY_HIGH) @utils.kwarg("priority", EventManager.PRIORITY_HIGH)
def preprocess(self, event): def preprocess(self, event):
spec_arguments = event["hook"].get_kwarg("spec", None) specs = event["hook"].get_kwargs("spec")
if not spec_arguments == None: if specs:
server = event["server"] server = event["server"]
channel = event["target"] if event["is_channel"] else None channel = event["target"] if event["is_channel"] else None
user = event["user"] user = event["user"]
args = event["args_split"].copy() args = event["args_split"].copy()
first_error = None
for spec_arguments in specs:
out = [] out = []
kwargs = {"channel": channel} kwargs = {"channel": channel}
failed = False
for spec_argument in spec_arguments: for spec_argument in spec_arguments:
argument_type_multi = len(set(
t.type for t in spec_argument.types)) > 1
options = self._spec_value(server, kwargs["channel"], user, options = self._spec_value(server, kwargs["channel"], user,
spec_argument.types, args) spec_argument.types, args)
found = None found = None
first_error = None
for argument_type, value, n, error in options: for argument_type, value, n, error in options:
if not value == None: if not value == None:
if argument_type.exported: if argument_type.exported:
kwargs[argument_type.exported] = value kwargs[argument_type.exported] = value
found = True
args = args[n:] args = args[n:]
if len(spec_argument.types) > 1: if argument_type_multi:
value = [argument_type.type, value] value = [argument_type.type, value]
found = value found = value
break break
elif not error and n > len(args): elif not error and n > len(args):
error = "Not enough arguments" error = "Not enough arguments"
if error and not first_error: first_error = first_error or error
first_error = error if not found == None:
if not spec_argument.optional and not found:
error = first_error or "Invalid arguments"
return utils.consts.PERMISSION_HARD_FAIL, error
out.append(found) out.append(found)
elif not spec_argument.optional:
failed = True
break
if not failed:
kwargs["spec"] = out kwargs["spec"] = out
event["kwargs"].update(kwargs) event["kwargs"].update(kwargs)
return
error = first_error or "Invalid arguments"
return utils.consts.PERMISSION_HARD_FAIL, error