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

@@ -77,17 +77,21 @@ async def on_message(message):
if not active_langs:
return
text_to_translate = message.content
import html
text_escaped = html.escape(message.content)
mention_pattern = re.compile(r'<@!?(\d+)>|<@&(\d+)>|<#(\d+)>')
# Buscamos menciones que ya están escapadas (&lt;@...&gt;)
mention_pattern = re.compile(r'&lt;@!?(\d+)&gt;|&lt;@&(\d+)&gt;|&lt;#(\d+)&gt;')
mentions_map = {}
def replace_mention(match):
placeholder = f"__MENTION_{len(mentions_map)}__"
mentions_map[placeholder] = match.group(0)
# Usamos una etiqueta autocerrada para que el traductor no intente cerrarla él mismo
placeholder = f"<m{len(mentions_map)} />"
# Guardamos la mención original (sin escapar) para restaurarla luego
mentions_map[placeholder] = html.unescape(match.group(0))
return placeholder
text_to_translate = mention_pattern.sub(replace_mention, text_to_translate)
text_to_translate = mention_pattern.sub(replace_mention, text_escaped)
langs_to_show = active_langs