-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
252 lines (217 loc) · 7.91 KB
/
bot.js
File metadata and controls
252 lines (217 loc) · 7.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Cargar variables de entorno desde el archivo .env
import dotenv from 'dotenv';
dotenv.config();
import fetch from 'node-fetch';
import { franc } from 'franc';
import { Translate } from '@google-cloud/translate/build/src/v2/index.js';
import { ApiClient } from '@twurple/api';
import { RefreshingAuthProvider } from '@twurple/auth';
import { ChatClient } from '@twurple/chat';
// --------------------------
// Configuración y constantes
// --------------------------
// Lista de usuarios administradores obtenida desde las variables de entorno
const ADMIN_USERS = (process.env.ADMIN_USERS || 'zhanthh').split(',');
// Modo de traducción inicial (por defecto 'polish' si no se especifica en las variables de entorno)
let translationMode = process.env.DEFAULT_TRANSLATION_MODE || 'single';
const TARGET_LANGUAGE = process.env.TARGET_LANGUAGE || 'pl';
const DEEPL_API_KEY = process.env.DEEPL_API_KEY;
// Lista de emotes de 7TV
let sevenTVEmotes = new Set();
// Obtención de los emotes de 7TV del canal
const fetch7TVEmotes = async (channelName) => {
try {
const response = await fetch(`https://7tv.io/v3/users/${channelName}`);
const data = await response.json();
if (data.emote_set?.emotes) {
sevenTVEmotes = new Set(data.emote_set.emotes.map(emote => emote.name));
}
} catch (error) {
console.error('Error fetching 7TV emotes:', error);
}
};
// --------------------------
// Funciones auxiliares
// --------------------------
// Determina si un mensaje es un emote basado en patrones comunes o 7TV
const isEmote = (text) => {
const commonExpressions = [
/^[A-Z]+$/,
/^[xX][dD]+$/i,
/^[hH][aA]+$/,
/^[jJ][aA]+$/,
/^[lL][oO][lL]+$/i,
/^[k]+[e]+[k]+$/i,
/^[w]+$/i,
/^\s*[;:][\'\"]*[-~]*[)(DO0PpxX]+\s*$/
];
// Check if it's a 7TV emote
if (sevenTVEmotes.has(text)) {
return true;
}
return commonExpressions.some(pattern => pattern.test(text)) || text.length <= 3;
};
// Limpia un mensaje para la detección de idioma, eliminando caracteres no alfabéticos y espacios extra
const cleanMessageForLangDetection = (message) => {
return message
.replace(/\s+/g, ' ')
.replace(/[^\p{L}\s]/gu, '') // Conserva solo letras y espacios
.trim();
};
// --------------------------
// Funciones de traducción y detección de idioma
// --------------------------
// Detecta el idioma de un texto usando la API de DeepL
const deeplDetectLanguage = async (text) => {
try {
const params = new URLSearchParams({
auth_key: DEEPL_API_KEY,
text,
target_lang: 'EN'
});
const response = await fetch(`https://api-free.deepl.com/v2/translate?${params}`);
const data = await response.json();
return data.translations[0].detected_source_language;
} catch (error) {
console.error('Error detectando idioma con DeepL:', error.message);
return null;
}
};
// Traduce un texto al español usando la API de DeepL
const deeplTranslate = async (text) => {
try {
const params = new URLSearchParams({
auth_key: DEEPL_API_KEY,
text,
target_lang: 'ES',
formality: 'less'
});
const response = await fetch(`https://api-free.deepl.com/v2/translate?${params}`);
if (response.status === 456) {
console.log('⚠️ Límite de DeepL alcanzado, se cambia a Google Translate.');
return null;
}
const data = await response.json();
return data.translations[0].text;
} catch (error) {
console.error('Error en DeepL:', error.message);
return null;
}
};
// Crea una instancia del cliente de Google Translate con la clave de API
const googleTranslate = new Translate({
key: process.env.GOOGLE_TRANSLATE_API_KEY
});
// Detecta el idioma de un texto usando Google Translate
const googleDetectLanguage = async (text) => {
try {
const [detection] = await googleTranslate.detect(text);
return detection.language;
} catch (error) {
console.error('Error detectando idioma con Google:', error.message);
return null;
}
};
// Traduce un texto al español usando Google Translate
const googleTranslateText = async (text) => {
try {
const [translation] = await googleTranslate.translate(text, 'es');
return translation;
} catch (error) {
console.error('Error en Google Translate:', error.message);
return null;
}
};
// --------------------------
// Función principal: inicializar el bot
// --------------------------
async function initializeBot() {
const authProvider = new RefreshingAuthProvider(
{
clientId: process.env.TWITCH_CLIENT_ID,
clientSecret: process.env.TWITCH_CLIENT_SECRET,
onRefresh: async (userId, newTokenData) => {
console.log('Token refrescado para el usuario:', userId);
}
}
);
await authProvider.addUserForToken({
accessToken: process.env.TWITCH_ACCESS_TOKEN,
refreshToken: process.env.TWITCH_REFRESH_TOKEN,
expiresIn: 0,
obtainmentTimestamp: 0,
scope: ['chat:read', 'chat:edit']
}, ['chat']);
const apiClient = new ApiClient({ authProvider });
const client = new ChatClient({
authProvider,
channels: [process.env.TWITCH_CHANNEL]
});
await fetch7TVEmotes(process.env.TWITCH_CHANNEL);
client.onMessage(async (channel, user, message, msg) => {
if (msg.userInfo.userId === client.userId) return;
if (ADMIN_USERS.includes(user.toLowerCase()) && message.startsWith('!translate')) {
const command = message.split(' ')[1];
if (command === 'single') {
translationMode = 'single';
client.say(channel, `Traduciendo ${process.env.LANGUAGE_NAME || 'el idioma configurado'}.`);
return;
} else if (command === 'all') {
translationMode = 'all';
client.say(channel, `Traduciendo todos los idiomas.`);
return;
} else if (command === 'off') {
translationMode = 'off';
client.say(channel, `Traducciones desactivadas.`);
return;
}
}
// Si las traducciones están desactivadas, no hace nada
if (translationMode === 'off') return;
try {
// Limpia el mensaje y verifica si es un emote; si lo es, lo ignora
const cleanMessage = message.trim();
if (isEmote(cleanMessage)) return;
// Prepara el texto para la detección de idioma y verifica longitud mínima
const textForLangDetection = cleanMessageForLangDetection(cleanMessage);
if (textForLangDetection.length < 4) return;
// Detección preliminar del idioma con franc para filtrar mensajes obvios
const preliminaryLang = franc(textForLangDetection);
if (preliminaryLang === 'eng' || preliminaryLang === 'spa' || preliminaryLang === 'und') {
return;
}
// Intenta detectar el idioma con DeepL; si falla, usa Google Translate
let detectedLanguage = await deeplDetectLanguage(cleanMessage);
if (!detectedLanguage) {
detectedLanguage = await googleDetectLanguage(cleanMessage);
}
// Ignora mensajes si no se detecta idioma o si son en inglés o español
if (!detectedLanguage ||
detectedLanguage.toLowerCase() === 'en' ||
detectedLanguage.toLowerCase() === 'es') {
return;
}
// En modo 'polish', solo traduce mensajes en polaco
if (translationMode === 'single' && detectedLanguage.toLowerCase() !== TARGET_LANGUAGE.toLowerCase()) {
return;
}
// Intenta traducir con DeepL; si falla, usa Google Translate
let translatedText = await deeplTranslate(cleanMessage);
if (!translatedText) {
translatedText = await googleTranslateText(cleanMessage);
}
// Envía la traducción al chat si se obtuvo correctamente
if (translatedText) {
client.say(channel, `@${user}: ${translatedText}`);
}
} catch (error) {
console.error('Error en la traducción:', error);
}
});
await client.connect();
console.log('Translation bot connected successfully');
}
// --------------------------
// Iniciar el bot
// --------------------------
initializeBot().catch(console.error);