Spaces:
Paused
Paused
File size: 15,207 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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 | 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 <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')
|