import base64 import codecs import html import json import math def _text(ctx): return (ctx.args or (ctx.reply.text if ctx.reply and ctx.reply.text else '') or (ctx.reply.caption if ctx.reply and ctx.reply.caption else '') or '').strip() def _num(ctx): text = _text(ctx) if not text: return None try: return float(text.replace(',', '.')) except Exception: return None ROMAN = [ (1000, 'M'), (900, 'CM'), (500, 'D'), (400, 'CD'), (100, 'C'), (90, 'XC'), (50, 'L'), (40, 'XL'), (10, 'X'), (9, 'IX'), (5, 'V'), (4, 'IV'), (1, 'I') ] ROMAN_MAP = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000} MORSE = { 'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.', 'F': '..-.', 'G': '--.', 'H': '....', 'I': '..', 'J': '.---', 'K': '-.-', 'L': '.-..', 'M': '--', 'N': '-.', 'O': '---', 'P': '.--.', 'Q': '--.-', 'R': '.-.', 'S': '...', 'T': '-', 'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-', 'Y': '-.--', 'Z': '--..', '0': '-----', '1': '.----', '2': '..---', '3': '...--', '4': '....-', '5': '.....', '6': '-....', '7': '--...', '8': '---..', '9': '----.', ' ': '/' } MORSE_DEC = {v: k for k, v in MORSE.items()} def to_roman(num: int) -> str: out = [] for value, symbol in ROMAN: while num >= value: out.append(symbol) num -= value return ''.join(out) def from_roman(text: str) -> int: total = 0 prev = 0 for ch in reversed(text.upper()): value = ROMAN_MAP[ch] if value < prev: total -= value else: total += value prev = value return total def setup(registry): async def hexe_cmd(ctx): text = _text(ctx) if not text: await ctx.respond('⚠️ Pásame texto para convertir a hexadecimal.') return await ctx.respond(f'🧪 Hex\n\n{text.encode().hex()}') async def hexd_cmd(ctx): text = _text(ctx).replace(' ', '') if not text: await ctx.respond('⚠️ Pásame un texto hexadecimal.') return try: decoded = bytes.fromhex(text).decode('utf-8', errors='ignore') except Exception as exc: await ctx.respond(f'❌ Hex inválido: {html.escape(str(exc))}') return await ctx.respond(f'🧪 Hex decode\n\n{html.escape(decoded)}') async def binee_cmd(ctx): text = _text(ctx) if not text: await ctx.respond('⚠️ Pásame texto para convertir a binario.') return out = ' '.join(format(ord(ch), '08b') for ch in text) await ctx.respond(f'💠 Binario\n\n{out[:3900]}') async def bind_cmd(ctx): text = _text(ctx) if not text: await ctx.respond('⚠️ Pásame binario separado por espacios.') return try: out = ''.join(chr(int(x, 2)) for x in text.split()) except Exception as exc: await ctx.respond(f'❌ Binario inválido: {html.escape(str(exc))}') return await ctx.respond(f'💠 Binario decode\n\n{html.escape(out)}') async def ords_cmd(ctx): text = _text(ctx) if not text: await ctx.respond('⚠️ Pásame texto.') return out = ' '.join(str(ord(ch)) for ch in text) await ctx.respond(f'🔢 Códigos Unicode\n\n{out[:3900]}') async def charsfrom_cmd(ctx): text = _text(ctx) if not text: await ctx.respond('⚠️ Pásame números Unicode separados por espacios o comas.') return try: nums = [int(x) for x in text.replace(',', ' ').split()] out = ''.join(chr(n) for n in nums) except Exception as exc: await ctx.respond(f'❌ Entrada inválida: {html.escape(str(exc))}') return await ctx.respond(f'🔤 Texto desde códigos\n\n{html.escape(out)}') async def rot13_cmd(ctx): text = _text(ctx) if not text: await ctx.respond('⚠️ Pásame texto.') return await ctx.respond(f'🌀 ROT13\n\n{html.escape(codecs.encode(text, "rot_13"))}') async def roman_cmd(ctx): num = _num(ctx) if num is None: await ctx.respond('⚠️ Pásame un número entero del 1 al 3999.') return value = int(num) if value < 1 or value > 3999: await ctx.respond('⚠️ El rango válido es 1-3999.') return await ctx.respond(f'🏛️ Número romano\n\n{to_roman(value)}') async def unroman_cmd(ctx): text = _text(ctx).upper() if not text: await ctx.respond('⚠️ Pásame un número romano.') return try: value = from_roman(text) except Exception as exc: await ctx.respond(f'❌ Número romano inválido: {html.escape(str(exc))}') return await ctx.respond(f'🏛️ Romano a decimal\n\n{value}') async def c2f_cmd(ctx): num = _num(ctx) if num is None: await ctx.respond('⚠️ Pásame grados Celsius.') return out = (num * 9 / 5) + 32 await ctx.respond(f'🌡️ Celsius → Fahrenheit\n\n{out:.2f} °F') async def f2c_cmd(ctx): num = _num(ctx) if num is None: await ctx.respond('⚠️ Pásame grados Fahrenheit.') return out = (num - 32) * 5 / 9 await ctx.respond(f'🌡️ Fahrenheit → Celsius\n\n{out:.2f} °C') async def km2mi_cmd(ctx): num = _num(ctx) if num is None: await ctx.respond('⚠️ Pásame kilómetros.') return await ctx.respond(f'🛣️ Kilómetros → Millas\n\n{num * 0.621371:.4f} mi') async def mi2km_cmd(ctx): num = _num(ctx) if num is None: await ctx.respond('⚠️ Pásame millas.') return await ctx.respond(f'🛣️ Millas → Kilómetros\n\n{num / 0.621371:.4f} km') async def kg2lb_cmd(ctx): num = _num(ctx) if num is None: await ctx.respond('⚠️ Pásame kilogramos.') return await ctx.respond(f'🏋️ Kilogramos → Libras\n\n{num * 2.20462:.4f} lb') async def lb2kg_cmd(ctx): num = _num(ctx) if num is None: await ctx.respond('⚠️ Pásame libras.') return await ctx.respond(f'🏋️ Libras → Kilogramos\n\n{num / 2.20462:.4f} kg') async def cm2in_cmd(ctx): num = _num(ctx) if num is None: await ctx.respond('⚠️ Pásame centímetros.') return await ctx.respond(f'📏 Centímetros → Pulgadas\n\n{num / 2.54:.4f} in') async def in2cm_cmd(ctx): num = _num(ctx) if num is None: await ctx.respond('⚠️ Pásame pulgadas.') return await ctx.respond(f'📏 Pulgadas → Centímetros\n\n{num * 2.54:.4f} cm') async def m2ft_cmd(ctx): num = _num(ctx) if num is None: await ctx.respond('⚠️ Pásame metros.') return await ctx.respond(f'📐 Metros → Pies\n\n{num * 3.28084:.4f} ft') async def ft2m_cmd(ctx): num = _num(ctx) if num is None: await ctx.respond('⚠️ Pásame pies.') return await ctx.respond(f'📐 Pies → Metros\n\n{num / 3.28084:.4f} m') async def dec2hex_cmd(ctx): num = _num(ctx) if num is None: await ctx.respond('⚠️ Pásame un número entero.') return await ctx.respond(f'🔣 Decimal → Hex\n\n{hex(int(num))}') async def hex2dec_cmd(ctx): text = _text(ctx).lower().replace('0x', '') if not text: await ctx.respond('⚠️ Pásame un hexadecimal.') return try: out = int(text, 16) except Exception as exc: await ctx.respond(f'❌ Hex inválido: {html.escape(str(exc))}') return await ctx.respond(f'🔣 Hex → Decimal\n\n{out}') async def dec2bin_cmd(ctx): num = _num(ctx) if num is None: await ctx.respond('⚠️ Pásame un número entero.') return await ctx.respond(f'💡 Decimal → Binario\n\n{bin(int(num))}') async def bin2dec_cmd(ctx): text = _text(ctx).replace('0b', '').replace(' ', '') if not text: await ctx.respond('⚠️ Pásame un binario.') return try: out = int(text, 2) except Exception as exc: await ctx.respond(f'❌ Binario inválido: {html.escape(str(exc))}') return await ctx.respond(f'💡 Binario → Decimal\n\n{out}') async def jsonmin_cmd(ctx): text = _text(ctx) if not text: await ctx.respond('⚠️ Pásame JSON.') return try: obj = json.loads(text) except Exception as exc: await ctx.respond(f'❌ JSON inválido: {html.escape(str(exc))}') return await ctx.respond(f'🗜️ JSON minificado\n\n{html.escape(json.dumps(obj, ensure_ascii=False, separators=(",", ":"))[:3900])}') async def htmlescape_cmd(ctx): text = _text(ctx) if not text: await ctx.respond('⚠️ Pásame un texto.') return await ctx.respond(f'🧷 HTML escape\n\n{html.escape(text)}') async def htmlunescape_cmd(ctx): text = _text(ctx) if not text: await ctx.respond('⚠️ Pásame un texto.') return await ctx.respond(f'🧷 HTML unescape\n\n{html.escape(html.unescape(text))}') async def base32e_cmd(ctx): text = _text(ctx) if not text: await ctx.respond('⚠️ Pásame un texto.') return out = base64.b32encode(text.encode()).decode() await ctx.respond(f'📦 Base32 encode\n\n{out}') async def base32d_cmd(ctx): text = _text(ctx) if not text: await ctx.respond('⚠️ Pásame Base32.') return try: out = base64.b32decode(text.encode()).decode(errors='ignore') except Exception as exc: await ctx.respond(f'❌ Base32 inválido: {html.escape(str(exc))}') return await ctx.respond(f'📦 Base32 decode\n\n{html.escape(out)}') async def morsee_cmd(ctx): text = _text(ctx).upper() if not text: await ctx.respond('⚠️ Pásame un texto simple.') return out = ' '.join(MORSE.get(ch, '?') for ch in text) await ctx.respond(f'📡 Morse\n\n{out[:3900]}') async def morsed_cmd(ctx): text = _text(ctx) if not text: await ctx.respond('⚠️ Pásame código Morse.') return out = ''.join(MORSE_DEC.get(token, '?') for token in text.split()) await ctx.respond(f'📡 Morse decode\n\n{html.escape(out)}') registry.add('hexe', hexe_cmd, 'Convierte texto a hexadecimal.', usage='.hexe hola', category='Conversión') registry.add('hexd', hexd_cmd, 'Convierte hexadecimal a texto.', usage='.hexd 686f6c61', category='Conversión') registry.add('binee', binee_cmd, 'Convierte texto a binario.', usage='.binee hola', category='Conversión') registry.add('bind', bind_cmd, 'Convierte binario a texto.', usage='.bind 01101000 01101111', category='Conversión') registry.add('ords', ords_cmd, 'Muestra códigos Unicode.', usage='.ords hola', category='Conversión') registry.add('charsfrom', charsfrom_cmd, 'Crea texto desde códigos Unicode.', usage='.charsfrom 72 111 108 97', category='Conversión') registry.add('rot13', rot13_cmd, 'Aplica ROT13.', usage='.rot13 hola', category='Conversión') registry.add('roman', roman_cmd, 'Convierte decimal a romano.', usage='.roman 2026', category='Conversión') registry.add('unroman', unroman_cmd, 'Convierte romano a decimal.', usage='.unroman MMXXVI', category='Conversión') registry.add('c2f', c2f_cmd, 'Convierte Celsius a Fahrenheit.', usage='.c2f 25', category='Conversión') registry.add('f2c', f2c_cmd, 'Convierte Fahrenheit a Celsius.', usage='.f2c 77', category='Conversión') registry.add('km2mi', km2mi_cmd, 'Convierte kilómetros a millas.', usage='.km2mi 10', category='Conversión') registry.add('mi2km', mi2km_cmd, 'Convierte millas a kilómetros.', usage='.mi2km 10', category='Conversión') registry.add('kg2lb', kg2lb_cmd, 'Convierte kg a lb.', usage='.kg2lb 70', category='Conversión') registry.add('lb2kg', lb2kg_cmd, 'Convierte lb a kg.', usage='.lb2kg 154', category='Conversión') registry.add('cm2in', cm2in_cmd, 'Convierte cm a pulgadas.', usage='.cm2in 10', category='Conversión') registry.add('in2cm', in2cm_cmd, 'Convierte pulgadas a cm.', usage='.in2cm 4', category='Conversión') registry.add('m2ft', m2ft_cmd, 'Convierte metros a pies.', usage='.m2ft 2', category='Conversión') registry.add('ft2m', ft2m_cmd, 'Convierte pies a metros.', usage='.ft2m 6', category='Conversión') registry.add('dec2hex', dec2hex_cmd, 'Convierte decimal a hex.', usage='.dec2hex 255', category='Conversión') registry.add('hex2dec', hex2dec_cmd, 'Convierte hex a decimal.', usage='.hex2dec ff', category='Conversión') registry.add('dec2bin', dec2bin_cmd, 'Convierte decimal a binario.', usage='.dec2bin 10', category='Conversión') registry.add('bin2dec', bin2dec_cmd, 'Convierte binario a decimal.', usage='.bin2dec 1010', category='Conversión') registry.add('jsonmin', jsonmin_cmd, 'Minifica JSON.', usage='.jsonmin {"ok":true}', category='Conversión') registry.add('htmlescape', htmlescape_cmd, 'Escapa HTML.', usage='.htmlescape hola', category='Conversión') registry.add('htmlunescape', htmlunescape_cmd, 'Desescapa HTML.', usage='.htmlunescape <b>hola</b>', category='Conversión') registry.add('base32e', base32e_cmd, 'Codifica en Base32.', usage='.base32e hola', category='Conversión') registry.add('base32d', base32d_cmd, 'Decodifica Base32.', usage='.base32d NBSWY3DP', category='Conversión') registry.add('morsee', morsee_cmd, 'Convierte texto a Morse.', usage='.morsee SOS', category='Conversión') registry.add('morsed', morsed_cmd, 'Convierte Morse a texto.', usage='.morsed ... --- ...', category='Conversión')