File size: 3,157 Bytes
4297e31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import asyncio 
import os
import tempfile 
from pyrogram import Client, filters 
from pyrogram.types import Message 
from bot.config import Config 
 
 
async def background_wait(process, message: Message):
    stdout, stderr = await process.communicate()
    output = (stdout or stderr).decode(errors="ignore") or "Proses selesai tanpa output."

    if len(output) < 3500:
        await message.reply_text(
            f"✅ Proses selesai\n\n{output}",
            parse_mode=None,
            reply_to_message_id=message.id
        )
    else:
        filename = None
        try:
            with tempfile.NamedTemporaryFile(
                mode="w+",
                suffix=".txt",
                delete=False
            ) as f:
                f.write(output)
                filename = f.name

            await message.reply_document(
                filename,
                caption="📄 Proses selesai (output lengkap)",
                reply_to_message_id=message.id
            )
        finally:
            if filename and os.path.exists(filename):
                os.remove(filename)
 
 
@Client.on_message(

    filters.command("shell") &

    filters.user(Config.OWNER_ID)

)
async def shell_handler(client: Client, message: Message):
    if len(message.command) < 2:
        await message.reply_text(
            "❌ Gunakan: /shell <command>",
            reply_to_message_id=message.id
        )
        return

    command = message.text.split(" ", 1)[1]

    process = await asyncio.create_subprocess_shell(
        command,
        stdout=asyncio.subprocess.PIPE,
        stderr=asyncio.subprocess.PIPE
    )

    try:
        stdout, stderr = await asyncio.wait_for(
            process.communicate(),
            timeout=3
        )

        output = (stdout or stderr).decode(errors="ignore") or "Tidak ada output."

        if len(output) < 3500:
            await message.reply_text(
                output,
                parse_mode=None,
                reply_to_message_id=message.id
            )
        else:
            filename = None
            try:
                with tempfile.NamedTemporaryFile(
                    mode="w+",
                    suffix=".txt",
                    delete=False
                ) as f:
                    f.write(output)
                    filename = f.name

                await message.reply_document(
                    filename,
                    caption="📄 Output terlalu panjang",
                    reply_to_message_id=message.id
                )
            finally:
                if filename and os.path.exists(filename):
                    os.remove(filename)

    except asyncio.TimeoutError:
        asyncio.create_task(
            background_wait(process, message)
        )

        await message.reply_text(
            "⏳ Proses berat terdeteksi (mis. ffmpeg)\n"
            "Berjalan di background.\n"
            "Output akan dikirim otomatis saat selesai.",
            parse_mode=None,
            reply_to_message_id=message.id
        )