File size: 4,910 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
"""
plugins/notes.py β€” Notas persistentes por chat.

Comandos:
  .save note <nombre>   β€” Guarda el mensaje respondido como nota
  .note <nombre>        β€” Muestra la nota
  .notes                β€” Lista todas las notas del chat
  .delnote <nombre>     β€” Elimina una nota
"""
import asyncio
import json
from pathlib import Path

from pyrogram import filters
from pyrogram.types import Message

from bot import app
from config import Config

P = Config.CMD_PREFIX
NOTES_FILE = Path("data/notes.json")
NOTES_FILE.parent.mkdir(exist_ok=True)


# ── Storage helpers ───────────────────────────────────────────────────────────

def _load() -> dict:
    if NOTES_FILE.exists():
        try:
            return json.loads(NOTES_FILE.read_text())
        except Exception:
            return {}
    return {}


def _save(data: dict):
    NOTES_FILE.write_text(json.dumps(data, ensure_ascii=False, indent=2))


def _key(chat_id: int) -> str:
    return str(chat_id)


async def _respond(client, chat_id: int, text: str, delay: int = 5):
    note = await client.send_message(chat_id, text)
    await asyncio.sleep(delay)
    await note.delete()


# ── .save note <nombre> ───────────────────────────────────────────────────────
@app.on_message(filters.me & filters.command("save", prefixes=P))
async def cmd_save_note(client, msg: Message):
    args = msg.text.split(None, 2)

    # "save note <nombre>" flow
    if len(args) >= 3 and args[1].lower() == "note":
        note_name = args[2].lower().strip()
        await msg.delete()

        if not msg.reply_to_message:
            await _respond(client, msg.chat.id, "❓ Responde a un mensaje para guardarlo como nota.")
            return

        r = msg.reply_to_message
        content = r.text or r.caption or ""

        db  = _load()
        cid = _key(msg.chat.id)
        if cid not in db:
            db[cid] = {}

        db[cid][note_name] = {
            "text": content,
            "media_type": str(r.media) if r.media else None,
        }
        _save(db)
        await _respond(client, msg.chat.id, f"βœ… Nota `{note_name}` guardada.")
    # else: handled by chat.py's .save command (no "note" keyword)


# ── .note <nombre> ────────────────────────────────────────────────────────────
@app.on_message(filters.me & filters.command("note", prefixes=P))
async def cmd_get_note(client, msg: Message):
    args = msg.text.split(None, 1)
    await msg.delete()

    if len(args) < 2:
        await _respond(client, msg.chat.id, f"❓ Uso: `{P}note &lt;nombre&gt;`"); return

    name = args[1].lower().strip()
    db   = _load()
    cid  = _key(msg.chat.id)

    if cid not in db or name not in db[cid]:
        await _respond(client, msg.chat.id, f"❌ No existe la nota `{name}`.")
        return

    entry = db[cid][name]
    text  = entry.get("text") or "(sin texto)"
    await client.send_message(msg.chat.id, f"πŸ“ **{name}**\n\n{text}")


# ── .notes ────────────────────────────────────────────────────────────────────
@app.on_message(filters.me & filters.command("notes", prefixes=P))
async def cmd_list_notes(client, msg: Message):
    await msg.delete()
    db  = _load()
    cid = _key(msg.chat.id)

    if cid not in db or not db[cid]:
        await _respond(client, msg.chat.id, "πŸ“‹ No hay notas en este chat.")
        return

    names = sorted(db[cid].keys())
    text  = "πŸ“‹ **Notas guardadas:**\n\n" + "\n".join(f"β€’ `{n}`" for n in names)
    note  = await client.send_message(msg.chat.id, text)
    await asyncio.sleep(12)
    await note.delete()


# ── .delnote &lt;nombre&gt; ─────────────────────────────────────────────────────────
@app.on_message(filters.me & filters.command("delnote", prefixes=P))
async def cmd_del_note(client, msg: Message):
    args = msg.text.split(None, 1)
    await msg.delete()

    if len(args) < 2:
        await _respond(client, msg.chat.id, f"❓ Uso: `{P}delnote &lt;nombre&gt;`"); return

    name = args[1].lower().strip()
    db   = _load()
    cid  = _key(msg.chat.id)

    if cid in db and name in db[cid]:
        del db[cid][name]
        _save(db)
        await _respond(client, msg.chat.id, f"πŸ—‘οΈ Nota `{name}` eliminada.")
    else:
        await _respond(client, msg.chat.id, f"❌ No existe la nota `{name}`.")