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'🔢 Caracteres\n\n{len(text)}') 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'🧮 Palabras\n\n{len(text.split())}') 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'📏 Líneas\n\n{len(text.splitlines() or [text])}') 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'💾 Tamaño UTF-8\n\n{len(text.encode("utf-8"))} bytes') 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'🥇 Primera línea\n\n{html.escape(text.splitlines()[0])}') 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'🏁 Última línea\n\n{html.escape(text.splitlines()[-1])}') async def repeat_cmd(ctx): if not ctx.args.strip(): await ctx.respond('⚠️ Usa .repeatx 3 hola') return parts = ctx.args.split(maxsplit=1) if len(parts) < 2 or not parts[0].isdigit(): await ctx.respond('⚠️ Usa .repeatx 3 hola') return n = max(1, min(30, int(parts[0]))) text = parts[1] await ctx.respond('🔁 Repetición\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 .replacex buscar | reemplazo | texto o responde a un mensaje con .replacex buscar | reemplazo.') return if ctx.reply and not ctx.args.strip(): await ctx.respond('⚠️ Responde al texto y usa .replacex buscar | reemplazo.') 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 .replacex buscar | reemplazo 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 .replacex buscar | reemplazo | texto.') return old, new, base_text = parts[0], parts[1], ' | '.join(parts[2:]) out = base_text.replace(old, new) await ctx.respond(f'🪄 Texto reemplazado\n\n{html.escape(out[:3900])}') async def striptxt_cmd(ctx): text = _text(ctx) if not text: await ctx.respond('⚠️ Pásame un texto.') return await ctx.respond(f'🧼 Strip\n\n{html.escape(text.strip())}') async def lstriptxt_cmd(ctx): text = _text(ctx) if not text: await ctx.respond('⚠️ Pásame un texto.') return await ctx.respond(f'↩️ LStrip\n\n{html.escape(text.lstrip())}') async def rstriptxt_cmd(ctx): text = _text(ctx) if not text: await ctx.respond('⚠️ Pásame un texto.') return await ctx.respond(f'↪️ RStrip\n\n{html.escape(text.rstrip())}') async def swapcase_cmd(ctx): text = _text(ctx) if not text: await ctx.respond('⚠️ Pásame un texto.') return await ctx.respond(f'🔄 Swap Case\n\n{html.escape(text.swapcase())}') async def capitalize_cmd(ctx): text = _text(ctx) if not text: await ctx.respond('⚠️ Pásame un texto.') return await ctx.respond(f'✍️ Capitalizado\n\n{html.escape(text.capitalize())}') async def slug_cmd(ctx): text = _text(ctx) if not text: await ctx.respond('⚠️ Pásame un texto.') return await ctx.respond(f'🔗 Slug\n\n{_slugify(text)}') async def wrap_cmd(ctx): if not ctx.args.strip(): await ctx.respond('⚠️ Usa .wrap 20 texto largo') return parts = ctx.args.split(maxsplit=1) if len(parts) < 2 or not parts[0].isdigit(): await ctx.respond('⚠️ Usa .wrap 20 texto largo') return width = max(5, min(120, int(parts[0]))) out = textwrap.fill(parts[1], width=width) await ctx.respond(f'🧵 Texto envuelto\n\n{html.escape(out[:3900])}') async def center_cmd(ctx): if not ctx.args.strip(): await ctx.respond('⚠️ Usa .centerx 20 hola') return parts = ctx.args.split(maxsplit=1) if len(parts) < 2 or not parts[0].isdigit(): await ctx.respond('⚠️ Usa .centerx 20 hola') return width = max(1, min(120, int(parts[0]))) await ctx.respond(f'🎯 Center\n\n{html.escape(parts[1].center(width))}') 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'🧹 Sin palabras repetidas\n\n{html.escape(" ".join(seen)[:3900])}') 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'🔤 Palabras ordenadas\n\n{html.escape(" ".join(words)[:3900])}') 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'🔃 Líneas invertidas\n\n{html.escape(joined)}') 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'🧾 Líneas ordenadas\n\n{html.escape(joined)}') 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'✅ Líneas únicas\n\n{html.escape(joined)}') async def find_cmd(ctx): raw = ctx.args.strip() if not raw: await ctx.respond('⚠️ Usa .findx palabra | texto o responde al texto con .findx palabra.') 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 .findx palabra | texto.') 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'🔎 Coincidencias\n\n🔢 Total: {len(matches)}\n📍 Posiciones: {joined}') async def countsub_cmd(ctx): raw = ctx.args.strip() if not raw: await ctx.respond('⚠️ Usa .countsub palabra | texto o responde al texto con .countsub palabra.') 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 .countsub palabra | texto.') return needle, hay = parts count = hay.lower().count(needle.lower()) await ctx.respond(f'🔢 Recuento\n\n{count}') async def splitx_cmd(ctx): raw = ctx.args.strip() if not raw: await ctx.respond('⚠️ Usa .splitx separador | texto o responde al texto con .splitx ,.') 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 .splitx separador | texto.') 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'🧩 Split\n\n🔢 Partes: {len(items)}\n\n{html.escape(preview[:3500])}') async def joinx_cmd(ctx): raw = ctx.args.strip() if not raw: await ctx.respond('⚠️ Usa .joinx separador | a,b,c') return parts = [p.strip() for p in raw.split(' | ', 1)] if len(parts) != 2: await ctx.respond('⚠️ Usa .joinx separador | a,b,c') 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'🪢 Join\n\n{html.escape(sep.join(items)[:3900])}') 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'🎲 Líneas mezcladas\n\n{html.escape(joined)}') 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')