-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy patheval.py
More file actions
70 lines (58 loc) · 1.96 KB
/
Copy patheval.py
File metadata and controls
70 lines (58 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import io
import sys
import traceback
from PyrogramBot import app
from PyrogramBot.config import OWNER_ID
from PyrogramBot.utils.errors import capture_err
from pyrogram import Client, filters
__MODULE__ = "Eval"
__HELP__ = "/eval [python code] - Evaluate the Given Code"
@app.on_message(filters.user(OWNER_ID) & filters.command("eval"))
@capture_err
async def eval(client, message):
status_message = await message.reply_text("Processing ...")
cmd = message.text.split(" ", maxsplit=1)[1]
reply_to_ = message
if message.reply_to_message:
reply_to_ = message.reply_to_message
old_stderr = sys.stderr
old_stdout = sys.stdout
redirected_output = sys.stdout = io.StringIO()
redirected_error = sys.stderr = io.StringIO()
stdout, stderr, exc = None, None, None
try:
await aexec(cmd, client, message)
except Exception:
exc = traceback.format_exc()
stdout = redirected_output.getvalue()
stderr = redirected_error.getvalue()
sys.stdout = old_stdout
sys.stderr = old_stderr
evaluation = ""
if exc:
evaluation = exc
elif stderr:
evaluation = stderr
elif stdout:
evaluation = stdout
else:
evaluation = "Success"
final_output = "<b>EVAL</b>: "
final_output += f"<code>{cmd}</code>\n\n"
final_output += "<b>OUTPUT</b>:\n"
final_output += f"<code>{evaluation.strip()}</code> \n"
if len(final_output) > 4096:
with io.BytesIO(str.encode(final_output)) as out_file:
out_file.name = "eval.text"
await reply_to_.reply_document(
document=out_file, caption=cmd, disable_notification=True
)
else:
await reply_to_.reply_text(final_output)
await status_message.delete()
async def aexec(code, client, message):
exec(
"async def __aexec(client, message): "
+ "".join(f"\n {l_}" for l_ in code.split("\n"))
)
return await locals()["__aexec"](client, message)