Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions bot/exts/backend/error_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ async def on_command_error(self, ctx: Context, e: errors.CommandError) -> None:
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):
Expand Down Expand Up @@ -190,9 +190,9 @@ async def try_get_tag(self, ctx: Context) -> None:
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:
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")```

Expand All @@ -204,11 +204,18 @@ async def try_run_eval(self, ctx: Context) -> bool:
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)

Expand Down
6 changes: 3 additions & 3 deletions tests/bot/exts/backend/test_error_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async def test_error_handler_command_not_found_error_not_invoked_by_handler(self
)
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)
Comment thread
wookie184 marked this conversation as resolved.

for case in test_cases:
with self.subTest(try_silence_return=case["try_silence_return"], try_get_tag=case["called_try_get_tag"]):
Expand Down Expand Up @@ -75,15 +75,15 @@ async def test_error_handler_command_not_found_error_invoked_by_handler(self):

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()

self.assertIsNone(await self.cog.on_command_error(ctx, error))

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):
Expand Down