From f6215bb61fed2e1247237752414e38f539d54015 Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Tue, 23 Mar 2021 06:12:34 +0530 Subject: Feature: suggest command usage for misspell commands --- bot/exts/evergreen/error_handler.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'bot/exts/evergreen/error_handler.py') diff --git a/bot/exts/evergreen/error_handler.py b/bot/exts/evergreen/error_handler.py index 28902503..d5069168 100644 --- a/bot/exts/evergreen/error_handler.py +++ b/bot/exts/evergreen/error_handler.py @@ -1,3 +1,4 @@ +import difflib import logging import math import random @@ -5,6 +6,7 @@ from typing import Iterable, Union from discord import Embed, Message from discord.ext import commands +from discord.ext.commands import errors from sentry_sdk import push_scope from bot.constants import Channels, Colours, ERROR_REPLIES, NEGATIVE_REPLIES @@ -14,6 +16,9 @@ from bot.utils.exceptions import UserNotPlayingError log = logging.getLogger(__name__) +QUESTION_MARK_ICON = "https://cdn.discordapp.com/emojis/512367613339369475.png" + + class CommandErrorHandler(commands.Cog): """A error handler for the PythonDiscord server.""" @@ -55,6 +60,7 @@ class CommandErrorHandler(commands.Cog): ) if isinstance(error, commands.CommandNotFound): + await self.send_command_suggestion(ctx, ctx.invoked_with) return if isinstance(error, (InChannelCheckFailure, InMonthCheckFailure)): @@ -128,6 +134,35 @@ class CommandErrorHandler(commands.Cog): log.exception(f"Unhandled command error: {str(error)}", exc_info=error) + async def send_command_suggestion(self, ctx: commands.Context, command_name: str) -> None: + """Sends user similar commands if any can be found.""" + raw_commands = [] + for cmd in self.bot.walk_commands(): + if not cmd.hidden: + raw_commands += (cmd.name, *cmd.aliases) + if similar_command_data := difflib.get_close_matches(command_name, raw_commands, 1): + similar_command_name = similar_command_data[0] + similar_command = self.bot.get_command(similar_command_name) + + if not similar_command: + return + + log_msg = "Cancelling attempt to suggest a command due to failed checks." + try: + if not await similar_command.can_run(ctx): + log.debug(log_msg) + return + except errors.CommandError as cmd_error: + log.debug(log_msg) + await self.on_command_error(ctx, cmd_error) + return + + misspelled_content = ctx.message.content + e = Embed() + e.set_author(name="Did you mean:", icon_url=QUESTION_MARK_ICON) + e.description = f"{misspelled_content.replace(command_name, similar_command_name, 1)}" + await ctx.send(embed=e, delete_after=10.0) + def setup(bot: commands.Bot) -> None: """Error handler Cog load.""" -- cgit v1.2.3 From 9473521a9e8457882e135e278996a3294e53cd36 Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Tue, 23 Mar 2021 06:14:04 +0530 Subject: Fix imports --- bot/exts/evergreen/error_handler.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'bot/exts/evergreen/error_handler.py') diff --git a/bot/exts/evergreen/error_handler.py b/bot/exts/evergreen/error_handler.py index d5069168..3d056f81 100644 --- a/bot/exts/evergreen/error_handler.py +++ b/bot/exts/evergreen/error_handler.py @@ -6,7 +6,6 @@ from typing import Iterable, Union from discord import Embed, Message from discord.ext import commands -from discord.ext.commands import errors from sentry_sdk import push_scope from bot.constants import Channels, Colours, ERROR_REPLIES, NEGATIVE_REPLIES @@ -152,7 +151,7 @@ class CommandErrorHandler(commands.Cog): if not await similar_command.can_run(ctx): log.debug(log_msg) return - except errors.CommandError as cmd_error: + except commands.errors.CommandError as cmd_error: log.debug(log_msg) await self.on_command_error(ctx, cmd_error) return -- cgit v1.2.3 From 09eeac31df1fd9138d81d057829dd20cb8504ef9 Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Sat, 3 Apr 2021 11:18:38 +0530 Subject: Use constants for delete delay and remove redundant f-string. --- bot/constants.py | 5 +++++ bot/exts/evergreen/error_handler.py | 6 +++--- 2 files changed, 8 insertions(+), 3 deletions(-) (limited to 'bot/exts/evergreen/error_handler.py') diff --git a/bot/constants.py b/bot/constants.py index 416dd0e7..ba4345bc 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -20,6 +20,7 @@ __all__ = ( "Tokens", "Wolfram", "RedisConfig", + "RedirectOutput", "MODERATION_ROLES", "STAFF_ROLES", "WHITELISTED_CHANNELS", @@ -270,6 +271,10 @@ class Source: github_avatar_url = "https://avatars1.githubusercontent.com/u/9919" +class RedirectOutput: + delete_delay: int = 10 + + # Default role combinations MODERATION_ROLES = Roles.moderator, Roles.admin, Roles.owner STAFF_ROLES = Roles.helpers, Roles.moderator, Roles.admin, Roles.owner diff --git a/bot/exts/evergreen/error_handler.py b/bot/exts/evergreen/error_handler.py index 3d056f81..f74218b7 100644 --- a/bot/exts/evergreen/error_handler.py +++ b/bot/exts/evergreen/error_handler.py @@ -8,7 +8,7 @@ from discord import Embed, Message from discord.ext import commands from sentry_sdk import push_scope -from bot.constants import Channels, Colours, ERROR_REPLIES, NEGATIVE_REPLIES +from bot.constants import Channels, Colours, ERROR_REPLIES, NEGATIVE_REPLIES, RedirectOutput from bot.utils.decorators import InChannelCheckFailure, InMonthCheckFailure from bot.utils.exceptions import UserNotPlayingError @@ -159,8 +159,8 @@ class CommandErrorHandler(commands.Cog): misspelled_content = ctx.message.content e = Embed() e.set_author(name="Did you mean:", icon_url=QUESTION_MARK_ICON) - e.description = f"{misspelled_content.replace(command_name, similar_command_name, 1)}" - await ctx.send(embed=e, delete_after=10.0) + e.description = misspelled_content.replace(command_name, similar_command_name, 1) + await ctx.send(embed=e, delete_after=RedirectOutput.delete_delay) def setup(bot: commands.Bot) -> None: -- cgit v1.2.3 From 03a2c3c737795863f70572d080ff5eed15137cce Mon Sep 17 00:00:00 2001 From: Shivansh Date: Thu, 20 May 2021 18:47:27 +0530 Subject: (hotfix): Add __init__ to error handler cog --- bot/exts/evergreen/error_handler.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'bot/exts/evergreen/error_handler.py') diff --git a/bot/exts/evergreen/error_handler.py b/bot/exts/evergreen/error_handler.py index cd53f932..5873fb83 100644 --- a/bot/exts/evergreen/error_handler.py +++ b/bot/exts/evergreen/error_handler.py @@ -22,6 +22,9 @@ QUESTION_MARK_ICON = "https://cdn.discordapp.com/emojis/512367613339369475.png" class CommandErrorHandler(commands.Cog): """A error handler for the PythonDiscord server.""" + def __init__(self, bot: Bot) -> None: + self.bot = bot + @staticmethod def revert_cooldown_counter(command: commands.Command, message: Message) -> None: """Undoes the last cooldown counter for user-error cases.""" @@ -166,4 +169,4 @@ class CommandErrorHandler(commands.Cog): def setup(bot: Bot) -> None: """Load the ErrorHandler cog.""" - bot.add_cog(CommandErrorHandler()) + bot.add_cog(CommandErrorHandler(bot)) -- cgit v1.2.3