mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-06 03:08:55 -05:00
Fix duplicate commands in fuzzy help (#2798)
* Fix duplicate commands in fuzzy help Signed-off-by: Toby Harradine <tobyharradine@gmail.com> * Use help command's filter for all fuzzy Signed-off-by: Toby Harradine <tobyharradine@gmail.com>
This commit is contained in:
parent
461f56bca1
commit
f3bbfdc64d
@ -417,8 +417,9 @@ class RedHelpFormatter:
|
|||||||
pages = [box(p) for p in pagify(to_page)]
|
pages = [box(p) for p in pagify(to_page)]
|
||||||
await self.send_pages(ctx, pages, embed=False)
|
await self.send_pages(ctx, pages, embed=False)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
async def help_filter_func(
|
async def help_filter_func(
|
||||||
self, ctx, objects: Iterable[SupportsCanSee], bypass_hidden=False
|
ctx, objects: Iterable[SupportsCanSee], bypass_hidden=False
|
||||||
) -> AsyncIterator[SupportsCanSee]:
|
) -> AsyncIterator[SupportsCanSee]:
|
||||||
"""
|
"""
|
||||||
This does most of actual filtering.
|
This does most of actual filtering.
|
||||||
@ -450,7 +451,7 @@ class RedHelpFormatter:
|
|||||||
"""
|
"""
|
||||||
Sends an error, fuzzy help, or stays quiet based on settings
|
Sends an error, fuzzy help, or stays quiet based on settings
|
||||||
"""
|
"""
|
||||||
coms = [c async for c in self.help_filter_func(ctx, ctx.bot.walk_commands())]
|
coms = {c async for c in self.help_filter_func(ctx, ctx.bot.walk_commands())}
|
||||||
fuzzy_commands = await fuzzy_command_search(ctx, help_for, commands=coms, min_score=75)
|
fuzzy_commands = await fuzzy_command_search(ctx, help_for, commands=coms, min_score=75)
|
||||||
use_embeds = await ctx.embed_requested()
|
use_embeds = await ctx.embed_requested()
|
||||||
if fuzzy_commands:
|
if fuzzy_commands:
|
||||||
|
|||||||
@ -13,6 +13,7 @@ import pkg_resources
|
|||||||
from colorama import Fore, Style, init
|
from colorama import Fore, Style, init
|
||||||
from pkg_resources import DistributionNotFound
|
from pkg_resources import DistributionNotFound
|
||||||
|
|
||||||
|
from redbot.core.commands import RedHelpFormatter
|
||||||
from .. import __version__ as red_version, version_info as red_version_info, VersionInfo
|
from .. import __version__ as red_version, version_info as red_version_info, VersionInfo
|
||||||
from . import commands
|
from . import commands
|
||||||
from .config import get_latest_confs
|
from .config import get_latest_confs
|
||||||
@ -201,7 +202,12 @@ def init_events(bot, cli_flags):
|
|||||||
bot._last_exception = exception_log
|
bot._last_exception = exception_log
|
||||||
await ctx.send(inline(message))
|
await ctx.send(inline(message))
|
||||||
elif isinstance(error, commands.CommandNotFound):
|
elif isinstance(error, commands.CommandNotFound):
|
||||||
fuzzy_commands = await fuzzy_command_search(ctx)
|
fuzzy_commands = await fuzzy_command_search(
|
||||||
|
ctx,
|
||||||
|
commands={
|
||||||
|
c async for c in RedHelpFormatter.help_filter_func(ctx, bot.walk_commands())
|
||||||
|
},
|
||||||
|
)
|
||||||
if not fuzzy_commands:
|
if not fuzzy_commands:
|
||||||
pass
|
pass
|
||||||
elif await ctx.embed_requested():
|
elif await ctx.embed_requested():
|
||||||
|
|||||||
@ -19,14 +19,18 @@ from typing import (
|
|||||||
Tuple,
|
Tuple,
|
||||||
TypeVar,
|
TypeVar,
|
||||||
Union,
|
Union,
|
||||||
|
Set,
|
||||||
|
TYPE_CHECKING,
|
||||||
)
|
)
|
||||||
|
|
||||||
import discord
|
import discord
|
||||||
from fuzzywuzzy import fuzz, process
|
from fuzzywuzzy import fuzz, process
|
||||||
from redbot.core import commands
|
|
||||||
|
|
||||||
from .chat_formatting import box
|
from .chat_formatting import box
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from ..commands import Command, Context
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"bounded_gather",
|
"bounded_gather",
|
||||||
"safe_delete",
|
"safe_delete",
|
||||||
@ -180,12 +184,12 @@ async def async_enumerate(
|
|||||||
|
|
||||||
|
|
||||||
async def fuzzy_command_search(
|
async def fuzzy_command_search(
|
||||||
ctx: commands.Context,
|
ctx: "Context",
|
||||||
term: Optional[str] = None,
|
term: Optional[str] = None,
|
||||||
*,
|
*,
|
||||||
commands: Optional[list] = None,
|
commands: Optional[Set["Command"]] = None,
|
||||||
min_score: int = 80,
|
min_score: int = 80,
|
||||||
) -> Optional[List[commands.Command]]:
|
) -> Optional[List["Command"]]:
|
||||||
"""Search for commands which are similar in name to the one invoked.
|
"""Search for commands which are similar in name to the one invoked.
|
||||||
|
|
||||||
Returns a maximum of 5 commands which must all be at least matched
|
Returns a maximum of 5 commands which must all be at least matched
|
||||||
@ -196,8 +200,11 @@ async def fuzzy_command_search(
|
|||||||
ctx : `commands.Context <redbot.core.commands.Context>`
|
ctx : `commands.Context <redbot.core.commands.Context>`
|
||||||
The command invocation context.
|
The command invocation context.
|
||||||
term : Optional[str]
|
term : Optional[str]
|
||||||
The name of the invoked command. If ``None``, `Context.invoked_with`
|
The name of the invoked command. If ``None``,
|
||||||
will be used instead.
|
`Context.invoked_with` will be used instead.
|
||||||
|
commands : Optional[Set[commands.Command]]
|
||||||
|
The commands available to choose from when doing a fuzzy match.
|
||||||
|
When omitted, `Bot.walk_commands` will be used instead.
|
||||||
min_score : int
|
min_score : int
|
||||||
The minimum score for matched commands to reach. Defaults to 80.
|
The minimum score for matched commands to reach. Defaults to 80.
|
||||||
|
|
||||||
@ -239,7 +246,7 @@ async def fuzzy_command_search(
|
|||||||
|
|
||||||
# Do the scoring. `extracted` is a list of tuples in the form `(command, score)`
|
# Do the scoring. `extracted` is a list of tuples in the form `(command, score)`
|
||||||
extracted = process.extract(
|
extracted = process.extract(
|
||||||
term, (commands or ctx.bot.walk_commands()), limit=5, scorer=fuzz.QRatio
|
term, (commands or set(ctx.bot.walk_commands())), limit=5, scorer=fuzz.QRatio
|
||||||
)
|
)
|
||||||
if not extracted:
|
if not extracted:
|
||||||
return
|
return
|
||||||
@ -257,10 +264,7 @@ async def fuzzy_command_search(
|
|||||||
|
|
||||||
|
|
||||||
async def format_fuzzy_results(
|
async def format_fuzzy_results(
|
||||||
ctx: commands.Context,
|
ctx: "Context", matched_commands: List["Command"], *, embed: Optional[bool] = None
|
||||||
matched_commands: List[commands.Command],
|
|
||||||
*,
|
|
||||||
embed: Optional[bool] = None,
|
|
||||||
) -> Union[str, discord.Embed]:
|
) -> Union[str, discord.Embed]:
|
||||||
"""Format the result of a fuzzy command search.
|
"""Format the result of a fuzzy command search.
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user