aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/core_modules/aliases.py19
-rw-r--r--src/utils/parse/__init__.py7
2 files changed, 20 insertions, 6 deletions
diff --git a/src/core_modules/aliases.py b/src/core_modules/aliases.py
index b6ca8961..b5ed86f1 100644
--- a/src/core_modules/aliases.py
+++ b/src/core_modules/aliases.py
@@ -3,6 +3,8 @@ from src import EventManager, ModuleManager, utils
SETTING_PREFIX = "command-alias-"
+class VariableKeyError(KeyError):
+ pass
class Module(ModuleManager.BaseModule):
def _arg_replace(self, s, args_split, kwargs):
vars = {}
@@ -11,7 +13,11 @@ class Module(ModuleManager.BaseModule):
vars["%d-" % i] = " ".join(args_split[i:])
vars["-"] = " ".join(args_split)
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):
setting = "%s%s" % (SETTING_PREFIX, command)
@@ -45,9 +51,14 @@ class Module(ModuleManager.BaseModule):
if event["command"].args:
given_args = event["command"].args.split(" ")
- event["command"].command = alias
- event["command"].args = self._arg_replace(alias_args, given_args,
- event["kwargs"])
+ try:
+ event["command"].args = self._arg_replace(alias_args,
+ given_args, event["kwargs"])
+ except VariableKeyError:
+ pass
+ else:
+ event["command"].command = alias
+
@utils.hook("received.command.alias",
permission="alias")
diff --git a/src/utils/parse/__init__.py b/src/utils/parse/__init__.py
index 262edf4a..36da8ffb 100644
--- a/src/utils/parse/__init__.py
+++ b/src/utils/parse/__init__.py
@@ -131,7 +131,7 @@ def format_tokens(s: str, sigil: str="$"
return tokens
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.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.reverse()
+ not_found: typing.List[str] = []
for start, end, token in tokens:
if token in vars:
s = s[:start] + vars[token] + s[end+1:]
- return s
+ else:
+ not_found += token
+ return not_found, s