-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathdev.py
More file actions
146 lines (138 loc) · 4.54 KB
/
Copy pathdev.py
File metadata and controls
146 lines (138 loc) · 4.54 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# This file is Originally Written By @okay-retard on GitHub
# The Author (Jayant Kageri) just Ported this for Devloper Userbot
# (C) 2021 Jayant Kageri
from config import PREFIX
import traceback
import sys
import os
import re
import subprocess
from io import StringIO
from _pyrogram import app, CMD_HELP
from pyrogram import filters
CMD_HELP.update(
{
"Developer": """
**Developer**
`peval` -> For Running Pyrogram Evaluations
`teval` -> For Running Telethon Evaluations
`sh` -> For Running commands in shell.
"""
}
)
async def aexec(code, client, message):
exec(
f"async def __aexec(client, message): "
+ "".join(f"\n {l}" for l in code.split("\n"))
)
return await locals()["__aexec"](client, message)
@app.on_message(filters.command("peval", PREFIX) & filters.me)
async def evaluate(client, message):
status_message = await message.edit("`Running ...`")
try:
cmd = message.text.split(" ", maxsplit=1)[1]
except IndexError:
await status_message.delete()
return
reply_to_id = message.message_id
if message.reply_to_message:
reply_to_id = message.reply_to_message.message_id
old_stderr = sys.stderr
old_stdout = sys.stdout
redirected_output = sys.stdout = StringIO()
redirected_error = sys.stderr = 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 = f"<b>Command:</b>\n<code>{cmd}</code>\n\n<b>Output</b>:\n<code>{evaluation.strip()}</code>"
if len(final_output) > 4096:
filename = "output.txt"
with open(filename, "w+", encoding="utf8") as out_file:
out_file.write(str(final_output))
await message.reply_document(
document=filename,
caption="Pyrogram Eval",
disable_notification=True,
reply_to_message_id=reply_to_id,
)
os.remove(filename)
await status_message.delete()
else:
await status_message.edit(final_output)
@app.on_message(filters.command("sh", PREFIX) & filters.me)
async def terminal(client, message):
if len(message.text.split()) == 1:
await message.edit(f"Usage: `{PREFIX}sh echo owo`")
return
args = message.text.split(None, 1)
teks = args[1]
if "\n" in teks:
code = teks.split("\n")
output = ""
for x in code:
shell = re.split(""" (?=(?:[^'"]|'[^']*'|"[^"]*")*$)""", x)
try:
process = subprocess.Popen(
shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
except Exception as err:
print(err)
await message.edit(
"""
**Error:**
```{}```
""".format(
err
)
)
output += "**{}**\n".format(code)
output += process.stdout.read()[:-1].decode("utf-8")
output += "\n"
else:
shell = re.split(""" (?=(?:[^'"]|'[^']*'|"[^"]*")*$)""", teks)
for a in range(len(shell)):
shell[a] = shell[a].replace('"', "")
try:
process = subprocess.Popen(
shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
except Exception as err:
exc_type, exc_obj, exc_tb = sys.exc_info()
errors = traceback.format_exception(
etype=exc_type, value=exc_obj, tb=exc_tb
)
await message.edit("""**Error:**\n```{}```""".format("".join(errors)))
return
output = process.stdout.read()[:-1].decode("utf-8")
if str(output) == "\n":
output = None
if output:
if len(output) > 4096:
with open("output.txt", "w+") as file:
file.write(output)
await client.send_document(
message.chat.id,
"output.txt",
reply_to_message_id=message.message_id,
caption="`Output file`",
)
os.remove("output.txt")
return
await message.edit(f"**Output:**\n```{output}```", parse_mode="markdown")
else:
await message.edit("**Output:**\n`No Output`")