feat(discord): proteger menciones y mejorar traducción HTML para mensajes multilínea

This commit is contained in:
2026-03-06 19:28:48 -06:00
parent e4d50b6eb5
commit 7601979d3e
3 changed files with 29 additions and 7 deletions

View File

@@ -34,9 +34,27 @@ class TranslationButton(discord.ui.Button):
# Traducimos el texto
translated = await translate_text(self.text, self.lang_code)
# Desescapamos el HTML para recuperar caracteres especiales
import html
import re
translated = html.unescape(translated)
# Reemplazamos menciones si existen
for placeholder, mention in self.mentions_map.items():
translated = translated.replace(placeholder, mention)
# Extraer el número del tag, ej: m0 de <m0 />
match = re.search(r'm\d+', placeholder)
if not match: continue
tag_num = match.group()
# 1. Reemplazamos la etiqueta de apertura (o autocontenida) por la mención
# Patrón: < \s* mX \s* /? \s* >
open_pattern = re.compile(rf'<\s*{tag_num}\s*/?\s*>')
translated = open_pattern.sub(mention, translated)
# 2. Eliminamos cualquier etiqueta de cierre que el traductor haya "inventado"
# Patrón: < \s* / \s* mX \s* >
close_pattern = re.compile(rf'<\s*/\s*{tag_num}\s*>')
translated = close_pattern.sub('', translated)
# En lugar de enviar un mensaje nuevo, EDITAMOS el mensaje actual (el de los botones)
if self.attachments: