""" plugins/notes.py — Notas persistentes por chat. Comandos: .save note — Guarda el mensaje respondido como nota .note — Muestra la nota .notes — Lista todas las notas del chat .delnote — 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 ─────────────────────────────────────────────────────── @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 " 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 ──────────────────────────────────────────────────────────── @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 `"); 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 ───────────────────────────────────────────────────────── @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 `"); 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}`.")