diff options
author | 2023-02-27 19:26:43 +0000 | |
---|---|---|
committer | 2023-02-27 19:26:43 +0000 | |
commit | df1768617f185097ebf8f0b002c37db96c53af0a (patch) | |
tree | e221d8d62cb8e98d8de029ffd8eff6b4eccb4c1f | |
parent | Implement shtlrs reviews (diff) | |
parent | Merge pull request #2417 from python-discord/swfarnsworth-code-command-paste (diff) |
Merge branch 'main' into migration/tag
-rw-r--r-- | bot/exts/backend/error_handler.py | 19 | ||||
-rw-r--r-- | bot/resources/tags/codeblock.md | 2 | ||||
-rw-r--r-- | tests/bot/exts/backend/test_error_handler.py | 6 |
3 files changed, 18 insertions, 9 deletions
diff --git a/bot/exts/backend/error_handler.py b/bot/exts/backend/error_handler.py index e274e337a..8883f7566 100644 --- a/bot/exts/backend/error_handler.py +++ b/bot/exts/backend/error_handler.py @@ -66,7 +66,7 @@ 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): + if await self.try_run_fixed_codeblock(ctx): return await self.try_get_tag(ctx) # Try to look for a tag with the command's name elif isinstance(e, errors.UserInputError): @@ -195,9 +195,9 @@ class ErrorHandler(Cog): else: await self.on_command_error(ctx, errors.CommandInvokeError(err)) - async def try_run_eval(self, ctx: Context) -> bool: + async def try_run_fixed_codeblock(self, ctx: Context) -> bool: """ - Attempt to run eval command with backticks directly after command. + Attempt to run eval or timeit command with triple backticks directly after command. For example: !eval```print("hi")``` @@ -209,11 +209,18 @@ class ErrorHandler(Cog): 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: + if new_ctx.command is None: return False - log.debug("Running fixed eval command.") + allowed_commands = [ + self.bot.get_command("eval"), + self.bot.get_command("timeit"), + ] + + if new_ctx.command not in allowed_commands: + return False + + log.debug("Running %r command with fixed codeblock.", new_ctx.command.qualified_name) new_ctx.invoked_from_error_handler = True await self.bot.invoke(new_ctx) diff --git a/bot/resources/tags/codeblock.md b/bot/resources/tags/codeblock.md index ac64656e5..6cb90696a 100644 --- a/bot/resources/tags/codeblock.md +++ b/bot/resources/tags/codeblock.md @@ -5,3 +5,5 @@ print('Hello world!') \`\`\` **These are backticks, not quotes.** Check [this](https://superuser.com/questions/254076/how-do-i-type-the-tick-and-backtick-characters-on-windows/254077#254077) out if you can't find the backtick key. + +**For long code samples,** you can use our [pastebin](https://paste.pythondiscord.com/). diff --git a/tests/bot/exts/backend/test_error_handler.py b/tests/bot/exts/backend/test_error_handler.py index 533eaeda6..0ba2fcf11 100644 --- a/tests/bot/exts/backend/test_error_handler.py +++ b/tests/bot/exts/backend/test_error_handler.py @@ -47,7 +47,7 @@ class ErrorHandlerTests(unittest.IsolatedAsyncioTestCase): ) self.cog.try_silence = AsyncMock() self.cog.try_get_tag = AsyncMock() - self.cog.try_run_eval = AsyncMock(return_value=False) + self.cog.try_run_fixed_codeblock = 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"]): @@ -75,7 +75,7 @@ class ErrorHandlerTests(unittest.IsolatedAsyncioTestCase): self.cog.try_silence = AsyncMock() self.cog.try_get_tag = AsyncMock() - self.cog.try_run_eval = AsyncMock() + self.cog.try_run_fixed_codeblock = AsyncMock() error = errors.CommandNotFound() @@ -83,7 +83,7 @@ class ErrorHandlerTests(unittest.IsolatedAsyncioTestCase): self.cog.try_silence.assert_not_awaited() self.cog.try_get_tag.assert_not_awaited() - self.cog.try_run_eval.assert_not_awaited() + self.cog.try_run_fixed_codeblock.assert_not_awaited() self.ctx.send.assert_not_awaited() async def test_error_handler_user_input_error(self): |