File size: 7,409 Bytes
5321464
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
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()