aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar MarkKoz <[email protected]>2020-08-13 19:00:11 -0700
committerGravatar MarkKoz <[email protected]>2020-08-14 09:43:40 -0700
commit394aca8451c930b3807ec6b944deee7546701858 (patch)
treea9659f12056afd4e7718b1b86406ef3b79b1cd4e
parentExtensions: refactor category grouping code (diff)
Extensions: support unqualified extension names
It's convenient for users to type less to specify the exception they want. Only require a qualified name if an unqualified name is ambiguous (i.e. two modules in different subpackages have identical names).
-rw-r--r--bot/cogs/utils/extensions.py21
1 files changed, 18 insertions, 3 deletions
diff --git a/bot/cogs/utils/extensions.py b/bot/cogs/utils/extensions.py
index 52b5adefc..2cde07035 100644
--- a/bot/cogs/utils/extensions.py
+++ b/bot/cogs/utils/extensions.py
@@ -68,11 +68,26 @@ class Extension(commands.Converter):
argument = argument.lower()
- if "." not in argument:
- argument = f"{cogs.__name__}.{argument}"
-
if argument in EXTENSIONS:
return argument
+ elif (qualified_arg := f"{cogs.__name__}.{argument}") in EXTENSIONS:
+ return qualified_arg
+
+ matches = []
+ for ext in EXTENSIONS:
+ name = ext.rsplit(".", maxsplit=1)[-1]
+ if argument == name:
+ matches.append(ext)
+
+ if len(matches) > 1:
+ matches.sort()
+ names = "\n".join(matches)
+ raise commands.BadArgument(
+ f":x: `{argument}` is an ambiguous extension name. "
+ f"Please use one of the following fully-qualified names.```\n{names}```"
+ )
+ elif matches:
+ return matches[0]
else:
raise commands.BadArgument(f":x: Could not find the extension `{argument}`.")