mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-21 02:16:09 -05:00
[V3] Start work on fuzzy command search (#1600)
* [V3] Start work on fuzzy command search * Implement in command error handler * Something isn't working here, try fixing * Style compliance * Add fuzzywuzzy to pipfile * Dump the short doc part if there is no short doc * Add fuzzy command search on command not found in help * Move things around, implement for use of default d.py help formatter * Formatting compliance * Undo pipfile changes
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
__all__ = ["TYPE_CHECKING", "NewType", "safe_delete"]
|
||||
__all__ = ["TYPE_CHECKING", "NewType", "safe_delete", "fuzzy_command_search"]
|
||||
|
||||
from pathlib import Path
|
||||
import os
|
||||
import shutil
|
||||
from redbot.core import commands
|
||||
from fuzzywuzzy import process
|
||||
from .chat_formatting import box
|
||||
|
||||
try:
|
||||
from typing import TYPE_CHECKING
|
||||
@@ -26,3 +29,15 @@ def safe_delete(pth: Path):
|
||||
for f in files:
|
||||
os.chmod(os.path.join(root, f), 0o755)
|
||||
shutil.rmtree(str(pth), ignore_errors=True)
|
||||
|
||||
|
||||
def fuzzy_command_search(ctx: commands.Context, term: str):
|
||||
out = ""
|
||||
for pos, extracted in enumerate(process.extract(term, ctx.bot.walk_commands(), limit=5), 1):
|
||||
out += "{0}. {1.prefix}{2.qualified_name}{3}\n".format(
|
||||
pos,
|
||||
ctx,
|
||||
extracted[0],
|
||||
" - {}".format(extracted[0].short_doc) if extracted[0].short_doc else "",
|
||||
)
|
||||
return box(out, lang="Perhaps you wanted one of these?")
|
||||
|
||||
Reference in New Issue
Block a user