Skip to content

Commit 85084b5

Browse files
authored
Merge pull request #999 from allthingslinux/command-suggestion-aliases
refactor(error): improve command suggestion logic to return better matches using aliases
2 parents 1c151c4 + 354a24f commit 85084b5

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

tux/handlers/error.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,8 +1175,9 @@ async def _suggest_command(self, ctx: commands.Context[Tux]) -> list[str] | None
11751175
Returns
11761176
-------
11771177
Optional[List[str]]
1178-
A list of suggested command qualified names (e.g., ["tag create", "tag edit"])
1179-
or None if no suitable suggestions are found.
1178+
A list of suggested command names or aliases (e.g., ["tag create", "status", "ping"])
1179+
or None if no suitable suggestions are found. When an alias matches better than
1180+
the original command name, the alias is returned instead.
11801181
"""
11811182
# Suggestions require a guild context (commands vary across guilds)
11821183
# and the name the user actually typed.
@@ -1199,7 +1200,7 @@ async def _suggest_command(self, ctx: commands.Context[Tux]) -> list[str] | None
11991200

12001201
logger.bind(**log_context).debug("Attempting command suggestion.")
12011202

1202-
# Store potential matches: {qualified_name: min_distance}
1203+
# Store potential matches: {name_to_suggest: min_distance}
12031204
command_distances: dict[str, int] = {}
12041205

12051206
# Iterate through all commands registered with the bot.
@@ -1209,6 +1210,7 @@ async def _suggest_command(self, ctx: commands.Context[Tux]) -> list[str] | None
12091210
continue
12101211

12111212
min_dist_for_cmd = max_distance + 1
1213+
best_match_name = cmd.qualified_name
12121214
qualified_name = cmd.qualified_name
12131215
# Check against the command's main name and all its aliases.
12141216
names_to_check = [qualified_name, *cmd.aliases]
@@ -1217,15 +1219,17 @@ async def _suggest_command(self, ctx: commands.Context[Tux]) -> list[str] | None
12171219
for name in names_to_check:
12181220
# Perform case-insensitive comparison.
12191221
distance = Levenshtein.distance(command_name.lower(), name.lower())
1220-
min_dist_for_cmd = min(min_dist_for_cmd, distance)
1222+
if distance < min_dist_for_cmd:
1223+
min_dist_for_cmd = distance
1224+
best_match_name = name
12211225

12221226
# If the command is close enough, store its distance.
12231227
if min_dist_for_cmd <= max_distance:
12241228
# If we found a closer match for this command (e.g., via an alias)
12251229
# than previously stored, update the distance.
1226-
current_min = command_distances.get(qualified_name, max_distance + 1)
1230+
current_min = command_distances.get(best_match_name, max_distance + 1)
12271231
if min_dist_for_cmd < current_min:
1228-
command_distances[qualified_name] = min_dist_for_cmd
1232+
command_distances[best_match_name] = min_dist_for_cmd
12291233

12301234
# If no commands were within the distance threshold.
12311235
if not command_distances:

0 commit comments

Comments
 (0)