userbotsaskeds / plugins /notes.py
saske560's picture
Upload 18 files
5321464 verified
Raw History Blame Contribute Delete
4.91 kB
"""
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}`.")