From a7f692cb5d344acc739ac226bd32110e4f892fe3 Mon Sep 17 00:00:00 2001 From: Nick80835 Date: Sun, 19 Jul 2020 11:13:54 -0400 Subject: [PATCH] add eval --- ubot/modules/_sudo.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/ubot/modules/_sudo.py b/ubot/modules/_sudo.py index fc4b6cb..5400c12 100644 --- a/ubot/modules/_sudo.py +++ b/ubot/modules/_sudo.py @@ -1,6 +1,8 @@ # SPDX-License-Identifier: GPL-2.0-or-later import asyncio +import inspect +import io import os from platform import python_version @@ -10,6 +12,36 @@ from telethon import version from ubot.micro_bot import ldr, micro_bot +@ldr.add("eval", owner=True, hide_help=True) +async def evaluate(event): + if not event.args: + await event.reply("Give me code to run!") + return + + eval_msg = await event.reply("Processing…") + reply = await event.get_reply_message() + + try: + eval_ret = eval(event.args) + except Exception as exception: + eval_ret = exception + + if inspect.isawaitable(eval_ret): + isawait = " (awaited)" + eval_ret = await eval_ret + else: + isawait = "" + + if len(f"**Evaluation:**\n{event.args}\n**Return{isawait}:**\n{eval_ret}") > 4096: + text_io = io.BytesIO(str(eval_ret).encode("utf-8")) + text_io.name = "return.txt" + await eval_msg.edit("Output too large for a message, sending as a file…") + await eval_msg.reply(file=text_io) + return + + await eval_msg.edit(f"**Evaluation:**\n{event.args}\n**Return{isawait}:**\n{eval_ret}") + + @ldr.add("reload", sudo=True, hide_help=True) async def reload_modules(event): reload_msg = await event.reply("Reloading modules…")