-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
583 lines (501 loc) · 22 KB
/
Copy pathmain.js
File metadata and controls
583 lines (501 loc) · 22 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
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
// main.js - PrismGuardian Fluxer Bot
const { Client, GatewayDispatchEvents } = require('@discordjs/core');
const { REST } = require('@discordjs/rest');
const { WebSocketManager } = require('@discordjs/ws');
require('dotenv').config();
const fs = require('fs');
const path = require('path');
const { handleFlagReaction } = require('./reactionHandler');
// ==================== SETUP ====================
const token = process.env.FLUXER_TOKEN;
if (!token) {
throw new Error('You forgot the FLUXER_TOKEN in your .env file!');
}
const PREFIX = process.env.PREFIX || '!';
const FLUXER_API = 'https://api.fluxer.app';
const API_VERSION = '1';
// ==================== INITIALIZE CLIENT ====================
const rest = new REST({ api: FLUXER_API, version: API_VERSION }).setToken(token);
const gateway = new WebSocketManager({
intents: 0,
rest,
token,
version: API_VERSION,
});
const client = new Client({ rest, gateway });
// ==================== INITIALIZE DATABASE ====================
const db = require('./db');
// ==================== COMMAND LOADER ====================
const commands = new Map();
async function loadCommands() {
const commandsPath = path.join(__dirname, 'commands');
// Check if commands folder exists
if (!fs.existsSync(commandsPath)) {
console.warn('⚠️ Commands folder not found! Creating /commands directory...');
fs.mkdirSync(commandsPath, { recursive: true });
console.log('📁 Created /commands folder - add your command files there!\n');
return;
}
// Read all .js files in /commands
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
if (commandFiles.length === 0) {
console.warn('⚠️ No command files found in /commands folder\n');
return;
}
console.log(`\n📂 Loading ${commandFiles.length} command file(s)...\n`);
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
try {
// Clear require cache to allow hot-reloading
delete require.cache[require.resolve(filePath)];
const command = require(filePath);
// Validate command structure
if (!command.name || !command.execute) {
console.warn(` ⚠️ Skipping ${file}: Missing 'name' or 'execute' property`);
continue;
}
commands.set(command.name, command);
// Show aliases if any
const aliases = command.aliases ? ` (aliases: ${command.aliases.join(', ')})` : '';
console.log(` ✅ Loaded: ${command.name}${aliases}`);
} catch (error) {
console.error(` ❌ Error loading ${file}:`, error.message);
}
}
console.log(`\n✨ Successfully loaded ${commands.size} command(s)\n`);
}
// ==================== READY EVENT ====================
// ==================== READY EVENT ====================
client.on(GatewayDispatchEvents.Ready, async ({ data }) => {
const { username, discriminator } = data.user;
console.log('\n╔═══════════════════════════════════════╗');
console.log('║ 🌈 PrismGuardian is Online! 🛡️ ║');
console.log('╚═══════════════════════════════════════╝\n');
console.log(`📊 Bot: ${username}#${discriminator}`);
console.log(`🎯 Prefix: ${PREFIX}`);
console.log(`📝 Commands loaded: ${commands.size}`);
console.log(`✨ Ready to go!\n`);
// Initialize database if not already done
if (!db.initialized) {
try {
await db.initialize();
console.log('🗄️ Database ready\n');
} catch (error) {
console.error('⚠️ Database initialization failed, some features may not work\n');
}
}
});
// ==================== TRACK WELCOME MESSAGES ====================
let botStartTime = Date.now();
// ==================== GUILD CREATE EVENT (Bot joins server) ====================
client.on(GatewayDispatchEvents.GuildCreate, async ({ api, data }) => {
const guild = data;
// Don't send welcome message on startup (within 5 seconds of bot start)
const timeSinceStart = Date.now() - botStartTime;
if (timeSinceStart < 5000) {
console.log(`⏭️ Skipping welcome message for ${guild.name} (startup)`);
return;
}
// Check database if we've already sent welcome to this server
const alreadySent = await db.hasWelcomeMessageSent(guild.id);
if (alreadySent) {
console.log(`⏭️ Welcome already sent to ${guild.name}`);
return;
}
console.log(`\n✨ Joined new server: ${guild.name} (${guild.id})`);
try {
// Get the default channel (usually #general)
const channels = await api.guilds.getChannels(guild.id);
let defaultChannel = null;
// Try to find #general or first text channel
for (const channel of channels) {
if (channel.name === 'general' || channel.type === 0) {
defaultChannel = channel;
break;
}
}
if (!defaultChannel) {
console.warn(`⚠️ Could not find suitable channel for welcome message in ${guild.name}`);
return;
}
// Send welcome embed
const welcomeEmbed = {
title: '👋 Welcome to PrismGuardian!',
description: 'Advanced AI-powered moderation bot protecting your community from spam, hate speech, and inappropriate content.',
color: 5793266, // Purple
fields: [
{
name: '🚀 Quick Start',
value: `\`${PREFIX}help\` - View all commands\n\`${PREFIX}config view\` - See filter settings`
},
{
name: '🛡️ What We Protect Against',
value: 'Hate speech • Profanity • Spam • Sexual content • Racist content • NSFW'
},
{
name: '⚙️ Setup',
value: `Use \`${PREFIX}config set <filter> allow\` to disable a filter\nUse \`${PREFIX}config logs <channel>\` to set mod logs`
}
],
footer: {
text: 'PrismGuardian v1.0 | Type !help for more info'
},
timestamp: new Date().toISOString()
};
const response = await api.channels.createMessage(defaultChannel.id, {
embeds: [welcomeEmbed]
});
// Record in database
await db.recordWelcomeMessage(guild.id, response.id);
console.log(`✅ Welcome message sent to ${guild.name}`);
} catch (error) {
console.error(`❌ Error sending welcome message:`, error.message);
}
});
// ==================== INITIALIZE AUTOMOD ====================
const textLogic = require('./autoMod/textLogic.js');
// ==================== REACTION HANDLER ====================
client.on(GatewayDispatchEvents.MessageReactionAdd, async ({ api, data }) => {
await handleFlagReaction(api, data, data.user_id, true);
});
client.on(GatewayDispatchEvents.MessageReactionRemove, async ({ api, data }) => {
await handleFlagReaction(api, data, data.user_id, false);
});
// ==================== MESSAGE CREATE EVENT ====================
client.on(GatewayDispatchEvents.MessageCreate, async ({ api, data }) => {
const message = data;
// Ignore bot messages
if (message.author.bot) {
return;
}
// Ignore DMs (only moderate in guilds)
if (!message.guild_id) {
return;
}
try {
// ==================== COMMAND HANDLER ====================
if (message.content.startsWith(PREFIX)) {
// Parse command and arguments
const args = message.content.slice(PREFIX.length).trim().split(/ +/);
const commandName = args.shift().toLowerCase();
// Try to find command by name or alias
let command = commands.get(commandName);
if (!command) {
// Check aliases
for (const [, cmd] of commands) {
if (cmd.aliases && cmd.aliases.includes(commandName)) {
command = cmd;
break;
}
}
}
if (!command) {
console.log(`❌ Unknown command: ${commandName}`);
await api.channels.createMessage(message.channel_id, {
content: `❌ Unknown command: \`${commandName}\`\nType \`${PREFIX}help\` for available commands`,
message_reference: { message_id: message.id },
});
return;
}
console.log(`📝 ${message.author.username} used: ${commandName}`);
// Execute command
try {
await command.execute({
api,
message,
args,
prefix: PREFIX,
});
} catch (error) {
console.error(`❌ Error executing command ${commandName}:`, error.message);
await api.channels.createMessage(message.channel_id, {
content: `❌ Error executing command: ${error.message}`,
message_reference: { message_id: message.id },
}).catch(() => null);
}
return;
}
// ==================== AUTO-MODERATION ====================
try {
const settings = await db.getModerationsSettings(message.guild_id);
if (!settings) {
return; // No settings configured
}
// Check if any filters are enabled
const hasActiveFilters = settings.nsfw || settings.sexual || settings.racist ||
settings.hate || settings.profanity || settings.spam;
if (!hasActiveFilters) {
return; // No filters enabled
}
// Run text moderation (Regex > Toxic-BERT > Groq)
const modResult = await textLogic.moderateText(message, api, db);
if (!modResult.safe && !modResult.alreadyLogged) {
// Message wasn't already handled by regex/patterns
console.log(`⚠️ VIOLATION: ${message.author.username} - ${modResult.reason} (${modResult.filterType})`);
// Delete the message
try {
await api.channels.deleteMessage(message.channel_id, message.id);
} catch (e) {
// Already deleted
}
// Send warning DM to user as embed
try {
const dmChannel = await api.users.createDM(message.author.id);
const severityIcon = modResult.severity === 'CRITICAL' ? '🚨' :
modResult.severity === 'HIGH' ? '⚠️' : '📋';
const dmEmbed = {
title: `${severityIcon} Message Removed`,
description: `Your message in the server was removed for violating community guidelines.`,
color: modResult.severity === 'CRITICAL' ? 15158332 : modResult.severity === 'HIGH' ? 15105570 : 9807270,
fields: [
{
name: '❌ Reason',
value: modResult.reason
},
{
name: '⚡ Severity',
value: modResult.severity || 'MEDIUM'
}
],
footer: {
text: 'Please follow the server\'s content policies'
},
timestamp: new Date().toISOString()
};
await api.channels.createMessage(dmChannel.id, {
embeds: [dmEmbed]
});
} catch (e) {
// DM failed
}
try {
await db.addInfraction(
message.author.id,
message.guild_id,
'automod',
modResult.reason,
'bot_automod',
true // automated
);
// Get updated infraction count and apply progressive punishment
const infractions = await db.getUserInfractions(message.author.id, message.guild_id, 100);
const violationCount = infractions.length;
console.log(`📊 ${message.author.username} has ${violationCount} infraction(s)`);
// Check if user is new (account created less than 7 days ago)
// Note: Fluxer may not provide created_timestamp, so default to not new
const accountAgeMs = message.author.created_timestamp ? Date.now() - message.author.created_timestamp : Date.now();
const accountAgeDays = accountAgeMs / (1000 * 60 * 60 * 24);
const isNewUser = accountAgeMs && accountAgeDays < 7;
console.log(`📅 ${message.author.username} account age: ${isNewUser ? accountAgeDays.toFixed(1) : 'unknown'} days${isNewUser ? ' (NEW USER)' : ''}`);
// Progressive punishment thresholds (stricter for new users)
// NOTE: Automatic punishments disabled for now - bot needs higher permissions
// Just log what punishment WOULD be applied
let punishment = null;
let punishmentTime = null;
if (isNewUser) {
// New users get stricter punishment
if (violationCount >= 10) {
punishment = 'ban';
} else if (violationCount >= 5) {
punishment = 'kick';
} else if (violationCount >= 2) {
punishment = 'mute';
punishmentTime = 10 * 60 * 1000; // 10 minutes
}
} else {
// Regular users have more lenient thresholds
if (violationCount >= 20) {
punishment = 'ban';
} else if (violationCount >= 10) {
punishment = 'kick';
} else if (violationCount >= 5) {
punishment = 'mute';
punishmentTime = 60 * 60 * 1000; // 1 hour
} else if (violationCount >= 3) {
punishment = 'mute';
punishmentTime = 10 * 60 * 1000; // 10 minutes
}
}
// Apply punishment
if (punishment) {
console.log(`🔨 Applying punishment: ${punishment} to ${message.author.username}`);
try {
if (punishment === 'ban') {
// Ban the user
if (api.guilds.createBan) {
await api.guilds.createBan(message.guild_id, message.author.id, {
reason: `Auto-mod: ${violationCount} infractions`
});
} else if (api.guilds.banUser) {
await api.guilds.banUser(message.guild_id, message.author.id);
}
console.log(`✅ ${message.author.username} has been banned`);
// Send ban DM
try {
const dmChannel = await api.users.createDM(message.author.id);
const banEmbed = {
title: '🔨 You Have Been Banned',
description: `You have been permanently banned from the server for violating community guidelines.`,
color: 15158332,
fields: [
{ name: '❌ Reason', value: `${violationCount} infractions (auto-mod)` },
{ name: '⚠️ Next Step', value: 'You cannot rejoin this server' }
],
timestamp: new Date().toISOString()
};
await api.channels.createMessage(dmChannel.id, { embeds: [banEmbed] });
} catch (e) {
console.warn('Could not DM banned user');
}
} else if (punishment === 'kick') {
// Kick the user
await api.guilds.removeMember(message.guild_id, message.author.id);
console.log(`✅ ${message.author.username} has been kicked`);
// Send kick DM
try {
const dmChannel = await api.users.createDM(message.author.id);
const kickEmbed = {
title: '👢 You Have Been Kicked',
description: `You have been removed from the server for violating community guidelines.`,
color: 15105570,
fields: [
{ name: '❌ Reason', value: `${violationCount} infractions (auto-mod)` },
{ name: '⚠️ Next Step', value: 'You can rejoin the server with a new invite' }
],
timestamp: new Date().toISOString()
};
await api.channels.createMessage(dmChannel.id, { embeds: [kickEmbed] });
} catch (e) {
console.warn('Could not DM kicked user');
}
} else if (punishment === 'mute') {
// Mute the user
const until = new Date(Date.now() + punishmentTime).toISOString();
await api.guilds.editMember(message.guild_id, message.author.id, {
communication_disabled_until: until
});
const minutes = punishmentTime / 1000 / 60;
console.log(`✅ ${message.author.username} has been muted for ${minutes} minutes`);
// Send mute DM
try {
const dmChannel = await api.users.createDM(message.author.id);
const muteEmbed = {
title: '🔇 You Have Been Muted',
description: `You have been muted for violating community guidelines.`,
color: 9807270,
fields: [
{ name: '❌ Reason', value: `${violationCount} infractions (auto-mod)` },
{ name: '⏱️ Duration', value: `${minutes} minute(s)` },
{ name: '⚠️ Next Step', value: `You will be able to chat again after the mute expires` }
],
timestamp: new Date().toISOString()
};
await api.channels.createMessage(dmChannel.id, { embeds: [muteEmbed] });
} catch (e) {
console.warn('Could not DM muted user');
}
}
} catch (punishErr) {
console.error(`❌ Failed to apply ${punishment}:`, punishErr.message);
}
}
} catch (e) {
console.error('Error adding infraction:', e.message);
}
// Log message removal to mod channel (with punishment info if applied)
if (settings.log_channel) {
try {
const severityIcon = modResult.severity === 'CRITICAL' ? '🚨' :
modResult.severity === 'HIGH' ? '⚠️' : '📋';
const infractions = await db.getUserInfractions(message.author.id, message.guild_id, 100);
const violationCount = infractions.length;
// Determine what punishment was/will be applied
let punishmentText = 'None (Warning only)';
const accountAgeMs = message.author.created_timestamp ? Date.now() - message.author.created_timestamp : Date.now();
const accountAgeDays = accountAgeMs / (1000 * 60 * 60 * 24);
const isNewUser = accountAgeMs && accountAgeDays < 7;
if (isNewUser) {
if (violationCount >= 10) punishmentText = 'Ban';
else if (violationCount >= 5) punishmentText = 'Kick';
else if (violationCount >= 2) punishmentText = '10-min Mute';
} else {
if (violationCount >= 20) punishmentText = 'Ban';
else if (violationCount >= 10) punishmentText = 'Kick';
else if (violationCount >= 5) punishmentText = '1-hour Mute';
else if (violationCount >= 3) punishmentText = '10-min Mute';
}
const logEmbed = {
title: `${severityIcon} Message Removed`,
description: `A message was removed for violating community guidelines`,
color: modResult.severity === 'CRITICAL' ? 15158332 : modResult.severity === 'HIGH' ? 15105570 : 9807270,
fields: [
{ name: '👤 User', value: `${message.author.username} (${message.author.id})`, inline: true },
{ name: '❌ Reason', value: modResult.reason, inline: true },
{ name: '📊 Total Infractions', value: `${violationCount}`, inline: true },
{ name: '🔨 Punishment Applied', value: punishmentText, inline: true },
{ name: '📝 Message Content', value: `\`\`\`${message.content.substring(0, 100)}\`\`\`` }
],
footer: { text: 'Auto-moderation' },
timestamp: new Date().toISOString()
};
await api.channels.createMessage(settings.log_channel, { embeds: [logEmbed] });
} catch (e) {
console.error('Error logging to mod channel:', e.message);
}
}
}
// ==================== IMAGE MODERATION ====================
// Disabled for now - Fluxer message structure differs from Discord.js
// Re-enable after understanding Fluxer's attachment handling
/*
try {
const imageResult = await imageDetector.moderateImage(message, api, db);
if (imageResult.hasImages && !imageResult.safe) {
console.log(`🚫 NSFW Image detected: ${message.author.username}`);
}
} catch (error) {
console.error('Error in image moderation:', error.message);
}
*/
} catch (error) {
console.error('Error in auto-moderation:', error.message);
}
} catch (error) {
console.error('Error in message handler:', error.message);
}
});
client.on(GatewayDispatchEvents.GuildMemberAdd, async ({ api, data }) => {
try {
await db.recordUserJoin(data.user.id, data.guild_id);
console.log(`✅ Recorded join: ${data.user.username} in guild ${data.guild_id}`);
} catch (error) {
console.error('Error recording user join:', error.message);
}
});
// ==================== STARTUP ====================
async function start() {
try {
console.log('🚀 Starting PrismGuardian...\n');
// Load commands
await loadCommands();
// Connect to gateway
await gateway.connect();
} catch (error) {
console.error('❌ Failed to start bot:', error.message);
process.exit(1);
}
}
start();
// ==================== GRACEFUL SHUTDOWN ====================
process.on('SIGINT', async () => {
console.log('\n\n👋 Shutting down gracefully...');
await db.close();
gateway.destroy();
process.exit(0);
});
process.on('SIGTERM', async () => {
console.log('\n\n👋 Shutting down gracefully...');
await db.close();
gateway.destroy();
process.exit(0);
});