forked from lyonipiece/FridayUserbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode_runner.py
239 lines (218 loc) · 5.86 KB
/
code_runner.py
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# Copyright (C) 2020-2021 by DevsExpo@Github, < https://github.com/DevsExpo >.
#
# This file is part of < https://github.com/DevsExpo/FridayUserBot > project,
# and is released under the "GNU v3.0 License Agreement".
# Please see < https://github.com/DevsExpo/blob/master/LICENSE >
#
# All rights reserved.
import asyncio
import io
import sys
import traceback
import requests
from main_startup.core.decorators import friday_on_cmd
from main_startup.core.startup_helpers import run_cmd
from main_startup.helper_func.basic_helpers import (
edit_or_reply,
edit_or_send_as_file,
get_text,
)
langs = [
"c#",
"vb.net",
"f#",
"java",
"python",
"c (gcc)",
"c++ (gcc)",
"php",
"pascal",
"objective-c",
"haskell",
"ruby",
"perl",
"lua",
"nasm",
"sql server",
"javascript",
"lisp",
"prolog",
"go",
"scala",
"scheme",
"node.js",
"python 3",
"octave",
"c (clang)",
"c++ (clang)",
"c++ (vc++)",
"c (vc)",
"d",
"r",
"tcl",
"mysql",
"postgresql",
"oracle",
"swift",
"bash",
"ada",
"erlang",
"elixir",
"ocaml",
"kotlin",
"brainfuck",
"fortran",
]
EVAL = "**▶ Code :** \n`{code}` \n\n**▶ Output / TraceBack :** \n`{result}`"
@friday_on_cmd(
cmd=["exec", "eval"],
ignore_errors=True,
cmd_help={"help": "Run Python Code!", "example": '{ch}eval print("FridayUserBot")'},
)
async def eval(client, message):
stark = await edit_or_reply(message, "`Running Code... Please Wait!`")
cmd = get_text(message)
if not cmd:
await stark.edit(
"`Please Give Me A Valid Input. You Can Check Help Menu To Know More!`"
)
return
if message.reply_to_message:
message.reply_to_message.message_id
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 = EVAL.format(code=cmd, result=evaluation)
if len(cmd) >= 1023:
capt = "Eval Result!"
else:
capt = cmd
await edit_or_send_as_file(final_output, stark, client, capt, "eval-result")
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)
@friday_on_cmd(
cmd=["rc", "run"],
cmd_help={
"help": "Reply To Any Programming Language's Code To Eval In Telegram!",
"example": "{ch}run python print('FridayUserBot')",
},
)
async def any_lang_cmd_runner(client, message):
stark = await edit_or_reply(message, "`Running Code... Please Wait!`")
if len(message.text.split()) == 1:
await stark.edit(
"`Please Give Me A Valid Input. You Can Check Help Menu To Know More!`"
)
return
if not message.reply_to_message:
await stark.edit("`Reply To A Code, My Master!`")
return
reply_code = message.reply_to_message.text
lang = message.text.split(None, 1)[1]
if not lang.lower() in langs:
await stark.edit("`Invalid Language Selected!`")
return
if reply_code is None:
await stark.edit("`Reply To A Code, My Master!`")
return
data = {
"code": reply_code,
"lang": lang,
"token": "5b5f0ad8-705a-4118-87d4-c0ca29939aed",
}
r = requests.post("https://starkapis.herokuapp.com/compiler", data=data).json()
if r.get("reason") != None:
iujwal = f"""**▶ Code :** \n`{reply_code}`
**▶ Result :**
`{r.get("results")}`
**▶ Error :**
`{r.get("errors")}`
**▶ Stats :**
`{r.get("stats")}`
**▶ Success :**
`{r.get("success")}`
**▶ Warnings :**
`{r.get("warnings")}`
**▶ Reason :**
`{r.get("reason")}`
"""
else:
iujwal = f"""**▶ Code :** \n`{reply_code}`
**▶ Result :**
`{r.get("results")}`
**▶ Error :**
`{r.get("errors")}`
**▶ Stats :**
`{r.get("stats")}`
**▶ Success :**
`{r.get("success")}`
**▶ Warnings :**
`{r.get("warnings")}`
"""
await edit_or_send_as_file(
iujwal, stark, client, "`Result of Your Code!`", "rc-result"
)
@friday_on_cmd(
cmd=["bash", "terminal"],
ignore_errors=True,
cmd_help={"help": "Run Bash/Terminal Command!", "example": "{ch}bash ls"},
)
async def sed_terminal(client, message):
stark = await edit_or_reply(message, "`Please Wait!`")
cmd = get_text(message)
if not cmd:
await stark.edit(
"`Please Give Me A Valid Input. You Can Check Help Menu To Know More!`"
)
return
cmd = message.text.split(None, 1)[1]
if message.reply_to_message:
message.reply_to_message.message_id
pid, err, out, ret = await run_command(cmd)
if not out:
out = "No OutPut!"
friday = f"""**▶ CMD :**
`{cmd}`
**▶ PID :**
`{pid}`
**▶ Error TraceBack (If Any) :**
`{err}`
**▶ Output / Result (If Any) :**
`{out}`
**▶ Return Code :**
`{ret}`
"""
await edit_or_send_as_file(friday, stark, client, cmd, "bash-result")
async def run_command(cmd):
process = await asyncio.create_subprocess_shell(
cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
)
stdout, stderr = await process.communicate()
errors = stderr.decode()
if not errors:
errors = "No Errors!"
output = stdout.decode()
return process.pid, errors, output, process.returncode