fail an alias when an ${} var isn't found
This commit is contained in:
parent
2f73159b95
commit
5df8df83ad
2 changed files with 20 additions and 6 deletions
|
@ -3,6 +3,8 @@ from src import EventManager, ModuleManager, utils
|
||||||
|
|
||||||
SETTING_PREFIX = "command-alias-"
|
SETTING_PREFIX = "command-alias-"
|
||||||
|
|
||||||
|
class VariableKeyError(KeyError):
|
||||||
|
pass
|
||||||
class Module(ModuleManager.BaseModule):
|
class Module(ModuleManager.BaseModule):
|
||||||
def _arg_replace(self, s, args_split, kwargs):
|
def _arg_replace(self, s, args_split, kwargs):
|
||||||
vars = {}
|
vars = {}
|
||||||
|
@ -11,7 +13,11 @@ class Module(ModuleManager.BaseModule):
|
||||||
vars["%d-" % i] = " ".join(args_split[i:])
|
vars["%d-" % i] = " ".join(args_split[i:])
|
||||||
vars["-"] = " ".join(args_split)
|
vars["-"] = " ".join(args_split)
|
||||||
vars.update(kwargs)
|
vars.update(kwargs)
|
||||||
return utils.parse.format_token_replace(s, vars)
|
|
||||||
|
not_found, new_s = utils.parse.format_token_replace(s, vars)
|
||||||
|
if not_found:
|
||||||
|
raise VariableKeyError(f"not found: {not_found!r}")
|
||||||
|
return new_s
|
||||||
|
|
||||||
def _get_alias(self, server, target, command):
|
def _get_alias(self, server, target, command):
|
||||||
setting = "%s%s" % (SETTING_PREFIX, command)
|
setting = "%s%s" % (SETTING_PREFIX, command)
|
||||||
|
@ -45,9 +51,14 @@ class Module(ModuleManager.BaseModule):
|
||||||
if event["command"].args:
|
if event["command"].args:
|
||||||
given_args = event["command"].args.split(" ")
|
given_args = event["command"].args.split(" ")
|
||||||
|
|
||||||
|
try:
|
||||||
|
event["command"].args = self._arg_replace(alias_args,
|
||||||
|
given_args, event["kwargs"])
|
||||||
|
except VariableKeyError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
event["command"].command = alias
|
event["command"].command = alias
|
||||||
event["command"].args = self._arg_replace(alias_args, given_args,
|
|
||||||
event["kwargs"])
|
|
||||||
|
|
||||||
@utils.hook("received.command.alias",
|
@utils.hook("received.command.alias",
|
||||||
permission="alias")
|
permission="alias")
|
||||||
|
|
|
@ -131,7 +131,7 @@ def format_tokens(s: str, sigil: str="$"
|
||||||
return tokens
|
return tokens
|
||||||
|
|
||||||
def format_token_replace(s: str, vars: typing.Dict[str, str],
|
def format_token_replace(s: str, vars: typing.Dict[str, str],
|
||||||
sigil: str="$") -> str:
|
sigil: str="$") -> typing.Tuple[typing.List[str], str]:
|
||||||
vars = vars.copy()
|
vars = vars.copy()
|
||||||
vars.update({sigil: sigil})
|
vars.update({sigil: sigil})
|
||||||
|
|
||||||
|
@ -140,7 +140,10 @@ def format_token_replace(s: str, vars: typing.Dict[str, str],
|
||||||
tokens.sort(key=lambda x: x[0])
|
tokens.sort(key=lambda x: x[0])
|
||||||
tokens.reverse()
|
tokens.reverse()
|
||||||
|
|
||||||
|
not_found: typing.List[str] = []
|
||||||
for start, end, token in tokens:
|
for start, end, token in tokens:
|
||||||
if token in vars:
|
if token in vars:
|
||||||
s = s[:start] + vars[token] + s[end+1:]
|
s = s[:start] + vars[token] + s[end+1:]
|
||||||
return s
|
else:
|
||||||
|
not_found += token
|
||||||
|
return not_found, s
|
||||||
|
|
Loading…
Reference in a new issue