-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.py
More file actions
342 lines (311 loc) · 13.6 KB
/
Copy pathcommand.py
File metadata and controls
342 lines (311 loc) · 13.6 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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import asyncio
import io
import json
import os
import re
import re
import sys
import traceback
import urllib.parse
from datetime import timedelta
from typing import Any
from urllib.parse import quote, urlparse
import requests
from neonize.aioze.client import NewAClient
from neonize.aioze.events import MessageEv, event
from neonize.proto import Neonize_pb2
from neonize.proto.waE2E.WAWebProtobufsE2E_pb2 import Message
from neonize.types import MessageServerID
from neonize.utils import get_message_type
from neonize.utils.enum import ReceiptType, VoteType, ParticipantChange
import config
from scrape import copilot, fesnuk, zerochan
from utils.serialize import Mess
async def aexec(code: str, client: NewAClient, m: Mess) -> Any:
local_namespace = {}
func_code = f"""
async def __eval_exec(client, m):
{chr(10).join(f' {line}' for line in code.splitlines())}
"""
try:
exec(func_code, globals(), local_namespace)
except SyntaxError as se:
raise RuntimeError(
f"Syntax Error in code:\n{se.text}\n{' ' * (se.offset - 1)}^\n{type(se).__name__}: {se.msg}"
)
eval_func = local_namespace.get("__eval_exec")
if not eval_func:
raise RuntimeError("Failed to compile code into a function.")
return await eval_func(client, m)
async def eval_message(m: Mess, cmd: str, client: NewAClient):
status_msg_info = None
temp_file_name = "neonize_eval_output.txt"
try:
status_msg_info = await client.send_message(
m.chat, "🔄 Processing eval command..."
)
status_msg_id = status_msg_info.ID
old_stdout = sys.stdout
old_stderr = sys.stderr
redirected_output = io.StringIO()
redirected_error = io.StringIO()
sys.stdout = redirected_output
sys.stderr = redirected_error
stdout_data = ""
stderr_data = ""
exception_data = ""
execution_result = None
try:
execution_result = await aexec(cmd, client, m)
except Exception as e:
exception_data = traceback.format_exc()
stdout_data = redirected_output.getvalue()
stderr_data = redirected_error.getvalue()
sys.stdout = old_stdout
sys.stderr = old_stderr
final_output_parts = [f"```python\n{cmd}\n```"]
if exception_data:
final_output_parts.append(f"```python\nException:\n{exception_data}\n```")
elif stderr_data:
final_output_parts.append(f"```stderr\n{stderr_data}\n```")
elif stdout_data:
final_output_parts.append(f"```stdout\n{stdout_data}\n```")
else:
if execution_result is not None:
try:
result_str = str(execution_result)
except Exception:
result_str = f"<{type(execution_result).__name__} object>"
final_output_parts.append(f"```Result:\n{result_str}\n```")
else:
final_output_parts.append(
"```Result:\n✅ Code executed successfully (no output).\n```"
)
final_output = "\n".join(final_output_parts)
max_message_length = 4000
if len(final_output) > max_message_length:
try:
with open(temp_file_name, "w", encoding="utf-8") as f:
f.write(final_output)
await client.send_document(
m.chat,
temp_file_name,
filename="eval_output.txt",
caption=f"📝 Eval output (too long):\n```python\n{cmd[:50]}{'...' if len(cmd) > 50 else ''}\n```",
quoted=m.message,
)
await client.edit_message(
m.chat,
status_msg_id,
Message(conversation="✅ Eval done. Output sent as file."),
)
finally:
if os.path.exists(temp_file_name):
try:
os.remove(temp_file_name)
except Exception as remove_err:
print(
f"Warning: Could not delete temp file {temp_file_name}: {remove_err}"
)
else:
await client.edit_message(
m.chat, status_msg_id, Message(conversation=final_output)
)
except Exception as outer_error:
error_trace = traceback.format_exc()
print(f"[Eval Error - Outer] {outer_error}\n{error_trace}")
if sys.stdout != old_stdout:
sys.stdout = old_stdout
if sys.stderr != old_stderr:
sys.stderr = old_stderr
if os.path.exists(temp_file_name):
try:
os.remove(temp_file_name)
except:
pass
error_msg = f"💥 *Eval Error (Outer):*\n```python\n{str(outer_error)}\n```\n```traceback\n{error_trace[-1000:]}\n```"
try:
if status_msg_info:
await client.edit_message(
m.chat, status_msg_info.ID, Message(conversation=error_msg)
)
else:
await client.send_message(m.chat, error_msg)
except:
pass
async def handler(client: NewAClient, message: Neonize_pb2.Message):
try:
async def check_owner(sender):
if sender.Server == "s.whatsapp.net":
return sender.User in config.owner
elif sender.Server == "lid":
pn = await client.get_pn_from_lid(sender)
return pn.User in config.owner
m = Mess(client,message)
budy = m.text
prefix = config.prefix
is_cmd = False
command = ""
text = ""
for p in prefix:
if budy.startswith(p):
is_cmd = True
parts = budy[len(p):].strip().split(" ", 1)
command = parts[0].lower()
if len(parts) > 1:
text = parts[1]
break
is_group = m.is_group
groupMetadata = await client.get_group_info(m.chat) if is_group else None
is_owner = await check_owner(m.sender)
is_admin = False
isBotAdmin = False
user_bot = await client.get_me()
if is_group and groupMetadata:
for participant in groupMetadata.Participants:
if (participant.JID.User == m.sender.User or participant.LID.User == m.sender.User) and (
participant.IsAdmin or participant.IsSuperAdmin
):
is_admin = True
if participant.LID.User == user_bot.LID.User and (
participant.IsAdmin or participant.IsSuperAdmin
):
isBotAdmin = True
if is_admin and isBotAdmin:
break
if not is_owner and not is_group:
return
def Example(teks):
return f"*Contoh* : {prefix}{command} " + str(teks)
match command:
case "translate"|"tr":
if not text:
return await m.reply(Example("id/en/jp"))
if not m.quoted.text:
return await m.reply("Reply pesan yang mau ditranslate")
data = requests.get(f"https://yrizzz.my.id/api/v1/tool/translate?from=auto&to={text}&data={urllib.parse.quote(m.quoted.text)}").json()
await m.reply(f"""*Detected*: {data["data"]["detect"]}
*To*: {text}
*Result*: `{data["data"]["translated"]}`""")
case "rvo"|"readviewonce":
if not m.quoted.is_media:
return await m.reply("Reply view once message to read it!")
mediad = await m.quoted.download()
media_type = m.quoted.media_type
caption = m.quoted.media_info["caption"]
if media_type == "image":
await client.send_image(m.chat, mediad, caption=caption)
elif media_type == "video":
await client.send_video(m.chat, mediad, caption=caption)
else:
return await m.reply("Unsupported media type for read-view-once.")
case "fbdl" | "fb" | "facebook" | "fesnuk":
if not text:
return await m.reply(Example("link"))
efbe_linko = fesnuk(text)
await client.send_video(m.chat, efbe_linko, quoted=message)
case "brat":
if not text:
return await m.reply(Example("halo"))
await client.send_sticker(
m.chat,
f"https://brat.siputzx.my.id/image?text={urllib.parse.quote(text)}",
quoted=message,
)
case "getme":
mek = await client.get_me()
await m.reply(mek.__str__())
case "join":
if not is_owner:
return await m.reply("Only owner!")
if not text:
return await m.reply(Example("link"))
await client.join_group_with_link(text)
await m.reply("Success")
case "leave":
if not is_owner:
return await m.reply("Only owner!")
if not is_group:
return await m.reply("Perintah ini hanya bisa digunakan di dalam grup.")
await client.send_message(m.chat, "Bye")
await client.leave_group(m.chat)
case "hidetag":
if not is_admin and not is_owner:
return await client.reply_message("Only admin!", message)
if not text:
return await client.reply_message(Example("teks"), message)
tagged = ""
for user in groupMetadata.Participants:
tagged += f"@{user.JID.User} "
await client.send_message(
m.chat, Message(conversation=text), ghost_mentions=tagged, mentions_are_lids=True
)
case "tt" | "tiktok":
if not text:
return await client.reply_message("Masukkan link video/foto TikTok", message)
try:
parsed = urlparse(text)
if not all([parsed.scheme, parsed.netloc]):
return await client.reply_message("URL tidak valid", message)
encoded_url = quote(text, safe="")
api_url = f"https://tikwm.com/api/?hd=1&url={encoded_url}"
response = requests.get(api_url, timeout=10).json()
if not response.get("data"):
return await client.reply_message("Gagal memproses video", message)
data = response["data"]
if data.get("images"):
return await client.send_album(m.chat, data["images"])
await client.send_video(m.chat, data["play"], quoted=message)
except requests.exceptions.RequestException:
await client.reply_message("Error saat menghubungi server", message)
except ValueError:
await client.reply_message("Response tidak valid", message)
case "zero":
if not text:
return await client.reply_message(Example("shiroko"), message)
linkz = zerochan(text)
await client.send_album(m.chat, linkz)
case "copilot":
if not text:
return await client.reply_message(Example("bagaimana cara ngoding"), message)
result = json.loads(copilot(text))
await client.send_message(m.chat, str(result["text"]))
case "ilping":
await client.reply_message("pong", message)
case "debug":
await client.send_message(m.chat, message.__str__())
case _:
if budy.startswith("=>"):
if not is_owner:
await m.reply("❌ Only owner can use eval!")
return
cmd = budy[2:].strip()
if not cmd:
await m.reply("❌ Please provide code to evaluate. Usage: `!=> print('Hello')`")
return
try:
await eval_message(m, cmd, client)
except Exception as e:
error_trace = traceback.format_exc()
print(f"[Eval Handler Error] {e}\n{error_trace}")
await client.send_message(
m.chat,
f"💥 *Error in eval handler setup:*\n```python\n{str(e)}\n```\n```traceback\n{error_trace[-500:]}\n```",
)
elif budy.startswith("&"):
if not is_owner:
return await m.reply("Only owner!")
command = budy[1:].strip()
process = await asyncio.create_subprocess_shell(
f"zsh -c {repr(command)}",
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
stdout, stderr = await process.communicate()
if stdout:
await m.reply(stdout.decode())
if stderr:
await m.reply(stderr.decode())
except Exception as e:
print(f"[Handler Error] {e}")
traceback.print_exc()