""" plugins/chat.py — Gestión de mensajes en chats. Comandos: .del — Elimina el mensaje respondido (y el comando) .purge [n] — Elimina los últimos n mensajes tuyos (default 10, max 200) .save — Guarda el mensaje respondido en Mensajes Guardados .pin — Fija el mensaje respondido .unpin — Desfija el mensaje respondido .edit <texto> — Edita el mensaje respondido por el texto dado .copy — Copia el mensaje respondido sin el link de origen """ import asyncio from pyrogram import filters from pyrogram.errors import MessageDeleteForbidden, RPCError from pyrogram.types import Message from bot import app from config import Config P = Config.CMD_PREFIX # ── .del ────────────────────────────────────────────────────────────────────── @app.on_message(filters.me & filters.command("del", prefixes=P)) async def cmd_del(client, msg: Message): tasks = [msg.delete()] if msg.reply_to_message: tasks.append(msg.reply_to_message.delete()) await asyncio.gather(*tasks, return_exceptions=True) # ── .purge ──────────────────────────────────────────────────────────────────── @app.on_message(filters.me & filters.command("purge", prefixes=P)) async def cmd_purge(client, msg: Message): args = msg.text.split() try: n = min(int(args[1]), 200) if len(args) > 1 else 10 except ValueError: n = 10 me = await client.get_me() await msg.delete() ids_to_del = [] count = 0 async for m in client.get_chat_history(msg.chat.id, limit=n * 6): if m.from_user and m.from_user.id == me.id: ids_to_del.append(m.id) count += 1 if count >= n: break # Delete in chunks of 100 for i in range(0, len(ids_to_del), 100): chunk = ids_to_del[i:i + 100] try: await client.delete_messages(msg.chat.id, chunk) except MessageDeleteForbidden: pass await asyncio.sleep(0.4) note = await client.send_message( msg.chat.id, f"🗑️ **Eliminados** `{count}` mensajes." ) await asyncio.sleep(5) await note.delete() # ── .save ───────────────────────────────────────────────────────────────────── @app.on_message(filters.me & filters.command("save", prefixes=P)) async def cmd_save(client, msg: Message): await msg.delete() if not msg.reply_to_message: note = await client.send_message(msg.chat.id, "❓ Responde a un mensaje para guardarlo.") await asyncio.sleep(5) await note.delete() return r = msg.reply_to_message me = await client.get_me() try: await client.forward_messages("me", msg.chat.id, r.id) note = await client.send_message(msg.chat.id, "✅ **Guardado** en Mensajes Guardados.") except Exception as e: # If forward is blocked, copy as document/photo/text try: if r.media: await client.copy_message("me", msg.chat.id, r.id) else: text = r.text or r.caption or "(mensaje vacío)" from_user = r.from_user name = (from_user.first_name or "?") if from_user else "Canal" header = f"📌 Guardado de **{name}** en `{msg.chat.title or 'chat privado'}`\n\n" await client.send_message("me", header + text) note = await client.send_message(msg.chat.id, "✅ **Copiado** a Mensajes Guardados.") except Exception as e2: note = await client.send_message(msg.chat.id, f"❌ Error: `{e2}`") await asyncio.sleep(5) await note.delete() # ── .pin ────────────────────────────────────────────────────────────────────── @app.on_message(filters.me & filters.command("pin", prefixes=P)) async def cmd_pin(client, msg: Message): await msg.delete() if not msg.reply_to_message: return try: await msg.reply_to_message.pin(disable_notification=False) note = await client.send_message(msg.chat.id, "📌 **Mensaje fijado.**") await asyncio.sleep(4) await note.delete() except RPCError as e: note = await client.send_message(msg.chat.id, f"❌ `{e}`") await asyncio.sleep(5) await note.delete() # ── .unpin ──────────────────────────────────────────────────────────────────── @app.on_message(filters.me & filters.command("unpin", prefixes=P)) async def cmd_unpin(client, msg: Message): await msg.delete() if not msg.reply_to_message: try: await client.unpin_all_chat_messages(msg.chat.id) note = await client.send_message(msg.chat.id, "📌 **Todos los mensajes desfijados.**") except RPCError as e: note = await client.send_message(msg.chat.id, f"❌ `{e}`") else: try: await client.unpin_chat_message(msg.chat.id, msg.reply_to_message.id) note = await client.send_message(msg.chat.id, "📌 **Mensaje desfijado.**") except RPCError as e: note = await client.send_message(msg.chat.id, f"❌ `{e}`") await asyncio.sleep(4) await note.delete() # ── .edit ───────────────────────────────────────────────────────────────────── @app.on_message(filters.me & filters.command("edit", prefixes=P)) async def cmd_edit(client, msg: Message): args = msg.text.split(None, 1) if len(args) < 2: await msg.delete() return new_text = args[1] target = msg.reply_to_message if msg.reply_to_message and msg.reply_to_message.from_user else msg try: await target.edit(new_text) if target.id != msg.id: await msg.delete() except RPCError as e: await msg.edit(f"❌ `{e}`") # ── .copy ───────────────────────────────────────────────────────────────────── @app.on_message(filters.me & filters.command("copy", prefixes=P)) async def cmd_copy(client, msg: Message): await msg.delete() if not msg.reply_to_message: note = await client.send_message(msg.chat.id, "❓ Responde a un mensaje para copiarlo.") await asyncio.sleep(5) await note.delete() return try: await client.copy_message(msg.chat.id, msg.chat.id, msg.reply_to_message.id) except Exception as e: note = await client.send_message(msg.chat.id, f"❌ `{e}`") await asyncio.sleep(5) await note.delete()