Spaces:
Paused
Paused
File size: 7,665 Bytes
1fefee0 | 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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | import calendar
import datetime as dt
import html
def _text(ctx):
return (ctx.args or (ctx.reply.text if ctx.reply and ctx.reply.text else '') or '').strip()
def _parse_date(text: str):
text = text.strip()
for fmt in ('%Y-%m-%d', '%d/%m/%Y', '%d-%m-%Y', '%Y/%m/%d'):
try:
return dt.datetime.strptime(text, fmt).date()
except Exception:
pass
return None
def setup(registry):
async def nowx_cmd(ctx):
now = dt.datetime.now()
await ctx.respond(f'🕒 <b>Ahora</b>\n\n<code>{now.strftime("%Y-%m-%d %H:%M:%S")}</code>')
async def today_cmd(ctx):
today = dt.date.today()
await ctx.respond(f'📅 <b>Hoy</b>\n\n<code>{today.isoformat()}</code>')
async def unixnow_cmd(ctx):
await ctx.respond(f'⏱️ <b>Unix actual</b>\n\n<code>{int(dt.datetime.now().timestamp())}</code>')
async def fromunix_cmd(ctx):
text = _text(ctx)
if not text or not text.lstrip('-').isdigit():
await ctx.respond('⚠️ Pásame un timestamp Unix.')
return
value = dt.datetime.fromtimestamp(int(text))
await ctx.respond(f'⏱️ <b>Desde Unix</b>\n\n<code>{value.strftime("%Y-%m-%d %H:%M:%S")}</code>')
async def timestamp_cmd(ctx):
text = _text(ctx)
date = _parse_date(text)
if not date:
await ctx.respond('⚠️ Usa un formato como <code>2026-04-09</code> o <code>09/04/2026</code>.')
return
value = int(dt.datetime.combine(date, dt.time()).timestamp())
await ctx.respond(f'🗓️ <b>Timestamp</b>\n\n<code>{value}</code>')
async def countdown_cmd(ctx):
date = _parse_date(_text(ctx))
if not date:
await ctx.respond('⚠️ Usa una fecha como <code>2026-12-31</code>.')
return
delta = date - dt.date.today()
await ctx.respond(f'⏳ <b>Cuenta regresiva</b>\n\nFaltan <code>{delta.days}</code> días.')
async def datediff_cmd(ctx):
text = _text(ctx)
if ' | ' not in text:
await ctx.respond('⚠️ Usa <code>.datediff 2026-01-01 | 2026-12-31</code>.')
return
a_raw, b_raw = [p.strip() for p in text.split(' | ', 1)]
a = _parse_date(a_raw)
b = _parse_date(b_raw)
if not a or not b:
await ctx.respond('⚠️ Fecha inválida. Usa <code>YYYY-MM-DD</code> o <code>DD/MM/YYYY</code>.')
return
await ctx.respond(f'📏 <b>Diferencia</b>\n\n<code>{abs((b - a).days)}</code> días')
async def adddays_cmd(ctx):
text = _text(ctx)
if ' | ' not in text:
await ctx.respond('⚠️ Usa <code>.adddays 2026-04-09 | 10</code>.')
return
d_raw, n_raw = [p.strip() for p in text.split(' | ', 1)]
date = _parse_date(d_raw)
if not date or not n_raw.lstrip('-').isdigit():
await ctx.respond('⚠️ Entrada inválida.')
return
out = date + dt.timedelta(days=int(n_raw))
await ctx.respond(f'➕ <b>Fecha resultante</b>\n\n<code>{out.isoformat()}</code>')
async def addhours_cmd(ctx):
text = _text(ctx)
if ' | ' not in text:
await ctx.respond('⚠️ Usa <code>.addhours 2026-04-09 12:00 | 5</code>.')
return
d_raw, n_raw = [p.strip() for p in text.split(' | ', 1)]
try:
value = dt.datetime.strptime(d_raw, '%Y-%m-%d %H:%M')
out = value + dt.timedelta(hours=int(n_raw))
except Exception:
await ctx.respond('⚠️ Usa el formato <code>YYYY-MM-DD HH:MM | horas</code>.')
return
await ctx.respond(f'🕓 <b>Fecha y hora resultante</b>\n\n<code>{out.strftime("%Y-%m-%d %H:%M")}</code>')
async def weeknum_cmd(ctx):
date = _parse_date(_text(ctx) or dt.date.today().isoformat())
if not date:
await ctx.respond('⚠️ Fecha inválida.')
return
await ctx.respond(f'🗂️ <b>Semana del año</b>\n\n<code>{date.isocalendar().week}</code>')
async def monthcal_cmd(ctx):
today = dt.date.today()
y, m = today.year, today.month
parts = _text(ctx).split()
if len(parts) == 2 and all(p.isdigit() for p in parts):
y, m = int(parts[0]), int(parts[1])
out = calendar.month(y, m)
await ctx.respond(f'📆 <b>Calendario mensual</b>\n\n<code>{html.escape(out)}</code>')
async def leap_cmd(ctx):
text = _text(ctx)
if not text.isdigit():
await ctx.respond('⚠️ Pásame un año.')
return
year = int(text)
await ctx.respond(f'🐸 <b>Año bisiesto</b>\n\n<b>{"Sí" if calendar.isleap(year) else "No"}</b>')
async def age_cmd(ctx):
born = _parse_date(_text(ctx))
if not born:
await ctx.respond('⚠️ Pásame una fecha de nacimiento válida.')
return
today = dt.date.today()
years = today.year - born.year - ((today.month, today.day) < (born.month, born.day))
await ctx.respond(f'🎂 <b>Edad aproximada</b>\n\n<code>{years}</code> años')
async def weekday_cmd(ctx):
date = _parse_date(_text(ctx) or dt.date.today().isoformat())
if not date:
await ctx.respond('⚠️ Fecha inválida.')
return
days = ['lunes', 'martes', 'miércoles', 'jueves', 'viernes', 'sábado', 'domingo']
await ctx.respond(f'📍 <b>Día de la semana</b>\n\n<b>{days[date.weekday()]}</b>')
async def yearprogress_cmd(ctx):
today = dt.date.today()
start = dt.date(today.year, 1, 1)
end = dt.date(today.year + 1, 1, 1)
pct = ((today - start).days / ((end - start).days or 1)) * 100
await ctx.respond(f'📈 <b>Progreso del año</b>\n\n<code>{pct:.2f}%</code>')
registry.add('nowx', nowx_cmd, 'Muestra fecha y hora actual.', usage='.nowx', category='Fechas')
registry.add('todayx', today_cmd, 'Muestra la fecha de hoy.', usage='.todayx', category='Fechas')
registry.add('unixnow', unixnow_cmd, 'Muestra timestamp Unix actual.', usage='.unixnow', category='Fechas')
registry.add('fromunix', fromunix_cmd, 'Convierte Unix a fecha.', usage='.fromunix 1712678400', category='Fechas')
registry.add('timestampx', timestamp_cmd, 'Convierte fecha a timestamp.', usage='.timestampx 2026-04-09', category='Fechas')
registry.add('countdown', countdown_cmd, 'Cuenta días hasta una fecha.', usage='.countdown 2026-12-31', category='Fechas')
registry.add('datediff', datediff_cmd, 'Calcula diferencia entre dos fechas.', usage='.datediff 2026-01-01 | 2026-12-31', category='Fechas')
registry.add('adddays', adddays_cmd, 'Suma días a una fecha.', usage='.adddays 2026-04-09 | 10', category='Fechas')
registry.add('addhours', addhours_cmd, 'Suma horas a una fecha/hora.', usage='.addhours 2026-04-09 12:00 | 5', category='Fechas')
registry.add('weeknum', weeknum_cmd, 'Muestra la semana ISO del año.', usage='.weeknum 2026-04-09', category='Fechas')
registry.add('monthcal', monthcal_cmd, 'Muestra un calendario mensual.', usage='.monthcal 2026 4', category='Fechas')
registry.add('leap', leap_cmd, 'Dice si un año es bisiesto.', usage='.leap 2028', category='Fechas')
registry.add('agex', age_cmd, 'Calcula edad aproximada.', usage='.agex 2000-01-01', category='Fechas')
registry.add('weekdayx', weekday_cmd, 'Muestra el día de la semana.', usage='.weekdayx 2026-04-09', category='Fechas')
registry.add('yearprogress', yearprogress_cmd, 'Muestra el progreso del año.', usage='.yearprogress', category='Fechas')
|