diff options
author | 2022-05-29 13:02:04 +0100 | |
---|---|---|
committer | 2022-05-29 13:02:04 +0100 | |
commit | c561d3fd0dfa9c0855b41e44d4fab7160923b037 (patch) | |
tree | 21ede6c4680230e4076a97f225e7fd9ca000e508 | |
parent | Merge pull request #2180 from python-discord/Log-more-data-on-failed-clean (diff) | |
parent | Merge branch 'main' into fix-eval-backticks (diff) |
Merge pull request #2181 from python-discord/fix-eval-backticks
Add special handling for eval command followed by backticks.
-rw-r--r-- | bot/exts/backend/error_handler.py | 27 | ||||
-rw-r--r-- | tests/bot/exts/backend/test_error_handler.py | 3 |
2 files changed, 30 insertions, 0 deletions
diff --git a/bot/exts/backend/error_handler.py b/bot/exts/backend/error_handler.py index 194a4889d..761991488 100644 --- a/bot/exts/backend/error_handler.py +++ b/bot/exts/backend/error_handler.py @@ -1,3 +1,4 @@ +import copy import difflib from botcore.site_api import ResponseCodeError @@ -65,6 +66,8 @@ class ErrorHandler(Cog): if isinstance(e, errors.CommandNotFound) and not getattr(ctx, "invoked_from_error_handler", False): if await self.try_silence(ctx): return + if await self.try_run_eval(ctx): + return await self.try_get_tag(ctx) # Try to look for a tag with the command's name elif isinstance(e, errors.UserInputError): log.debug(debug_message) @@ -179,6 +182,30 @@ class ErrorHandler(Cog): if not any(role.id in MODERATION_ROLES for role in ctx.author.roles): await self.send_command_suggestion(ctx, ctx.invoked_with) + async def try_run_eval(self, ctx: Context) -> bool: + """ + Attempt to run eval command with backticks directly after command. + + For example: !eval```print("hi")``` + + Return True if command was invoked, else False + """ + msg = copy.copy(ctx.message) + + command, sep, end = msg.content.partition("```") + msg.content = command + " " + sep + end + new_ctx = await self.bot.get_context(msg) + + eval_command = self.bot.get_command("eval") + if eval_command is None or new_ctx.command != eval_command: + return False + + log.debug("Running fixed eval command.") + new_ctx.invoked_from_error_handler = True + await self.bot.invoke(new_ctx) + + return True + async def send_command_suggestion(self, ctx: Context, command_name: str) -> None: """Sends user similar commands if any can be found.""" # No similar tag found, or tag on cooldown - diff --git a/tests/bot/exts/backend/test_error_handler.py b/tests/bot/exts/backend/test_error_handler.py index d02bd7c34..0a58126e7 100644 --- a/tests/bot/exts/backend/test_error_handler.py +++ b/tests/bot/exts/backend/test_error_handler.py @@ -48,6 +48,7 @@ class ErrorHandlerTests(unittest.IsolatedAsyncioTestCase): cog = ErrorHandler(self.bot) cog.try_silence = AsyncMock() cog.try_get_tag = AsyncMock() + cog.try_run_eval = AsyncMock(return_value=False) for case in test_cases: with self.subTest(try_silence_return=case["try_silence_return"], try_get_tag=case["called_try_get_tag"]): @@ -76,6 +77,7 @@ class ErrorHandlerTests(unittest.IsolatedAsyncioTestCase): cog = ErrorHandler(self.bot) cog.try_silence = AsyncMock() cog.try_get_tag = AsyncMock() + cog.try_run_eval = AsyncMock() error = errors.CommandNotFound() @@ -83,6 +85,7 @@ class ErrorHandlerTests(unittest.IsolatedAsyncioTestCase): cog.try_silence.assert_not_awaited() cog.try_get_tag.assert_not_awaited() + cog.try_run_eval.assert_not_awaited() self.ctx.send.assert_not_awaited() async def test_error_handler_user_input_error(self): |