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 ", 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 )