From c420f3de6bd85efc8a8a76f335e2e97e542ca501 Mon Sep 17 00:00:00 2001 From: ErrorNoInternet Date: Thu, 9 Jan 2025 16:56:23 -0500 Subject: [PATCH] refactor(core): clean up execute command --- core.py | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/core.py b/core.py index b5de1ca..f9b2a9d 100644 --- a/core.py +++ b/core.py @@ -77,33 +77,29 @@ async def on_message(message, edited=False): ) await utils.add_check_reaction(message) + case C.EXECUTE if message.author.id in OWNERS: code = message.content[len(tokens[0]) + 1 :].strip().strip("`") for replacement in ["python", "py"]: if code.startswith(replacement): code = code[len(replacement) :] - stdout = io.StringIO() try: + stdout = io.StringIO() with contextlib.redirect_stdout(stdout): - if "#globals" in code: - exec( - f"async def run_code():\n{textwrap.indent(code, ' ')}", - globals(), - ) + wrapped_code = ( + f"async def run_code():\n{textwrap.indent(code, ' ')}" + ) + if "# globals" in code: + exec(wrapped_code, globals()) await globals()["run_code"]() else: dictionary = dict(locals(), **globals()) - exec( - f"async def run_code():\n{textwrap.indent(code, ' ')}", - dictionary, - dictionary, - ) + exec(wrapped_code, dictionary, dictionary) await dictionary["run_code"]() output = stdout.getvalue() except Exception as e: output = "`" + str(e) + "`" - output = utils.filter_secrets(output) if len(output) > 2000: @@ -119,6 +115,7 @@ async def on_message(message, edited=False): await utils.add_check_reaction(message) else: await utils.reply(message, output) + case C.CLEAR | C.PURGE if message.author.id in OWNERS: await commands.tools.clear(message) case C.JOIN: