fix(discord): filtrar botones dinámicamente en la respuesta para respetar la configuración del servidor tras reinicios
This commit is contained in:
@@ -2,18 +2,18 @@ import discord
|
||||
import html
|
||||
import re
|
||||
from botdiscord.translate import get_lang_mapping, get_flag_mapping, get_name_to_code, translate_text
|
||||
from botdiscord.database import get_message, save_translation, get_cached_translation
|
||||
from botdiscord.database import get_message, save_translation, get_cached_translation, get_active_languages, get_bot_languages
|
||||
|
||||
class TranslationButton(discord.ui.Button):
|
||||
def __init__(self, lang_name: str, lang_code: str, flag: str):
|
||||
label = flag if flag else lang_name
|
||||
# custom_id estático: esto es lo que Discord usa para vincular el clic
|
||||
custom_id = f"btn_trans_{lang_code}"
|
||||
super().__init__(label=label, style=discord.ButtonStyle.primary, custom_id=custom_id)
|
||||
self.lang_code = lang_code
|
||||
self.lang_name = lang_name
|
||||
self.flag = flag
|
||||
|
||||
async def callback(self, interaction: discord.Interaction):
|
||||
# Evita el "Interacción fallida" por timeout de 3s
|
||||
await interaction.response.defer()
|
||||
|
||||
try:
|
||||
@@ -31,7 +31,7 @@ class TranslationButton(discord.ui.Button):
|
||||
text = db_msg['content']
|
||||
mentions_map = db_msg['mentions_map']
|
||||
|
||||
# Recuperar o traducir
|
||||
# Traducción
|
||||
cached = get_cached_translation(original_msg_id, self.lang_code)
|
||||
if cached:
|
||||
translated = cached
|
||||
@@ -39,17 +39,30 @@ class TranslationButton(discord.ui.Button):
|
||||
translated = await translate_text(text, self.lang_code)
|
||||
save_translation(original_msg_id, self.lang_code, translated)
|
||||
|
||||
# Limpiar texto
|
||||
translated = html.unescape(translated)
|
||||
if mentions_map:
|
||||
for placeholder, mention in mentions_map.items():
|
||||
# El mapa ya viene con el formato <m0 />: "<@123>"
|
||||
translated = translated.replace(placeholder, mention)
|
||||
# Por si el traductor quitó el espacio: <m0/>
|
||||
translated = translated.replace(placeholder.replace(" ", ""), mention)
|
||||
|
||||
# Editar el mensaje que contiene los botones
|
||||
await interaction.edit_original_response(content=translated, view=self.view)
|
||||
# --- FILTRADO DINÁMICO DE BOTONES ---
|
||||
# Obtenemos los idiomas activos para ESTE servidor
|
||||
guild_id = interaction.guild_id
|
||||
active_codes = get_active_languages(guild_id)
|
||||
if not active_codes:
|
||||
active_codes = get_bot_languages("discord")
|
||||
|
||||
# Reconstruimos la vista solo con los botones permitidos
|
||||
new_view = discord.ui.View(timeout=None)
|
||||
from botdiscord.config import get_languages
|
||||
all_langs = get_languages()
|
||||
|
||||
for lang in all_langs:
|
||||
if lang['code'] in active_codes:
|
||||
new_view.add_item(TranslationButton(lang['name'], lang['code'], lang.get('flag', '')))
|
||||
|
||||
# Editamos con la nueva vista filtrada
|
||||
await interaction.edit_original_response(content=translated, view=new_view)
|
||||
|
||||
except Exception as e:
|
||||
print(f"[ERROR UI] {e}")
|
||||
@@ -58,15 +71,12 @@ class TranslationButton(discord.ui.Button):
|
||||
class PersistentTranslationView(discord.ui.View):
|
||||
def __init__(self):
|
||||
super().__init__(timeout=None)
|
||||
# Cargamos los botones para todos los idiomas habilitados en la config
|
||||
# Esto asegura que cualquier botón de cualquier idioma sea reconocido
|
||||
from botdiscord.config import get_languages
|
||||
for lang in get_languages():
|
||||
self.add_item(TranslationButton(lang['name'], lang['code'], lang.get('flag', '')))
|
||||
|
||||
class ConfigSelect(discord.ui.Select):
|
||||
def __init__(self, guild_id: int, bot_type: str = "discord"):
|
||||
from botdiscord.database import get_active_languages
|
||||
lang_mapping = get_lang_mapping(bot_type)
|
||||
flag_mapping = get_flag_mapping(bot_type)
|
||||
active = get_active_languages(guild_id)
|
||||
|
||||
Reference in New Issue
Block a user