""" plugins/filters_cmd.py — Filtros de texto para limpiar captions. Guarda frases a filtrar (en JSON). El comando .limpiar quita esas frases del texto de un mensaje respondido. Comandos: .filteradd <frase> — Agrega una frase al filtro .filterdel <frase> — Elimina una frase del filtro .filterlist — Lista todas las frases filtradas .filterclear — Elimina todos los filtros .limpiar — Responde a un msg: devuelve el texto sin las frases filtradas """ 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 DB = Path("data/filters.json") DB.parent.mkdir(exist_ok=True) # ── Storage ─────────────────────────────────────────────────────────────────── def _load() -> list: if DB.exists(): try: return json.loads(DB.read_text()) except Exception: return [] return [] def _save(data: list): DB.write_text(json.dumps(data, ensure_ascii=False, indent=2)) async def _tmp(client, chat_id: int, text: str, delay: int = 6): m = await client.send_message(chat_id, text) await asyncio.sleep(delay) await m.delete() # ── .filteradd ──────────────────────────────────────────────────────────────── @app.on_message(filters.me & filters.command("filteradd", prefixes=P)) async def cmd_filteradd(client, msg: Message): await msg.delete() args = msg.text.split(None, 1) if len(args) < 2: await _tmp(client, msg.chat.id, f"❓ **Uso:** `{P}filteradd <frase>`") return frase = args[1].strip() lst = _load() if frase in lst: await _tmp(client, msg.chat.id, f"ℹ️ La frase ya está en el filtro: `{frase}`") return lst.append(frase) _save(lst) await _tmp(client, msg.chat.id, f"✅ Filtro añadido: `{frase}`") # ── .filterdel ──────────────────────────────────────────────────────────────── @app.on_message(filters.me & filters.command("filterdel", prefixes=P)) async def cmd_filterdel(client, msg: Message): await msg.delete() args = msg.text.split(None, 1) if len(args) < 2: await _tmp(client, msg.chat.id, f"❓ **Uso:** `{P}filterdel <frase>`") return frase = args[1].strip() lst = _load() if frase in lst: lst.remove(frase) _save(lst) await _tmp(client, msg.chat.id, f"✅ Filtro eliminado: `{frase}`") else: await _tmp(client, msg.chat.id, f"❌ No se encontró la frase: `{frase}`") # ── .filterlist ─────────────────────────────────────────────────────────────── @app.on_message(filters.me & filters.command("filterlist", prefixes=P)) async def cmd_filterlist(client, msg: Message): await msg.delete() lst = _load() if not lst: await _tmp(client, msg.chat.id, "📋 No hay filtros configurados.") return lines = ["🔍 **Filtros activos:**\n"] for i, f in enumerate(lst[:30], 1): lines.append(f"`{i}.` {f}") lines.append(f"\n**Total:** `{len(lst)}`") m = await client.send_message(msg.chat.id, "\n".join(lines)) await asyncio.sleep(12) await m.delete() # ── .filterclear ────────────────────────────────────────────────────────────── @app.on_message(filters.me & filters.command("filterclear", prefixes=P)) async def cmd_filterclear(client, msg: Message): await msg.delete() lst = _load() _save([]) await _tmp(client, msg.chat.id, f"🗑️ **{len(lst)}** filtros eliminados.") # ── .limpiar ────────────────────────────────────────────────────────────────── @app.on_message(filters.me & filters.command("limpiar", prefixes=P)) async def cmd_limpiar(client, msg: Message): await msg.delete() if not msg.reply_to_message: await _tmp(client, msg.chat.id, f"❓ Responde a un mensaje con `{P}limpiar` para limpiar su texto.") return texto = ( msg.reply_to_message.text or msg.reply_to_message.caption or "" ) if not texto: await _tmp(client, msg.chat.id, "❌ El mensaje no tiene texto.") return lst = _load() resultado = texto for frase in lst: resultado = resultado.replace(frase, "") # Limpiar líneas vacías sobrantes lineas = [l.strip() for l in resultado.splitlines() if l.strip()] resultado = "\n".join(lineas).strip() if not resultado: await _tmp(client, msg.chat.id, "⚠️ El texto quedó vacío tras aplicar los filtros.") return m = await client.send_message(msg.chat.id, f"✂️ **Texto limpiado:**\n\n{resultado}") await asyncio.sleep(15) await m.delete()