userbotsaske / modules /convertpack.py
saske560's picture
Upload 15 files
1fefee0 verified
Raw History Blame Contribute Delete
15.2 kB
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'🧪 <b>Hex</b>\n\n<code>{text.encode().hex()}</code>')
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: <code>{html.escape(str(exc))}</code>')
return
await ctx.respond(f'🧪 <b>Hex decode</b>\n\n<code>{html.escape(decoded)}</code>')
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'💠 <b>Binario</b>\n\n<code>{out[:3900]}</code>')
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: <code>{html.escape(str(exc))}</code>')
return
await ctx.respond(f'💠 <b>Binario decode</b>\n\n<code>{html.escape(out)}</code>')
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'🔢 <b>Códigos Unicode</b>\n\n<code>{out[:3900]}</code>')
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: <code>{html.escape(str(exc))}</code>')
return
await ctx.respond(f'🔤 <b>Texto desde códigos</b>\n\n<code>{html.escape(out)}</code>')
async def rot13_cmd(ctx):
text = _text(ctx)
if not text:
await ctx.respond('⚠️ Pásame texto.')
return
await ctx.respond(f'🌀 <b>ROT13</b>\n\n<code>{html.escape(codecs.encode(text, "rot_13"))}</code>')
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'🏛️ <b>Número romano</b>\n\n<code>{to_roman(value)}</code>')
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: <code>{html.escape(str(exc))}</code>')
return
await ctx.respond(f'🏛️ <b>Romano a decimal</b>\n\n<code>{value}</code>')
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'🌡️ <b>Celsius → Fahrenheit</b>\n\n<code>{out:.2f} °F</code>')
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'🌡️ <b>Fahrenheit → Celsius</b>\n\n<code>{out:.2f} °C</code>')
async def km2mi_cmd(ctx):
num = _num(ctx)
if num is None:
await ctx.respond('⚠️ Pásame kilómetros.')
return
await ctx.respond(f'🛣️ <b>Kilómetros → Millas</b>\n\n<code>{num * 0.621371:.4f} mi</code>')
async def mi2km_cmd(ctx):
num = _num(ctx)
if num is None:
await ctx.respond('⚠️ Pásame millas.')
return
await ctx.respond(f'🛣️ <b>Millas → Kilómetros</b>\n\n<code>{num / 0.621371:.4f} km</code>')
async def kg2lb_cmd(ctx):
num = _num(ctx)
if num is None:
await ctx.respond('⚠️ Pásame kilogramos.')
return
await ctx.respond(f'🏋️ <b>Kilogramos → Libras</b>\n\n<code>{num * 2.20462:.4f} lb</code>')
async def lb2kg_cmd(ctx):
num = _num(ctx)
if num is None:
await ctx.respond('⚠️ Pásame libras.')
return
await ctx.respond(f'🏋️ <b>Libras → Kilogramos</b>\n\n<code>{num / 2.20462:.4f} kg</code>')
async def cm2in_cmd(ctx):
num = _num(ctx)
if num is None:
await ctx.respond('⚠️ Pásame centímetros.')
return
await ctx.respond(f'📏 <b>Centímetros → Pulgadas</b>\n\n<code>{num / 2.54:.4f} in</code>')
async def in2cm_cmd(ctx):
num = _num(ctx)
if num is None:
await ctx.respond('⚠️ Pásame pulgadas.')
return
await ctx.respond(f'📏 <b>Pulgadas → Centímetros</b>\n\n<code>{num * 2.54:.4f} cm</code>')
async def m2ft_cmd(ctx):
num = _num(ctx)
if num is None:
await ctx.respond('⚠️ Pásame metros.')
return
await ctx.respond(f'📐 <b>Metros → Pies</b>\n\n<code>{num * 3.28084:.4f} ft</code>')
async def ft2m_cmd(ctx):
num = _num(ctx)
if num is None:
await ctx.respond('⚠️ Pásame pies.')
return
await ctx.respond(f'📐 <b>Pies → Metros</b>\n\n<code>{num / 3.28084:.4f} m</code>')
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'🔣 <b>Decimal → Hex</b>\n\n<code>{hex(int(num))}</code>')
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: <code>{html.escape(str(exc))}</code>')
return
await ctx.respond(f'🔣 <b>Hex → Decimal</b>\n\n<code>{out}</code>')
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'💡 <b>Decimal → Binario</b>\n\n<code>{bin(int(num))}</code>')
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: <code>{html.escape(str(exc))}</code>')
return
await ctx.respond(f'💡 <b>Binario → Decimal</b>\n\n<code>{out}</code>')
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: <code>{html.escape(str(exc))}</code>')
return
await ctx.respond(f'🗜️ <b>JSON minificado</b>\n\n<code>{html.escape(json.dumps(obj, ensure_ascii=False, separators=(",", ":"))[:3900])}</code>')
async def htmlescape_cmd(ctx):
text = _text(ctx)
if not text:
await ctx.respond('⚠️ Pásame un texto.')
return
await ctx.respond(f'🧷 <b>HTML escape</b>\n\n<code>{html.escape(text)}</code>')
async def htmlunescape_cmd(ctx):
text = _text(ctx)
if not text:
await ctx.respond('⚠️ Pásame un texto.')
return
await ctx.respond(f'🧷 <b>HTML unescape</b>\n\n<code>{html.escape(html.unescape(text))}</code>')
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'📦 <b>Base32 encode</b>\n\n<code>{out}</code>')
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: <code>{html.escape(str(exc))}</code>')
return
await ctx.respond(f'📦 <b>Base32 decode</b>\n\n<code>{html.escape(out)}</code>')
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'📡 <b>Morse</b>\n\n<code>{out[:3900]}</code>')
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'📡 <b>Morse decode</b>\n\n<code>{html.escape(out)}</code>')
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 <b>hola</b>', category='Conversión')
registry.add('htmlunescape', htmlunescape_cmd, 'Desescapa HTML.', usage='.htmlunescape &lt;b&gt;hola&lt;/b&gt;', 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')