-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.mjs
473 lines (402 loc) · 18.4 KB
/
index.mjs
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
import dotenv from 'dotenv';
dotenv.config();
import { Client, GatewayIntentBits, EmbedBuilder } from 'discord.js';
import fetch from 'node-fetch';
import langdetect from 'langdetect';
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, StringSelectMenuBuilder } from 'discord.js';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
// Setup log file
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const logFile = fs.createWriteStream(path.join(__dirname, 'bot.log'), { flags: 'a' });
const ignoreWordsPath = './ignoreWords.json';
const blacklistedChannelsPath = './blacklist.json';
let ignoreWords = [];
let blacklist = {};
const commandPrefix = 't.';
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });
const API_URL = process.env.API_URL;
// Logging function
function log(message) {
const timestamp = new Date().toISOString();
logFile.write(`[${timestamp}] ${message}\n`);
console.log(`[${timestamp}] ${message}`);
}
client.once('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
log(`Connected to servers: ${client.guilds.cache.map(guild => `${guild.name} (ID: ${guild.id})`).join(', ')}`);
// Log the blacklisted channels for each server
client.guilds.cache.forEach(guild => {
const blacklisted = blacklist[guild.id] || [];
const blacklistedNames = blacklisted.map(channelId => {
const channel = guild.channels.cache.get(channelId);
return channel ? `${channel.name} (ID: ${channelId})` : `Unknown (ID: ${channelId})`;
});
log(`Blacklisted channels in ${guild.name} (${guild.id}): ${blacklistedNames.join(', ')}`);
});
});
// Load existing ignore words and blacklisted channels from file
if (fs.existsSync(ignoreWordsPath)) {
const data = fs.readFileSync(ignoreWordsPath);
ignoreWords = JSON.parse(data);
}
if (fs.existsSync(ignoreWordsPath)) {
try {
const data = fs.readFileSync(ignoreWordsPath, 'utf8');
if (data.trim()) {
ignoreWords = JSON.parse(data);
} else {
log('Ignore words file is empty.');
}
} catch (error) {
log(`Failed to parse ignoreWords.json: ${error.message}`);
console.error('Error parsing ignoreWords.json:', error);
}
}
// Save blacklisted channels to file
function saveBlacklistedChannels() {
fs.writeFileSync(blacklistedChannelsPath, JSON.stringify(blacklist, null, 2));
}
// Load ignore words
function saveIgnoreWords() {
fs.writeFileSync(ignoreWordsPath, JSON.stringify(ignoreWords, null, 2));
}
function cleanMessage(content) {
return content
.replace(/<:[a-zA-Z0-9_]+:[0-9]+>/g, '') // Remove custom emotes
.replace(/:[a-zA-Z0-9_]+:/g, '') // Remove text-based emotes
.replace(/[\u{1F600}-\u{1F64F}]/gu, '') // Remove standard Unicode emojis
.trim();
}
function createIgnoreWordsEmbed(page = 1) {
const wordsPerPage = 50;
const totalPages = Math.ceil(ignoreWords.length / wordsPerPage);
const startIndex = (page - 1) * wordsPerPage;
const endIndex = startIndex + wordsPerPage;
const wordsToShow = ignoreWords.slice(startIndex, endIndex);
// * Set the desired column width
const columnWidth = 25; // * Adjust this value as needed for spacing
// * Format words into 2 columns with a specified width
let formattedWords = '';
for (let i = 0; i < wordsToShow.length; i += 2) {
const word1 = `${startIndex + i + 1}. ${wordsToShow[i] || ''}`.padEnd(columnWidth);
const word2 = wordsToShow[i + 1] ? `${startIndex + i + 2}. ${wordsToShow[i + 1] || ''}` : ''; // Avoids adding undefined
formattedWords += `${word1}${word2}\n`;
}
const embed = new EmbedBuilder()
.setTitle('Ignored Words')
.setDescription(`\`\`\`${formattedWords}\`\`\``)
.setFooter({ text: `Page ${page} of ${totalPages}` });
const buttons = new ActionRowBuilder().addComponents(
new ButtonBuilder()
.setCustomId('prev10')
.setLabel('Previous 10')
.setStyle(ButtonStyle.Secondary)
.setDisabled(page <= 10),
new ButtonBuilder()
.setCustomId('prev')
.setLabel('Previous')
.setStyle(ButtonStyle.Primary)
.setDisabled(page === 1),
new ButtonBuilder()
.setCustomId('next')
.setLabel('Next')
.setStyle(ButtonStyle.Primary)
.setDisabled(page === totalPages),
new ButtonBuilder()
.setCustomId('next10')
.setLabel('Next 10')
.setStyle(ButtonStyle.Secondary)
.setDisabled(page + 10 > totalPages)
);
return { embed, buttons };
}
client.on('interactionCreate', async (interaction) => {
if (!interaction.isButton()) return;
let currentPage = parseInt(interaction.message.embeds[0].footer.text.match(/Page (\d+) of/)[1]);
if (interaction.customId === 'prev10') { currentPage -= 10; }
if (interaction.customId === 'prev') { currentPage--; }
if (interaction.customId === 'next') { currentPage++; }
if (interaction.customId === 'next10') { currentPage += 10; }
// Ensure the page is within valid range
if (currentPage < 1) currentPage = 1;
if (currentPage > Math.ceil(ignoreWords.length / 50)) currentPage = Math.ceil(ignoreWords.length / 50);
const { embed, buttons } = createIgnoreWordsEmbed(currentPage);
await interaction.update({ embeds: [embed], components: [buttons] });
});
client.on('messageCreate', async (message) => {
try {
// * Check if the message is from a blacklisted channel
const isBlacklisted = blacklist[message.guild.id] && blacklist[message.guild.id].includes(message.channel.id);
// * List of management commands that can bypass the blacklist
const managementCommands = ['addbl', 'rmbl', 'viewbl'];
// * Extract the command from the message content
const [command] = message.content.slice(commandPrefix.length).trim().split(/\s+/);
// * Allow management commands even in blacklisted channels
if (isBlacklisted && !managementCommands.includes(command)) {
log('Message is in a blacklisted channel, ignoring.');
return;
}
// * Initialize blacklist for the server if it doesn't exist
if (!blacklist[message.guild.id]) {
blacklist[message.guild.id] = message.guild.channels.cache.map(channel => channel.id);
saveBlacklistedChannels();
}
// * Ignore messages from blacklisted channels if not a management command
if (isBlacklisted && !managementCommands.includes(command)) {
log(`Message in blacklisted channel ${message.channel.id} ignored.`);
return;
}
// * Ignore bot messages
if (message.author.bot) return;
if (message.content.startsWith(commandPrefix)) {
// ? Command handling logic
const [command, ...args] = message.content.slice(commandPrefix.length).trim().split(/\s+/);
if (!message.member.permissions.has('MANAGE_MESSAGES')) {
return message.reply('You do not have permission to use this command.');
}
// * Normal Commands *
if (command === 'view') {
const { embed, buttons } = createIgnoreWordsEmbed();
message.channel.send({ embeds: [embed], components: [buttons] });
console.log("View called\n" + `Ignore list: ${ignoreWords.join(', ')}`);
}
if (command === 'rm') {
const word = args[0];
ignoreWords = ignoreWords.filter(w => w !== word.toLowerCase());
saveIgnoreWords(); // Save updated list to file
message.reply(`Removed "${word}" from the ignore list.`);
console.log(`Removed "${word}" from the ignore list.`);
}
if (command === 'addi') {
const word = args[0];
if (word && !ignoreWords.includes(word.toLowerCase())) {
ignoreWords.push(word.toLowerCase());
saveIgnoreWords(); // Save updated list to file
message.reply(`Added "${word}" to the ignore list.`);
console.log(`Added "${word}" to the ignore list.`);
} else {
message.reply(`${word ? `"${word}" is already in the ignore list.` : "Please specify a word to add."}`);
}
}
// !!!!
// * Black List Commands *
if (command === 'addbl') {
const channelId = args[0];
if (!blacklist[message.guild.id].includes(channelId)) {
return message.reply(`Channel ${channelId} is not blacklisted.`);
}
blacklist[message.guild.id] = blacklist[message.guild.id].filter(id => id !== channelId);
saveBlacklistedChannels(); // Save updated blacklist to file
message.reply(`Channel <#${channelId}> has been removed from the blacklist.`);
console.log(`Channel ${channelId} has been removed from the blacklist.`);
}
if (command === 'viewbl') {
const blacklisted = blacklist[message.guild.id] || [];
if (blacklisted.length === 0) {
return message.reply('No channels are blacklisted in this server.');
}
let formattedChannels = '';
blacklisted.forEach((channelId, index) => {
const channel = message.guild.channels.cache.get(channelId);
formattedChannels += `${index + 1}. ${channel ? channel.name : `Unknown`} (ID: ${channelId})\n`;
});
const embed = new EmbedBuilder()
.setTitle('Blacklisted Channels')
.setDescription(`\`\`\`${formattedChannels}\`\`\``)
.setFooter({ text: `Total blacklisted channels: ${blacklisted.length}` });
message.channel.send({ embeds: [embed] });
}
if (command === 'rmbl') {
const channelId = args[0];
if (!blacklist[message.guild.id].includes(channelId)) {
return message.reply(`Channel ${channelId} is not blacklisted.`);
}
}
return; // Exit after handling a command
}
// Skip messages that contain ignored words
if (!shouldTranslate(message.content)) {
log('Message contains ignored words, skipping translation.');
return;
}
const cleanedContent = cleanMessage(message.content);
if (!cleanedContent) {
log('Message is empty after cleaning, skipping.');
return;
}
log(`Cleaned message content: ${cleanedContent}`);
const detectedLang = langdetect.detectOne(cleanedContent);
log(`Detected language: ${detectedLang}`);
if (detectedLang === 'en') {
log('Message is in English, skipping translation.');
return;
}
const { translatedText, flagUrl, languageName } = await translateText(cleanedContent);
log(`Translation result: ${translatedText}, Language: ${languageName}, Flag URL: ${flagUrl}`);
if (translatedText) {
const embed = new EmbedBuilder()
.setColor('#0099ff')
.setAuthor({ name: message.author.username, iconURL: message.author.displayAvatarURL() })
.setDescription(`${translatedText}`)
.setFooter({ text: `Original: ${message.content} | Language: ${languageName}` });
if (flagUrl) {
embed.setThumbnail(flagUrl);
}
await message.delete();
await message.channel.send({ embeds: [embed] });
log('Message translated and sent successfully.');
}
} catch (error) {
log(`Error processing message: ${error.stack || error.message}`);
console.error(error);
}
});
function shouldTranslate(messageContent) {
const words = messageContent.split(/\s+/);
return !words.some(word => ignoreWords.includes(word.toLowerCase()));
}
async function translateText(text) {
try {
log(`Attempting to translate text: ${text}`);
const response = await fetch(API_URL, {
method: "POST",
body: JSON.stringify({
q: text,
source: "auto",
target: "en",
format: "text"
}),
headers: { "Content-Type": "application/json" }
});
if (!response.ok) throw new Error(`Translation API responded with status ${response.status}`);
const data = await response.json();
log(`API Response: ${JSON.stringify(data)}`);
console.log(data);
// Extract the confidence score
const confidence = data.detectedLanguage?.confidence;
log(`Detected confidence: ${confidence}`);
// Skip translation if confidence is below 35%
if (confidence !== undefined && confidence < 35) {
log(`Translation skipped due to low confidence: ${confidence}`);
return { translatedText: null, flagUrl: null, languageName: null };
}
const languageCode = data.detectedLanguage.language;
const flagUrl = getFlagUrl(languageCode);
const languageName = getLanguageName(languageCode);
return { translatedText: data.translatedText, flagUrl, languageName };
}
catch (error) {
log(`Translation error: ${error.stack || error.message}`);
throw error; // Re-throw to be caught by the calling function
}
}
process.on('unhandledRejection', (reason, promise) => {
log(`Unhandled Rejection: ${reason.message || reason}`);
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
});
function getFlagUrl(languageCode) {
const flagMap = {
af: "https://flagcdn.com/w40/za.png", // Afrikaans (South Africa)
ar: "https://flagcdn.com/w40/sa.png", // Arabic (Saudi Arabia)
az: "https://flagcdn.com/w40/az.png", // Azerbaijani (Azerbaijan)
be: "https://flagcdn.com/w40/by.png", // Belarusian (Belarus)
bg: "https://flagcdn.com/w40/bg.png", // Bulgarian (Bulgaria)
bn: "https://flagcdn.com/w40/bd.png", // Bengali (Bangladesh)
cs: "https://flagcdn.com/w40/cz.png", // Czech (Czech Republic)
da: "https://flagcdn.com/w40/dk.png", // Danish (Denmark)
de: "https://flagcdn.com/w40/de.png", // German (Germany)
el: "https://flagcdn.com/w40/gr.png", // Greek (Greece)
es: "https://flagcdn.com/w40/es.png", // Spanish (Spain)
et: "https://flagcdn.com/w40/ee.png", // Estonian (Estonia)
fa: "https://flagcdn.com/w40/ir.png", // Persian (Iran)
fi: "https://flagcdn.com/w40/fi.png", // Finnish (Finland)
fr: "https://flagcdn.com/w40/fr.png", // French (France)
he: "https://flagcdn.com/w40/il.png", // Hebrew (Israel)
hi: "https://flagcdn.com/w40/in.png", // Hindi (India)
hr: "https://flagcdn.com/w40/hr.png", // Croatian (Croatia)
hu: "https://flagcdn.com/w40/hu.png", // Hungarian (Hungary)
id: "https://flagcdn.com/w40/id.png", // Indonesian (Indonesia)
it: "https://flagcdn.com/w40/it.png", // Italian (Italy)
ja: "https://flagcdn.com/w40/jp.png", // Japanese (Japan)
ka: "https://flagcdn.com/w40/ge.png", // Georgian (Georgia)
kk: "https://flagcdn.com/w40/kz.png", // Kazakh (Kazakhstan)
ko: "https://flagcdn.com/w40/kr.png", // Korean (South Korea)
lt: "https://flagcdn.com/w40/lt.png", // Lithuanian (Lithuania)
lv: "https://flagcdn.com/w40/lv.png", // Latvian (Latvia)
mk: "https://flagcdn.com/w40/mk.png", // Macedonian (North Macedonia)
mn: "https://flagcdn.com/w40/mn.png", // Mongolian (Mongolia)
ms: "https://flagcdn.com/w40/my.png", // Malay (Malaysia)
nb: "https://flagcdn.com/w40/no.png", // Norwegian (Norway)
nl: "https://flagcdn.com/w40/nl.png", // Dutch (Netherlands)
pl: "https://flagcdn.com/w40/pl.png", // Polish (Poland)
pt: "https://flagcdn.com/w40/pt.png", // Portuguese (Portugal)
ro: "https://flagcdn.com/w40/ro.png", // Romanian (Romania)
ru: "https://flagcdn.com/w40/ru.png", // Russian (Russia)
sk: "https://flagcdn.com/w40/sk.png", // Slovak (Slovakia)
sl: "https://flagcdn.com/w40/si.png", // Slovenian (Slovenia)
sq: "https://flagcdn.com/w40/al.png", // Albanian (Albania)
sr: "https://flagcdn.com/w40/rs.png", // Serbian (Serbia)
sv: "https://flagcdn.com/w40/se.png", // Swedish (Sweden)
th: "https://flagcdn.com/w40/th.png", // Thai (Thailand)
tr: "https://flagcdn.com/w40/tr.png", // Turkish (Turkey)
uk: "https://flagcdn.com/w40/ua.png", // Ukrainian (Ukraine)
vi: "https://flagcdn.com/w40/vn.png", // Vietnamese (Vietnam)
zh: "https://flagcdn.com/w40/cn.png", // Chinese (China)
};
return flagMap[languageCode] || null;
}
function getLanguageName(languageCode) {
const languageMap = {
af: "Afrikaans",
ar: "Arabic",
az: "Azerbaijani",
be: "Belarusian",
bg: "Bulgarian",
bn: "Bengali",
cs: "Czech",
da: "Danish",
de: "German",
el: "Greek",
es: "Spanish",
et: "Estonian",
fa: "Persian",
fi: "Finnish",
fr: "French",
he: "Hebrew",
hi: "Hindi",
hr: "Croatian",
hu: "Hungarian",
id: "Indonesian",
it: "Italian",
ja: "Japanese",
ka: "Georgian",
kk: "Kazakh",
ko: "Korean",
lt: "Lithuanian",
lv: "Latvian",
mk: "Macedonian",
mn: "Mongolian",
ms: "Malay",
nb: "Norwegian",
nl: "Dutch",
pl: "Polish",
pt: "Portuguese",
ro: "Romanian",
ru: "Russian",
sk: "Slovak",
sl: "Slovenian",
sq: "Albanian",
sr: "Serbian",
sv: "Swedish",
th: "Thai",
tr: "Turkish",
uk: "Ukrainian",
vi: "Vietnamese",
zh: "Chinese",
};
return languageMap[languageCode] || "Unknown Language";
}
client.login(process.env.BOT_TOKEN);