Spaces:
Paused
Paused
Download modules/textpack.py from saske560/userbotsaske: direct link, hf CLI and curl.
- Browser
- Download file 15.4 kB
-
https://huggingface.co/spaces/saske560/userbotsaske/resolve/main/modules/textpack.py
- Command line
-
hf download hf://spaces/saske560/userbotsaske/modules/textpack.py
-
curl -L -o textpack.py https://huggingface.co/spaces/saske560/userbotsaske/resolve/main/modules/textpack.py
15.4 kB
| 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') | |