refactor(core): clean up execute command

This commit is contained in:
Ryan 2025-01-09 16:56:23 -05:00
parent c80b926b35
commit c420f3de6b
Signed by: ErrorNoInternet
GPG Key ID: 2486BFB7B1E6A4A3

21
core.py
View File

@ -77,33 +77,29 @@ async def on_message(message, edited=False):
) )
await utils.add_check_reaction(message) await utils.add_check_reaction(message)
case C.EXECUTE if message.author.id in OWNERS: case C.EXECUTE if message.author.id in OWNERS:
code = message.content[len(tokens[0]) + 1 :].strip().strip("`") code = message.content[len(tokens[0]) + 1 :].strip().strip("`")
for replacement in ["python", "py"]: for replacement in ["python", "py"]:
if code.startswith(replacement): if code.startswith(replacement):
code = code[len(replacement) :] code = code[len(replacement) :]
stdout = io.StringIO()
try: try:
stdout = io.StringIO()
with contextlib.redirect_stdout(stdout): with contextlib.redirect_stdout(stdout):
if "#globals" in code: wrapped_code = (
exec( f"async def run_code():\n{textwrap.indent(code, ' ')}"
f"async def run_code():\n{textwrap.indent(code, ' ')}", )
globals(), if "# globals" in code:
) exec(wrapped_code, globals())
await globals()["run_code"]() await globals()["run_code"]()
else: else:
dictionary = dict(locals(), **globals()) dictionary = dict(locals(), **globals())
exec( exec(wrapped_code, dictionary, dictionary)
f"async def run_code():\n{textwrap.indent(code, ' ')}",
dictionary,
dictionary,
)
await dictionary["run_code"]() await dictionary["run_code"]()
output = stdout.getvalue() output = stdout.getvalue()
except Exception as e: except Exception as e:
output = "`" + str(e) + "`" output = "`" + str(e) + "`"
output = utils.filter_secrets(output) output = utils.filter_secrets(output)
if len(output) > 2000: if len(output) > 2000:
@ -119,6 +115,7 @@ async def on_message(message, edited=False):
await utils.add_check_reaction(message) await utils.add_check_reaction(message)
else: else:
await utils.reply(message, output) await utils.reply(message, output)
case C.CLEAR | C.PURGE if message.author.id in OWNERS: case C.CLEAR | C.PURGE if message.author.id in OWNERS:
await commands.tools.clear(message) await commands.tools.clear(message)
case C.JOIN: case C.JOIN: