support multiple specs per command
This commit is contained in:
parent
41e1115999
commit
2ae47364c0
1 changed files with 35 additions and 31 deletions
|
@ -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()
|
||||||
|
|
||||||
out = []
|
first_error = None
|
||||||
kwargs = {"channel": channel}
|
for spec_arguments in specs:
|
||||||
|
out = []
|
||||||
|
kwargs = {"channel": channel}
|
||||||
|
failed = False
|
||||||
|
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,
|
||||||
|
spec_argument.types, args)
|
||||||
|
|
||||||
for spec_argument in spec_arguments:
|
found = None
|
||||||
options = self._spec_value(server, kwargs["channel"], user,
|
for argument_type, value, n, error in options:
|
||||||
spec_argument.types, args)
|
if not value == None:
|
||||||
|
if argument_type.exported:
|
||||||
|
kwargs[argument_type.exported] = value
|
||||||
|
|
||||||
found = None
|
args = args[n:]
|
||||||
first_error = None
|
if argument_type_multi:
|
||||||
for argument_type, value, n, error in options:
|
value = [argument_type.type, value]
|
||||||
if not value == None:
|
found = value
|
||||||
if argument_type.exported:
|
break
|
||||||
kwargs[argument_type.exported] = value
|
elif not error and n > len(args):
|
||||||
|
error = "Not enough arguments"
|
||||||
|
|
||||||
found = True
|
first_error = first_error or error
|
||||||
args = args[n:]
|
if not found == None:
|
||||||
if len(spec_argument.types) > 1:
|
out.append(found)
|
||||||
value = [argument_type.type, value]
|
elif not spec_argument.optional:
|
||||||
found = value
|
failed = True
|
||||||
break
|
break
|
||||||
elif not error and n > len(args):
|
if not failed:
|
||||||
error = "Not enough arguments"
|
kwargs["spec"] = out
|
||||||
|
event["kwargs"].update(kwargs)
|
||||||
|
return
|
||||||
|
|
||||||
if error and not first_error:
|
error = first_error or "Invalid arguments"
|
||||||
first_error = error
|
return utils.consts.PERMISSION_HARD_FAIL, error
|
||||||
|
|
||||||
if not spec_argument.optional and not found:
|
|
||||||
error = first_error or "Invalid arguments"
|
|
||||||
return utils.consts.PERMISSION_HARD_FAIL, error
|
|
||||||
|
|
||||||
out.append(found)
|
|
||||||
|
|
||||||
kwargs["spec"] = out
|
|
||||||
event["kwargs"].update(kwargs)
|
|
||||||
|
|
Loading…
Reference in a new issue