dragxd commited on
Commit
de762be
ยท
1 Parent(s): 9a7472d

Admin: Implement sh, eval, and /usage commands with system monitoring

Browse files
Files changed (3) hide show
  1. plugins/eval.py +89 -0
  2. plugins/usage.py +36 -0
  3. requirements.txt +1 -0
plugins/eval.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+ import traceback
4
+ import io
5
+ import asyncio
6
+ from pyrogram import Client, filters
7
+ from config import ADMIN
8
+
9
+ @Client.on_message(filters.user(ADMIN) & filters.command("sh"))
10
+ async def shell_runner(client, message):
11
+ if len(message.command) < 2:
12
+ return await message.reply_text("<b>Usage:</b> <code>/sh [command]</code>")
13
+
14
+ code = message.text.split(None, 1)[1]
15
+ msg = await message.reply_text("<code>Executing...</code>")
16
+
17
+ process = await asyncio.create_subprocess_shell(
18
+ code,
19
+ stdout=asyncio.subprocess.PIPE,
20
+ stderr=asyncio.subprocess.PIPE
21
+ )
22
+ stdout, stderr = await process.communicate()
23
+
24
+ result = str(stdout.decode().strip()) + str(stderr.decode().strip())
25
+
26
+ if len(result) > 4096:
27
+ with io.BytesIO(str.encode(result)) as out_file:
28
+ out_file.name = "shell.txt"
29
+ await message.reply_document(document=out_file, caption=f"<code>{code}</code>")
30
+ await msg.delete()
31
+ else:
32
+ await msg.edit(f"<b>Command:</b>\n<code>{code}</code>\n\n<b>Result:</b>\n<code>{result}</code>")
33
+
34
+ @Client.on_message(filters.user(ADMIN) & filters.command("eval"))
35
+ async def eval_runner(client, message):
36
+ if len(message.command) < 2:
37
+ return await message.reply_text("<b>Usage:</b> <code>/eval [code]</code>")
38
+
39
+ status_message = await message.reply_text("<code>Processing...</code>")
40
+ cmd = message.text.split(None, 1)[1]
41
+
42
+ old_stderr = sys.stderr
43
+ old_stdout = sys.stdout
44
+ redirected_output = io.StringIO()
45
+ redirected_error = io.StringIO()
46
+ sys.stdout = redirected_output
47
+ sys.stderr = redirected_error
48
+ stdout, stderr, exc = None, None, None
49
+
50
+ try:
51
+ await aexec(cmd, client, message)
52
+ except Exception:
53
+ exc = traceback.format_exc()
54
+
55
+ stdout = redirected_output.getvalue()
56
+ stderr = redirected_error.getvalue()
57
+ sys.stdout = old_stdout
58
+ sys.stderr = old_stderr
59
+
60
+ evaluation = ""
61
+ if exc:
62
+ evaluation = exc
63
+ elif stderr:
64
+ evaluation = stderr
65
+ elif stdout:
66
+ evaluation = stdout
67
+ else:
68
+ evaluation = "Success"
69
+
70
+ final_output = f"<b>EVAL</b>: <code>{cmd}</code>\n\n<b>OUTPUT</b>:\n<code>{evaluation}</code>"
71
+
72
+ if len(final_output) > 4096:
73
+ with io.BytesIO(str.encode(evaluation)) as out_file:
74
+ out_file.name = "eval.txt"
75
+ await message.reply_document(
76
+ document=out_file,
77
+ caption=f"<code>{cmd}</code>",
78
+ quote=True
79
+ )
80
+ await status_message.delete()
81
+ else:
82
+ await status_message.edit(final_output)
83
+
84
+ async def aexec(code, client, message):
85
+ exec(
86
+ f"async def __aexec(client, message): "
87
+ + "".join(f"\n {l}" for l in code.split("\n"))
88
+ )
89
+ return await locals()["__aexec"](client, message)
plugins/usage.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import shutil
2
+ import psutil
3
+ from pyrogram import Client, filters
4
+ from config import ADMIN
5
+ from helper.progress import humanbytes
6
+
7
+ @Client.on_message(filters.user(ADMIN) & filters.command("usage"))
8
+ async def usage_stats(client, message):
9
+ msg = await message.reply_text("<code>Fetching System Stats...</code>")
10
+
11
+ # CPU Usage
12
+ cpu_usage = psutil.cpu_percent(interval=0.5)
13
+
14
+ # RAM Usage
15
+ ram = psutil.virtual_memory()
16
+ ram_total = humanbytes(ram.total)
17
+ ram_used = humanbytes(ram.used)
18
+ ram_usage = ram.percent
19
+
20
+ # Disk Usage
21
+ disk = shutil.disk_usage("/")
22
+ disk_total = humanbytes(disk.total)
23
+ disk_used = humanbytes(disk.used)
24
+ disk_free = humanbytes(disk.free)
25
+ disk_usage = (disk.used / disk.total) * 100
26
+
27
+ stats = f"""<b><emoji id=5875184591395885737>๐Ÿ“Š</emoji> System Usage Stats</b>
28
+
29
+ <b><emoji id=5456140674028019486>๐Ÿ–ฅ๏ธ</emoji> CPU Usage:</b> <code>{cpu_usage}%</code>
30
+ <b><emoji id=5985386442824619877>๐Ÿ“Ÿ</emoji> RAM Usage:</b> <code>{ram_used}</code> / <code>{ram_total}</code> (<code>{ram_usage}%</code>)
31
+ <b><emoji id=5433653135799228968>๐Ÿ“</emoji> Disk Usage:</b> <code>{disk_used}</code> / <code>{disk_total}</code> (<code>{disk_usage:.1f}%</code>)
32
+ <b><emoji id=5787432469598835099>๐Ÿ’ฟ</emoji> Disk Free:</b> <code>{disk_free}</code>
33
+
34
+ <b><emoji id=5316994101688677895>โšก๏ธ</emoji> Status:</b> <code>Stable</code>"""
35
+
36
+ await msg.edit(stats)
requirements.txt CHANGED
@@ -9,4 +9,5 @@ dnspython==2.8.0
9
  Flask==3.1.3
10
  gunicorn==25.1.0
11
  aiohttp==3.13.3
 
12
 
 
9
  Flask==3.1.3
10
  gunicorn==25.1.0
11
  aiohttp==3.13.3
12
+ psutil==7.0.0
13