Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions tux/handlers/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -1175,8 +1175,9 @@
Returns
-------
Optional[List[str]]
A list of suggested command qualified names (e.g., ["tag create", "tag edit"])
or None if no suitable suggestions are found.
A list of suggested command names or aliases (e.g., ["tag create", "status", "ping"])
or None if no suitable suggestions are found. When an alias matches better than
the original command name, the alias is returned instead.
"""
# Suggestions require a guild context (commands vary across guilds)
# and the name the user actually typed.
Expand All @@ -1199,7 +1200,7 @@

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

# Store potential matches: {qualified_name: min_distance}
# Store potential matches: {name_to_suggest: min_distance}
command_distances: dict[str, int] = {}

# Iterate through all commands registered with the bot.
Expand All @@ -1209,6 +1210,7 @@
continue

min_dist_for_cmd = max_distance + 1
best_match_name = cmd.qualified_name

Check warning on line 1213 in tux/handlers/error.py

View check run for this annotation

Codecov / codecov/patch

tux/handlers/error.py#L1213

Added line #L1213 was not covered by tests
qualified_name = cmd.qualified_name
# Check against the command's main name and all its aliases.
names_to_check = [qualified_name, *cmd.aliases]
Expand All @@ -1217,15 +1219,17 @@
for name in names_to_check:
# Perform case-insensitive comparison.
distance = Levenshtein.distance(command_name.lower(), name.lower())
min_dist_for_cmd = min(min_dist_for_cmd, distance)
if distance < min_dist_for_cmd:
min_dist_for_cmd = distance
best_match_name = name

Check warning on line 1224 in tux/handlers/error.py

View check run for this annotation

Codecov / codecov/patch

tux/handlers/error.py#L1223-L1224

Added lines #L1223 - L1224 were not covered by tests

# If the command is close enough, store its distance.
if min_dist_for_cmd <= max_distance:
# If we found a closer match for this command (e.g., via an alias)
# than previously stored, update the distance.
current_min = command_distances.get(qualified_name, max_distance + 1)
current_min = command_distances.get(best_match_name, max_distance + 1)

Check warning on line 1230 in tux/handlers/error.py

View check run for this annotation

Codecov / codecov/patch

tux/handlers/error.py#L1230

Added line #L1230 was not covered by tests
if min_dist_for_cmd < current_min:
command_distances[qualified_name] = min_dist_for_cmd
command_distances[best_match_name] = min_dist_for_cmd

Check warning on line 1232 in tux/handlers/error.py

View check run for this annotation

Codecov / codecov/patch

tux/handlers/error.py#L1232

Added line #L1232 was not covered by tests
Comment on lines +1222 to +1232
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Storing suggestions by best_match_name may lead to duplicate suggestions for commands with multiple close aliases.

To prevent redundant suggestions, deduplicate or select a single alias per command when multiple aliases meet the threshold.

Suggested change
if distance < min_dist_for_cmd:
min_dist_for_cmd = distance
best_match_name = name
# If the command is close enough, store its distance.
if min_dist_for_cmd <= max_distance:
# If we found a closer match for this command (e.g., via an alias)
# than previously stored, update the distance.
current_min = command_distances.get(qualified_name, max_distance + 1)
current_min = command_distances.get(best_match_name, max_distance + 1)
if min_dist_for_cmd < current_min:
command_distances[qualified_name] = min_dist_for_cmd
command_distances[best_match_name] = min_dist_for_cmd
if distance < min_dist_for_cmd:
min_dist_for_cmd = distance
best_match_name = name
canonical_name = qualified_name # Track the canonical command name
# If the command is close enough, store its distance.
if min_dist_for_cmd <= max_distance:
# Deduplicate by canonical command name, only keep the closest alias
current_min = command_distances.get(canonical_name, max_distance + 1)
if min_dist_for_cmd < current_min:
command_distances[canonical_name] = min_dist_for_cmd


# If no commands were within the distance threshold.
if not command_distances:
Expand Down