import html
import time
def human(seconds: int) -> str:
days, rem = divmod(seconds, 86400)
hours, rem = divmod(rem, 3600)
minutes, secs = divmod(rem, 60)
parts = []
if days:
parts.append(f"{days}d")
if hours:
parts.append(f"{hours}h")
if minutes:
parts.append(f"{minutes}m")
if secs or not parts:
parts.append(f"{secs}s")
return " ".join(parts)
def parse_duration(raw: str):
raw = raw.strip().lower()
if not raw:
return None
unit = raw[-1]
if unit not in {"s", "m", "h", "d"}:
return None
if not raw[:-1].isdigit():
return None
amount = int(raw[:-1])
return amount * {"s": 1, "m": 60, "h": 3600, "d": 86400}[unit]
def setup(registry):
async def remind_cmd(ctx):
if not ctx.args.strip():
await ctx.respond("⚠️ Usa .remind 10m llamar al cliente")
return
parts = ctx.args.split(maxsplit=1)
if len(parts) < 2:
await ctx.respond("⚠️ Debes indicar duración y texto. Ejemplo: .remind 30m beber agua")
return
seconds = parse_duration(parts[0])
if seconds is None:
await ctx.respond("⚠️ Tiempo inválido. Usa formatos como 10m, 2h, 1d.")
return
text = parts[1]
item = await ctx.app.reminder_manager.create(ctx.message.chat.id, text, seconds)
await ctx.respond(
"⏰ Recordatorio creado\n\n"
f"📝 Texto: {html.escape(text)}\n"
f"⌛ Se enviará en: {human(seconds)}\n"
f"🆔 ID: {item['id']}"
)
async def timers_cmd(ctx):
reminders = ctx.app.state.data.get("reminders", [])
if not reminders:
await ctx.respond("⏰ No hay recordatorios activos.")
return
lines = ["⏰ Recordatorios activos", ""]
now = time.time()
for item in reminders[:20]:
remaining = max(0, int(item.get("due_at", now) - now))
lines.append(
f"• {item['id']} — {html.escape(item['text'][:40])}\n"
f" vence en {human(remaining)}"
)
await ctx.respond("\n".join(lines))
registry.add("remind", remind_cmd, "Crea un recordatorio local.", usage=".remind 15m revisar correo", category="Recordatorios")
registry.add("timers", timers_cmd, "Lista recordatorios activos.", usage=".timers", aliases=["reminders"], category="Recordatorios")