From 56f646c1040c080b5ce777d66406d58d1af05dcd Mon Sep 17 00:00:00 2001 From: Mega-JC <65417594+Mega-JC@users.noreply.github.com> Date: Sat, 27 Jul 2024 22:38:52 +0200 Subject: [PATCH] Better support arbitrarily long outputs of 'eval' command in 'bot_management' extension --- Dockerfile | 1 + pcbot/exts/bot_management.py | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 4e10d1b..e1250ef 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,6 +6,7 @@ ENV PYTHONUNBUFFERED=1 \ PIP_DISABLE_PIP_VERSION_CHECK=on \ PIP_DEFAULT_TIMEOUT=100 \ VENV_PATH="/opt/.venv" + ENV PATH="$VENV_PATH/bin:$PATH" FROM python-base AS builder-base diff --git a/pcbot/exts/bot_management.py b/pcbot/exts/bot_management.py index a851415..5412579 100644 --- a/pcbot/exts/bot_management.py +++ b/pcbot/exts/bot_management.py @@ -516,7 +516,7 @@ async def eval(self, ctx: commands.Context[BotT], code: CodeBlock): try: script = compile(code.code, "", "eval") # compile script script_start = time.perf_counter() - eval_output = eval(script) # pylint: disable = eval-used + eval_output = repr(eval(script)) # pylint: disable = eval-used total = time.perf_counter() - script_start except Exception as ex: raise commands.CommandInvokeError( @@ -531,9 +531,13 @@ async def eval(self, ctx: commands.Context[BotT], code: CodeBlock): embed=discord.Embed( title=f"Return output (code executed in " f"{snakecore.utils.format_time_by_units(total)}):", - description=snakecore.utils.code_block(repr(eval_output)), + description=snakecore.utils.code_block(eval_output, max_characters=4096), color=int(self.theme_color), ), + file=discord.File( + io.StringIO(repr(eval_output)), # type: ignore + filename=f"eval_output_{datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.txt", + ) if len(eval_output) > 4096 else None, ) @is_bot_manager()