V2 Pro: Logging rotativo, Redis cache, Health Check de LibreTranslate y Rate Limiting en botones (#7)
This commit is contained in:
@@ -11,6 +11,11 @@ from botdiscord.database import (
|
||||
get_available_languages,
|
||||
save_message
|
||||
)
|
||||
from utils.logger import discord_logger as log
|
||||
from utils.cache import cache_increment
|
||||
|
||||
# Rate Limit: máximo 1 clic por usuario por idioma cada 3 segundos
|
||||
_RATE_LIMIT_SECONDS = 3
|
||||
|
||||
class TranslationButton(discord.ui.Button):
|
||||
def __init__(self, lang_name: str, lang_code: str, flag: str):
|
||||
@@ -24,6 +29,16 @@ class TranslationButton(discord.ui.Button):
|
||||
async def callback(self, interaction: discord.Interaction):
|
||||
await interaction.response.defer()
|
||||
|
||||
# Rate Limiting: verificamos que el usuario no esté haciendo spam
|
||||
rate_key = f"rl:discord:{interaction.user.id}:{self.lang_code}"
|
||||
clicks = cache_increment(rate_key, ttl=_RATE_LIMIT_SECONDS)
|
||||
if clicks > 1:
|
||||
await interaction.followup.send(
|
||||
f"⏳ Por favor espera {_RATE_LIMIT_SECONDS} segundos entre traducciones.",
|
||||
ephemeral=True
|
||||
)
|
||||
return
|
||||
|
||||
try:
|
||||
if not interaction.message.reference:
|
||||
await interaction.followup.send("⚠️ No se pudo encontrar el mensaje original.", ephemeral=True)
|
||||
@@ -52,25 +67,22 @@ class TranslationButton(discord.ui.Button):
|
||||
translated = translated.replace(placeholder, mention)
|
||||
translated = translated.replace(placeholder.replace(" ", ""), mention)
|
||||
|
||||
# --- FILTRADO DINÁMICO Y BANDERAS DE LA BD ---
|
||||
guild_id = interaction.guild_id
|
||||
active_codes = get_active_languages(guild_id)
|
||||
if not active_codes:
|
||||
active_codes = get_bot_languages("discord")
|
||||
|
||||
# Obtenemos los idiomas y banderas REALES de la base de datos
|
||||
db_langs = get_available_languages()
|
||||
|
||||
new_view = discord.ui.View(timeout=None)
|
||||
for lang in db_langs:
|
||||
if lang['code'] in active_codes:
|
||||
# Usamos el nombre y la bandera que vienen de MySQL
|
||||
new_view.add_item(TranslationButton(lang['name'], lang['code'], lang.get('flag', '')))
|
||||
|
||||
await interaction.edit_original_response(content=translated, view=new_view)
|
||||
|
||||
except Exception as e:
|
||||
print(f"[ERROR UI] {e}")
|
||||
log.error(f"Error en botón de traducción Discord: {e}")
|
||||
await interaction.followup.send(f"❌ Error: {str(e)}", ephemeral=True)
|
||||
|
||||
class PersistentTranslationView(discord.ui.View):
|
||||
|
||||
Reference in New Issue
Block a user