Spaces:
Paused
Paused
File size: 15,364 Bytes
1fefee0 a15b1d6 1fefee0 a15b1d6 1fefee0 a15b1d6 1fefee0 a15b1d6 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 | import html
import random
import re
import textwrap
import unicodedata
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('\n')
def _split_two(text: str):
if ' | ' in text:
return [p.strip() for p in text.split(' | ', 1)]
if '\n' in text:
return [p.strip() for p in text.split('\n', 1)]
parts = text.split(maxsplit=1)
if len(parts) == 2:
return parts
return [text, '']
def _slugify(value: str) -> str:
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii')
value = re.sub(r'[^a-zA-Z0-9]+', '-', value).strip('-').lower()
return value or 'texto'
def setup(registry):
async def chars_cmd(ctx):
text = _text(ctx)
if not text:
await ctx.respond('⚠️ Pásame un texto o responde a uno.')
return
await ctx.respond(f'🔢 <b>Caracteres</b>\n\n<code>{len(text)}</code>')
async def words_cmd(ctx):
text = _text(ctx)
if not text:
await ctx.respond('⚠️ Pásame un texto o responde a uno.')
return
await ctx.respond(f'🧮 <b>Palabras</b>\n\n<code>{len(text.split())}</code>')
async def lines_cmd(ctx):
text = _text(ctx)
if not text:
await ctx.respond('⚠️ Pásame un texto o responde a uno.')
return
await ctx.respond(f'📏 <b>Líneas</b>\n\n<code>{len(text.splitlines() or [text])}</code>')
async def bytes_cmd(ctx):
text = _text(ctx)
if not text:
await ctx.respond('⚠️ Pásame un texto o responde a uno.')
return
await ctx.respond(f'💾 <b>Tamaño UTF-8</b>\n\n<code>{len(text.encode("utf-8"))} bytes</code>')
async def first_cmd(ctx):
text = _text(ctx)
if not text:
await ctx.respond('⚠️ Pásame un texto o responde a uno.')
return
await ctx.respond(f'🥇 <b>Primera línea</b>\n\n<code>{html.escape(text.splitlines()[0])}</code>')
async def last_cmd(ctx):
text = _text(ctx)
if not text:
await ctx.respond('⚠️ Pásame un texto o responde a uno.')
return
await ctx.respond(f'🏁 <b>Última línea</b>\n\n<code>{html.escape(text.splitlines()[-1])}</code>')
async def repeat_cmd(ctx):
if not ctx.args.strip():
await ctx.respond('⚠️ Usa <code>.repeatx 3 hola</code>')
return
parts = ctx.args.split(maxsplit=1)
if len(parts) < 2 or not parts[0].isdigit():
await ctx.respond('⚠️ Usa <code>.repeatx 3 hola</code>')
return
n = max(1, min(30, int(parts[0])))
text = parts[1]
await ctx.respond('🔁 <b>Repetición</b>\n\n' + html.escape('\n'.join(text for _ in range(n))))
async def replace_cmd(ctx):
text = _text(ctx)
if not text:
await ctx.respond('⚠️ Usa <code>.replacex buscar | reemplazo | texto</code> o responde a un mensaje con <code>.replacex buscar | reemplazo</code>.')
return
if ctx.reply and not ctx.args.strip():
await ctx.respond('⚠️ Responde al texto y usa <code>.replacex buscar | reemplazo</code>.')
return
raw = ctx.args if ctx.args else text
if ctx.reply:
base_text = _text(ctx)
parts = [p.strip() for p in raw.split(' | ', 1)]
if len(parts) != 2:
await ctx.respond('⚠️ Usa <code>.replacex buscar | reemplazo</code> respondiendo al texto.')
return
old, new = parts
out = base_text.replace(old, new)
else:
parts = [p.strip() for p in raw.split(' | ')]
if len(parts) < 3:
await ctx.respond('⚠️ Usa <code>.replacex buscar | reemplazo | texto</code>.')
return
old, new, base_text = parts[0], parts[1], ' | '.join(parts[2:])
out = base_text.replace(old, new)
await ctx.respond(f'🪄 <b>Texto reemplazado</b>\n\n<code>{html.escape(out[:3900])}</code>')
async def striptxt_cmd(ctx):
text = _text(ctx)
if not text:
await ctx.respond('⚠️ Pásame un texto.')
return
await ctx.respond(f'🧼 <b>Strip</b>\n\n<code>{html.escape(text.strip())}</code>')
async def lstriptxt_cmd(ctx):
text = _text(ctx)
if not text:
await ctx.respond('⚠️ Pásame un texto.')
return
await ctx.respond(f'↩️ <b>LStrip</b>\n\n<code>{html.escape(text.lstrip())}</code>')
async def rstriptxt_cmd(ctx):
text = _text(ctx)
if not text:
await ctx.respond('⚠️ Pásame un texto.')
return
await ctx.respond(f'↪️ <b>RStrip</b>\n\n<code>{html.escape(text.rstrip())}</code>')
async def swapcase_cmd(ctx):
text = _text(ctx)
if not text:
await ctx.respond('⚠️ Pásame un texto.')
return
await ctx.respond(f'🔄 <b>Swap Case</b>\n\n<code>{html.escape(text.swapcase())}</code>')
async def capitalize_cmd(ctx):
text = _text(ctx)
if not text:
await ctx.respond('⚠️ Pásame un texto.')
return
await ctx.respond(f'✍️ <b>Capitalizado</b>\n\n<code>{html.escape(text.capitalize())}</code>')
async def slug_cmd(ctx):
text = _text(ctx)
if not text:
await ctx.respond('⚠️ Pásame un texto.')
return
await ctx.respond(f'🔗 <b>Slug</b>\n\n<code>{_slugify(text)}</code>')
async def wrap_cmd(ctx):
if not ctx.args.strip():
await ctx.respond('⚠️ Usa <code>.wrap 20 texto largo</code>')
return
parts = ctx.args.split(maxsplit=1)
if len(parts) < 2 or not parts[0].isdigit():
await ctx.respond('⚠️ Usa <code>.wrap 20 texto largo</code>')
return
width = max(5, min(120, int(parts[0])))
out = textwrap.fill(parts[1], width=width)
await ctx.respond(f'🧵 <b>Texto envuelto</b>\n\n<code>{html.escape(out[:3900])}</code>')
async def center_cmd(ctx):
if not ctx.args.strip():
await ctx.respond('⚠️ Usa <code>.centerx 20 hola</code>')
return
parts = ctx.args.split(maxsplit=1)
if len(parts) < 2 or not parts[0].isdigit():
await ctx.respond('⚠️ Usa <code>.centerx 20 hola</code>')
return
width = max(1, min(120, int(parts[0])))
await ctx.respond(f'🎯 <b>Center</b>\n\n<code>{html.escape(parts[1].center(width))}</code>')
async def dedupewords_cmd(ctx):
text = _text(ctx)
if not text:
await ctx.respond('⚠️ Pásame un texto.')
return
seen = []
for word in text.split():
if word not in seen:
seen.append(word)
await ctx.respond(f'🧹 <b>Sin palabras repetidas</b>\n\n<code>{html.escape(" ".join(seen)[:3900])}</code>')
async def sortwords_cmd(ctx):
text = _text(ctx)
if not text:
await ctx.respond('⚠️ Pásame un texto.')
return
words = sorted(text.split(), key=lambda x: x.lower())
await ctx.respond(f'🔤 <b>Palabras ordenadas</b>\n\n<code>{html.escape(" ".join(words)[:3900])}</code>')
async def reverselines_cmd(ctx):
text = _text(ctx)
if not text:
await ctx.respond('⚠️ Pásame un texto.')
return
lines = list(reversed(text.splitlines() or [text]))
joined = '\n'.join(lines)[:3900]
await ctx.respond(f'🔃 <b>Líneas invertidas</b>\n\n<code>{html.escape(joined)}</code>')
async def sortlines_cmd(ctx):
text = _text(ctx)
if not text:
await ctx.respond('⚠️ Pásame un texto.')
return
lines = sorted(text.splitlines() or [text], key=lambda x: x.lower())
joined = '\n'.join(lines)[:3900]
await ctx.respond(f'🧾 <b>Líneas ordenadas</b>\n\n<code>{html.escape(joined)}</code>')
async def uniqlines_cmd(ctx):
text = _text(ctx)
if not text:
await ctx.respond('⚠️ Pásame un texto.')
return
out = []
seen = set()
for line in text.splitlines() or [text]:
if line not in seen:
seen.add(line)
out.append(line)
joined = '\n'.join(out)[:3900]
await ctx.respond(f'✅ <b>Líneas únicas</b>\n\n<code>{html.escape(joined)}</code>')
async def find_cmd(ctx):
raw = ctx.args.strip()
if not raw:
await ctx.respond('⚠️ Usa <code>.findx palabra | texto</code> o responde al texto con <code>.findx palabra</code>.')
return
if ctx.reply:
needle = raw
hay = _text(ctx)
else:
parts = [p.strip() for p in raw.split(' | ', 1)]
if len(parts) != 2:
await ctx.respond('⚠️ Usa <code>.findx palabra | texto</code>.')
return
needle, hay = parts
matches = [m.start() for m in re.finditer(re.escape(needle), hay, flags=re.I)]
if not matches:
await ctx.respond('🔎 No encontré coincidencias.')
return
joined = ', '.join(map(str, matches[:50]))
await ctx.respond(f'🔎 <b>Coincidencias</b>\n\n🔢 Total: <code>{len(matches)}</code>\n📍 Posiciones: <code>{joined}</code>')
async def countsub_cmd(ctx):
raw = ctx.args.strip()
if not raw:
await ctx.respond('⚠️ Usa <code>.countsub palabra | texto</code> o responde al texto con <code>.countsub palabra</code>.')
return
if ctx.reply:
needle = raw
hay = _text(ctx)
else:
parts = [p.strip() for p in raw.split(' | ', 1)]
if len(parts) != 2:
await ctx.respond('⚠️ Usa <code>.countsub palabra | texto</code>.')
return
needle, hay = parts
count = hay.lower().count(needle.lower())
await ctx.respond(f'🔢 <b>Recuento</b>\n\n<code>{count}</code>')
async def splitx_cmd(ctx):
raw = ctx.args.strip()
if not raw:
await ctx.respond('⚠️ Usa <code>.splitx separador | texto</code> o responde al texto con <code>.splitx ,</code>.')
return
if ctx.reply:
sep = raw.encode('utf-8').decode('unicode_escape')
hay = _text(ctx)
else:
parts = [p.strip() for p in raw.split(' | ', 1)]
if len(parts) != 2:
await ctx.respond('⚠️ Usa <code>.splitx separador | texto</code>.')
return
sep = parts[0].encode('utf-8').decode('unicode_escape')
hay = parts[1]
items = hay.split(sep)
preview = '\n'.join(f'{i+1}. {item}' for i, item in enumerate(items[:80]))
await ctx.respond(f'🧩 <b>Split</b>\n\n🔢 Partes: <code>{len(items)}</code>\n\n<code>{html.escape(preview[:3500])}</code>')
async def joinx_cmd(ctx):
raw = ctx.args.strip()
if not raw:
await ctx.respond('⚠️ Usa <code>.joinx separador | a,b,c</code>')
return
parts = [p.strip() for p in raw.split(' | ', 1)]
if len(parts) != 2:
await ctx.respond('⚠️ Usa <code>.joinx separador | a,b,c</code>')
return
sep = parts[0].encode('utf-8').decode('unicode_escape')
items = [x.strip() for x in re.split(r'[,\n]', parts[1]) if x.strip()]
await ctx.respond(f'🪢 <b>Join</b>\n\n<code>{html.escape(sep.join(items)[:3900])}</code>')
async def shufflelines_cmd(ctx):
text = _text(ctx)
if not text:
await ctx.respond('⚠️ Pásame un texto con varias líneas.')
return
lines = text.splitlines() or [text]
random.shuffle(lines)
joined = '\n'.join(lines)[:3900]
await ctx.respond(f'🎲 <b>Líneas mezcladas</b>\n\n<code>{html.escape(joined)}</code>')
registry.add('chars', chars_cmd, 'Cuenta caracteres.', usage='.chars hola mundo', category='Texto')
registry.add('words', words_cmd, 'Cuenta palabras.', usage='.words hola mundo', category='Texto')
registry.add('lines', lines_cmd, 'Cuenta líneas.', usage='.lines texto', category='Texto')
registry.add('bytesx', bytes_cmd, 'Cuenta bytes en UTF-8.', usage='.bytesx hola', category='Texto')
registry.add('first', first_cmd, 'Muestra la primera línea.', usage='.first', category='Texto')
registry.add('last', last_cmd, 'Muestra la última línea.', usage='.last', category='Texto')
registry.add('repeatx', repeat_cmd, 'Repite un texto varias veces.', usage='.repeatx 3 hola', category='Texto')
registry.add('replacex', replace_cmd, 'Reemplaza texto.', usage='.replacex viejo | nuevo | texto', category='Texto')
registry.add('striptxt', striptxt_cmd, 'Quita espacios al inicio y final.', usage='.striptxt hola ', category='Texto')
registry.add('lstriptxt', lstriptxt_cmd, 'Quita espacios al inicio.', usage='.lstriptxt hola', category='Texto')
registry.add('rstriptxt', rstriptxt_cmd, 'Quita espacios al final.', usage='.rstriptxt hola ', category='Texto')
registry.add('swapcase', swapcase_cmd, 'Alterna mayúsculas/minúsculas.', usage='.swapcase HoLa', category='Texto')
registry.add('capitalizex', capitalize_cmd, 'Capitaliza la primera letra.', usage='.capitalizex hola', category='Texto')
registry.add('slug', slug_cmd, 'Convierte un texto en slug.', usage='.slug Mi título bonito', category='Texto')
registry.add('wrap', wrap_cmd, 'Ajusta texto a cierto ancho.', usage='.wrap 20 texto largo', category='Texto')
registry.add('centerx', center_cmd, 'Centra un texto en cierto ancho.', usage='.centerx 20 hola', category='Texto')
registry.add('dedupewords', dedupewords_cmd, 'Elimina palabras repetidas.', usage='.dedupewords hola hola mundo', category='Texto')
registry.add('sortwords', sortwords_cmd, 'Ordena palabras alfabéticamente.', usage='.sortwords pera manzana uva', category='Texto')
registry.add('reverselines', reverselines_cmd, 'Invierte el orden de las líneas.', usage='.reverselines', category='Texto')
registry.add('sortlines', sortlines_cmd, 'Ordena líneas alfabéticamente.', usage='.sortlines', category='Texto')
registry.add('uniqlines', uniqlines_cmd, 'Deja solo líneas únicas.', usage='.uniqlines', category='Texto')
registry.add('findx', find_cmd, 'Busca una subcadena y muestra posiciones.', usage='.findx hola | hola mundo hola', category='Texto')
registry.add('countsub', countsub_cmd, 'Cuenta apariciones de una subcadena.', usage='.countsub hola | hola mundo hola', category='Texto')
registry.add('splitx', splitx_cmd, 'Divide texto por separador.', usage='.splitx , | a,b,c', category='Texto')
registry.add('joinx', joinx_cmd, 'Une elementos con separador.', usage='.joinx - | a,b,c', category='Texto')
registry.add('shufflelines', shufflelines_cmd, 'Mezcla las líneas de un texto.', usage='.shufflelines', category='Texto')
|