diff --git a/discord_bot.php b/discord_bot.php index 15b9aba..4265496 100755 --- a/discord_bot.php +++ b/discord_bot.php @@ -3,6 +3,7 @@ require_once __DIR__ . '/vendor/autoload.php'; require_once __DIR__ . '/includes/db.php'; require_once __DIR__ . '/includes/env_loader.php'; +require_once __DIR__ . '/includes/emoji_helper.php'; use Discord\Discord; use Discord\Parts\Channel\Message; @@ -331,16 +332,17 @@ function sendToN8NIA(Message $message, string $userMessage): void function handleAutoTranslationWithButtons(PDO $pdo, Message $message, string $text): void { try { - // Si no hay texto, ignorar (ej: solo imagen/video sin caption) - if (empty(trim($text))) { + // Si no hay contenido real (solo emojis, espacios), ignorar + if (!hasRealContent($text)) { return; } require_once __DIR__ . '/src/Translate.php'; $translator = new src\Translate(); - // Detectar idioma del mensaje - $detectedLang = $translator->detectLanguage($text) ?? 'es'; + // Detectar idioma del mensaje (sin emojis para mejor precisi贸n) + $textForDetection = stripEmojisForDetection($text); + $detectedLang = $translator->detectLanguage($textForDetection) ?? 'es'; // Obtener idiomas activos de la base de datos $stmt = $pdo->query("SELECT language_code, flag_emoji FROM supported_languages WHERE is_active = 1"); @@ -531,8 +533,10 @@ function handleTranslateInteraction($interaction, string $customId): void $interaction->respondWithMessage($loadingBuilder, true); // Ahora traducir (esto puede tardar) - $sourceLang = $translator->detectLanguage($originalText) ?? 'es'; - $translated = $translator->translate($originalText, $sourceLang, $targetLang); + // Usar texto sin emojis para detectar idioma y traducir con mayor precisi贸n + $textForDetection = stripEmojisForDetection($originalText); + $sourceLang = $translator->detectLanguage($textForDetection) ?? 'es'; + $translated = $translator->translate($textForDetection, $sourceLang, $targetLang); if ($translated) { // Limpiar todo el HTML @@ -546,8 +550,24 @@ function handleTranslateInteraction($interaction, string $customId): void $translated = preg_replace('/\n\s*\n/', "\n", $translated); // Actualizar la respuesta con la traducci贸n real + // Preservar emojis del original si los hay + $displayText = trim($translated); + if (strpos($originalText, '馃憤') !== false || preg_match('/[\x{1F300}-\x{1F9FF}]/u', $originalText)) { + // Si el original ten铆a emojis, intentar identificarlos + // Para mensajes simples, los emojis generalmente est谩n al inicio o final + $emojiPrefix = ''; + $emojiSuffix = ''; + if (preg_match('/^([\x{1F300}-\x{1F9FF}]+)\s*/u', $originalText, $match)) { + $emojiPrefix = $match[1] . ' '; + } + if (preg_match('/\s*([\x{1F300}-\x{1F9FF}]+)$/u', $originalText, $match)) { + $emojiSuffix = ' ' . $match[1]; + } + $displayText = $emojiPrefix . $displayText . $emojiSuffix; + } + $finalBuilder = \Discord\Builders\MessageBuilder::new() - ->setContent("馃寪 **Traducci贸n (" . strtoupper($targetLang) . "):**\n\n" . trim($translated)); + ->setContent("馃寪 **Traducci贸n (" . strtoupper($targetLang) . "):**\n\n" . $displayText); $interaction->updateOriginalResponse($finalBuilder); } else { // Actualizar con error diff --git a/includes/emoji_helper.php b/includes/emoji_helper.php new file mode 100644 index 0000000..ef63e9e --- /dev/null +++ b/includes/emoji_helper.php @@ -0,0 +1,50 @@ +Traducciones disponibles:\nHaz clic en una bandera para ver la traducci贸n"; @@ -256,8 +268,8 @@ function handleTelegramCommand(Telegram\TelegramSender $sender, PDO $pdo, int $c function handleTelegramMessage(PDO $pdo, Telegram\TelegramSender $sender, int $chatId, int $userId, string $text): void { - // Si no hay texto, ignorar (ej: solo imagen/video sin caption) - if (empty(trim($text))) { + // Si no hay contenido real (solo emojis, espacios), ignorar + if (!hasRealContent($text)) { return; } @@ -380,12 +392,13 @@ function handleTelegramCallback(PDO $pdo, Telegram\TelegramSender $sender, src\T } try { - // Obtener el idioma original - $sourceLang = $translator->detectLanguage($originalText) ?? 'es'; + // Obtener el idioma original (usar texto sin emojis para mayor precisi贸n) + $textForDetection = stripEmojisForDetection($originalText); + $sourceLang = $translator->detectLanguage($textForDetection) ?? 'es'; file_put_contents('/var/www/html/lastwar/logs/telegram_debug.log', date('Y-m-d H:i:s') . " - sourceLang: $sourceLang, targetLang: $targetLang, originalText: " . substr($originalText, 0, 50) . "\n", FILE_APPEND); - // Traducir - $translated = $translator->translate($originalText, $sourceLang, $targetLang); + // Traducir (usar texto sin emojis para evitar interferencias) + $translated = $translator->translate($textForDetection ?: $originalText, $sourceLang, $targetLang); file_put_contents('/var/www/html/lastwar/logs/telegram_debug.log', date('Y-m-d H:i:s') . " - translated: $translated\n", FILE_APPEND); if ($translated) {