-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
840 lines (706 loc) · 32.8 KB
/
Copy pathbot.js
File metadata and controls
840 lines (706 loc) · 32.8 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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
const {
Client,
GatewayIntentBits,
Events,
Collection,
REST,
Routes,
SlashCommandBuilder,
InteractionType,
ChannelType,
PermissionsBitField,
AuditLogEvent
} = require('discord.js');
const { EmbedBuilder } = require('@discordjs/builders');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.GuildVoiceStates
],
partials: [
'MESSAGE',
'CHANNEL',
'REACTION'
]
});
require('dotenv').config();
const rest = new REST({ version: '10' }).setToken(process.env.LIVE_TOKEN);
// //
const { MilestoneLevel, Server, User, Punishment, ReactionRole } = require('./models/models.js');
const { logEvent, processLogs } = require('./events/logEvents');
const {
createEmbed
} = require('./commands/Utils_Functions/utils-embeds.js');
const {
manageRoles
} = require('./commands/Utils_Functions/utils-roles.js');
const {
getUserData,
getUserCount
} = require('./commands/Utils_Functions/utils-user.js');
const {
getServerData
} = require('./commands/Utils_Functions/utils-server.js');
const {
// Milestone Roles
checkAndGrantMilestoneRoles,
giveRoleToUserIfNoneArrange,
// Milestone Levels
isMilestoneLevel
} = require('./commands/Utils_Functions/utils-milestones.js');
const {
// Reaction Roles
saveReactionRole,
loadReactionRoles
} = require('./commands/Utils_Functions/utils-reactions.js');
// //
// Load Files
const adminCommands = require('./commands/admin_commands/admin_commands.js');
const communityCommands = require('./commands/community_commands/community_commands.js');
const configCommands = require('./commands/config_commands/configs_commands.js');
const { getReactionRoleConfigurations } = require('./commands/config_commands/configs_commands.js');
const milestoneCommands = require('./commands/milestone_commands/milestone_commands.js');
const help_menu_selected = require('./events/help_menu_selected.js');
const ownerCommands = require('./commands/owner_commands/owner_commands.js');
// //
// Load Commands
const commands = [
// Community Commands
new SlashCommandBuilder()
.setName('profile')
.setDescription('Check your profile or the profile of another user')
.addUserOption(option =>
option.setName('user')
.setDescription('The user to view stats for')
.setRequired(false)
),
new SlashCommandBuilder()
.setName('setbio')
.setDescription('Set your bio')
.addStringOption(option =>
option.setName('bio')
.setDescription('Your new bio')
.setRequired(true)
),
new SlashCommandBuilder()
.setName('help')
.setDescription('Get help with the bot'),
// //
// Admin Commands
new SlashCommandBuilder()
.setName('mod-ban')
.setDescription('Ban a user from the server')
.setDefaultMemberPermissions(PermissionsBitField.Flags.BanMembers)
.addUserOption(option =>
option.setName('user')
.setDescription('The user whose profile you want to view')
.setRequired(true))
.addStringOption(option =>
option.setName('reason')
.setDescription('The reason for the ban')
.setRequired(true)
),
new SlashCommandBuilder()
.setName('mod-unban')
.setDescription('Unban a user from the server')
.setDefaultMemberPermissions(PermissionsBitField.Flags.BanMembers)
.addUserOption(option =>
option.setName('user')
.setDescription('The user whose profile you want to view')
.setRequired(true))
.addStringOption(option =>
option.setName('reason')
.setDescription('The reason for the ban')
.setRequired(true)
),
// //
new SlashCommandBuilder()
.setName('mod-kick')
.setDescription('Kick a user from the server')
.setDefaultMemberPermissions(PermissionsBitField.Flags.KickMembers)
.addUserOption(option =>
option.setName('user')
.setDescription('The user to kick')
.setRequired(true)
)
.addStringOption(option =>
option.setName('reason')
.setDescription('The reason for kicking the user')
.setRequired(false)
),
// //
new SlashCommandBuilder()
.setName('mod-warn')
.setDescription('Warn a user in the server')
.setDefaultMemberPermissions(PermissionsBitField.Flags.BanMembers)
.addUserOption(option =>
option.setName('user')
.setDescription('The user whose profile you want to view')
.setRequired(true))
.addStringOption(option =>
option.setName('reason')
.setDescription('The reason for the ban')
.setRequired(true)
),
new SlashCommandBuilder()
.setName('mod-remove-warning')
.setDescription('Remove warning from user in server')
.setDefaultMemberPermissions(PermissionsBitField.Flags.BanMembers)
.addUserOption(option =>
option.setName('user')
.setDescription('The reason for the warn')
.setRequired(true)
),
// //
new SlashCommandBuilder()
.setName('mod-timeout')
.setDescription('Timeout a user for a specified duration')
.setDefaultMemberPermissions(PermissionsBitField.Flags.BanMembers)
.addUserOption(option =>
option.setName('user')
.setDescription('The user to timeout')
.setRequired(true))
.addIntegerOption(option =>
option.setName('duration')
.setDescription('The duration of the timeout in minutes (e.g., 10 for 10 minutes)')
.setRequired(true))
.addStringOption(option =>
option.setName('reason')
.setDescription('The reason for the timeout')
.setRequired(false)
),
// //
new SlashCommandBuilder()
.setName('mod-mute')
.setDescription('Mute a user in the server')
.setDefaultMemberPermissions(PermissionsBitField.Flags.BanMembers)
.addUserOption(option =>
option.setName('user')
.setDescription('The user whose profile you want to view')
.setRequired(true))
.addIntegerOption(option =>
option.setName('duration')
.setDescription('The duration of the timeout (10 = 10 minutes || 120 = 2 hours)')
.setRequired(true))
.addIntegerOption(option =>
option.setName('level')
.setDescription('The level of the mute (1 - No Talk, Can See || 2 - No Talk, No See)')
.setRequired(true))
.addStringOption(option =>
option.setName('reason')
.setDescription('The reason for the mute')
.setRequired(false)
),
new SlashCommandBuilder()
.setName('mod-unmute')
.setDescription('Unmute a user in the server')
.setDefaultMemberPermissions(PermissionsBitField.Flags.BanMembers)
.addUserOption(option =>
option.setName('user')
.setDescription('The user whose profile you want to view')
.setRequired(true)
)
.addStringOption(option =>
option.setName('reason')
.setDescription('The reason for the unmute')
.setRequired(false)
),
// //
// Configuration Setup Commands
new SlashCommandBuilder()
.setName('setup-mute-role')
.setDescription('Setup a mute role for the server')
.setDefaultMemberPermissions(PermissionsBitField.Flags.ManageRoles)
,
new SlashCommandBuilder()
.setName('setup-logging-channel')
.setDescription('Setup a logging channel for the server')
.setDefaultMemberPermissions(PermissionsBitField.Flags.ManageChannels)
.addChannelOption(option =>
option.setName('channel')
.setDescription('The channel to send logs to')
.setRequired(false)
),
new SlashCommandBuilder()
.setName('setup-welcome-channel')
.setDefaultMemberPermissions(PermissionsBitField.Flags.ManageChannels)
.setDescription('Sets the channel for welcoming new members')
.addChannelOption(option =>
option.setName('channel')
.setDescription('The channel to send welcome messages')
.setRequired(true)
),
new SlashCommandBuilder()
.setName('setup-reaction-role')
.setDefaultMemberPermissions(PermissionsBitField.Flags.ManageRoles)
.setDescription('Sets up a reaction role message')
.addChannelOption(option =>
option.setName('channel')
.setDescription('The channel where the reaction role message will be sent')
.setRequired(true)
),
new SlashCommandBuilder()
.setName('setup-milestone')
.setDescription('Set a milestone level and the role to be granted on reaching that level')
.setDefaultMemberPermissions(PermissionsBitField.Flags.ManageRoles)
.addIntegerOption(option =>
option.setName('level')
.setDescription('The level at which the milestone will be reached')
.setRequired(true))
.addRoleOption(option =>
option.setName('role')
.setDescription('The role to grant when reaching the milestone level')
.setRequired(true)),
new SlashCommandBuilder()
.setName('remove-milestone')
.setDescription('Remove a milestone level and its associated role')
.setDefaultMemberPermissions(PermissionsBitField.Flags.ManageRoles)
.addIntegerOption(option =>
option.setName('level')
.setDescription('The level at which the milestone will be reached')
.setRequired(true)),
new SlashCommandBuilder()
.setName('view-milestones')
.setDescription('View the milestone levels and their associated roles')
.setDefaultMemberPermissions(PermissionsBitField.Flags.ManageRoles),
// //
new SlashCommandBuilder()
.setName('setup-levelup-channel')
.setDefaultMemberPermissions(PermissionsBitField.Flags.ManageChannels)
.setDescription('Set the channel where level up messages will be sent')
.addChannelOption(option =>
option.setName('channel')
.setDescription('The channel to send level up messages to.')
.setRequired(true)
),
// //
new SlashCommandBuilder()
.setName('refresh-reactions')
.setDescription('Refresh the reaction roles if they stop working')
.setDefaultMemberPermissions(PermissionsBitField.Flags.ManageMessages),
// //
// * Owner Commands
new SlashCommandBuilder()
.setName('server-call')
.setDescription('Owner commands')
].map(command => command.toJSON());
// //
client.once('ready', async () => {
console.log(`Logged in as ${client.user.tag}!`);
try {
console.log('----------------\nStarted refreshing application (/) commands');
// Fetch all guilds the bot is in
const guilds = await client.guilds.fetch();
for (const guild of guilds.values()) {
try {
// Check if the server already exists in the database
const existingServer = await Server.findOne({ where: { serverId: guild.id } });
// If the server already exists, skip creation
if (existingServer) {
console.log(`Server ${guild.name} already exists in the database.`);
} else {
// Create default entries for the server
await Server.create({
serverId: guild.id,
serverName: guild.name,
textChannelId: null,
loggingChannelId: null,
welcomeChannelId: null,
rankUpChannelId: null,
logLevel: 'low',
mute_role_level_1_id: null,
mute_role_level_2_id: null
});
}
} catch (error) {
console.error(`Error adding guild to database: ${guild.name} (${guild.id})`, error);
}
}
// Load reaction roles before registering slash commands
await loadReactionRoles();
const reactionRoleConfigurations = getReactionRoleConfigurations();
console.log('Reaction role configurations loaded:', reactionRoleConfigurations);
// List all reaction roles to the console
for (const [guildId, configs] of reactionRoleConfigurations.entries()) {
for (const config of configs) {
try {
const guild = await client.guilds.fetch(guildId);
console.log(`Fetching channel ID: ${config.channelId} for guild ${guild.name}`);
const channel = await guild.channels.fetch(config.channelId); // Explicitly fetch the channel
if (!channel) {
console.log(`Channel not found: ${config.channelId} in guild ${guild.name}`);
continue;
}
const message = await channel.messages.fetch(config.messageId);
// Iterate through the reactions on the message using a proper async loop
for (const reaction of message.reactions.cache.values()) {
const users = await reaction.users.fetch();
for (const user of users.values()) {
if (!user.bot) {
console.log(`Reaction found from user ${user.tag} on message ${message.id}`);
}
}
}
} catch (error) {
console.error(`Error processing guild ${guildId} and config ${config}:`, error);
}
}
}
// Register slash commands for each guild dynamically
for (const guild of guilds.values()) {
try {
await rest.put(
Routes.applicationGuildCommands(client.user.id, guild.id),
{ body: commands }
);
console.log(`Successfully registered commands for guild: ${guild.id}`);
} catch (error) {
console.error(`Error registering commands for guild: ${guild.id}`, error);
}
}
console.log('Successfully reloaded application (/) commands');
} catch (error) {
console.error('An error occurred during initialization:', error);
}
});
client.on(Events.GuildCreate, async guild => {
try {
console.log(`Joined a new guild: ${guild.name} (ID: ${guild.id})`);
// Check if the server already exists in the database
const existingServer = await Server.findOne({ where: { serverId: guild.id } });
// If the server already exists, skip creation
if (existingServer) {
console.log(`Server ${guild.name} already exists in the database.`);
return;
} else {
// Create default entries for the server
await Server.create({
serverId: guild.id,
serverName: guild.name,
textChannelId: null,
loggingChannelId: null,
welcomeChannelId: null,
rankUpChannelId: null,
logLevel: 'low',
mute_role_level_1_id: null,
mute_role_level_2_id: null
});
}
console.log(`Default entries created for server ${guild.name} (ID: ${guild.id})`);
// You can also send a message to a system channel or owner of the server to say "Hello!"
const systemChannel = guild.systemChannel;
if (systemChannel) {
systemChannel.send('Hello! Thank you for inviting me! Use `/help` to see what I can do!');
}
} catch (error) {
console.error(`Error creating default server entry for ${guild.name}:`, error);
}
});
client.on(Events.GuildDelete, async guild => {
try {
console.log(`Bot was removed from: ${guild.name} (ID: ${guild.id})`);
await Server.destroy({ where: { serverId: guild.id } });
await User.destroy({ where: { guildId: guild.id } });
await MilestoneLevel.destroy({ where: { guildId: guild.id }});
await Punishment.destroy({ where: { guildId: guild.id } });
console.log(`Removed data for guild: ${guild.name}`);
} catch (error) {
console.error(`Error handling guildDelete for guild ${guild.name}:`, error);
}
});
// //
client.on('guildMemberAdd', async (member) => {
if (member.user.bot) return;
// Fetch the server entry to get the welcome channel ID
const server = await Server.findOne({ where: { serverId: member.guild.id } });
if (!server?.welcomeChannelId) return; // No welcome channel set
// Get the welcome channel
const welcomeChannel = member.guild.channels.cache.get(server.welcomeChannelId);
if (!welcomeChannel || welcomeChannel.type !== 0) return;
// Build the welcome message based on your reference
const welcomeEmbed = new EmbedBuilder()
.setTitle(`YAY! Welcome to ${member.guild.name} ${member.user.displayName}!`)
.setDescription(`
Yayyy! ${member.user}-sama has joined the fight to defend Earth with Son Goku and Sailor Moon.
\nWe now have ${member.guild.memberCount} warriors to join the fight! But are you a Saiyan, a Senshi, or both?
\n\n${member.user}-sama, please select your roles to identify your training grounds, your identification, and other things Goku and Usagi will need to know (they are a bit clueless).`)
.setColor(0x008080)
.setImage('https://tenor.com/en-GB/view/kids-goku-peace-cool-shades-son-goku-gif-16874131.gif')
.setFooter({ text: `Welcome to ${member.guild.name}` });
// Send the message in the welcome channel
try {
await welcomeChannel.send({ embeds: [welcomeEmbed] });
} catch (err) {
console.error('Error sending welcome message:', err);
}
});
client.on('guildMemberRemove', async (member) => {
// If user is a bot, skip
if (member.user.bot) return;
const kickAuditLogs = await member.guild.fetchAuditLogs({
limit: 1,
type: AuditLogEvent.MemberKick
});
const banAuditLogs = await member.guild.fetchAuditLogs({
limit: 1,
type: AuditLogEvent.MemberBanAdd
});
const kickLog = kickAuditLogs.entries.first();
const banLog = banAuditLogs.entries.first();
const currentTime = Date.now();
const wasKicked = kickLog && kickLog.target.id === member.id && currentTime - kickLog.createdTimestamp < 5000;
const wasBanned = banLog && banLog.target.id === member.id && currentTime - banLog.createdTimestamp < 5000;
if (wasKicked || wasBanned) return;
// Fetch the server entry to get the goodbye channel ID
const server = await Server.findOne({ where: { serverId: member.guild.id } });
if (!server?.welcomeChannelId) return; // No goodbye channel set
// Get the goodbye channel
const goodbyeChannel = member.guild.channels.cache.get(server.welcomeChannelId);
if (!goodbyeChannel || goodbyeChannel.type !== 0) return; // Invalid channel
// Build the goodbye message
const goodbyeEmbed = new EmbedBuilder()
.setTitle(`${member.user.displayName}-san has left us...`) // Customize the title as you like
.setDescription(`
O-oh... ... looks like <@${member.user.id}>-sama has left the fight to defend Earth with Son Goku and Sailor Moon. As they go to rest to King Kai, we hope they'll reincarnate and come back better than last time!
\n\n${member.user.displayName} will be remembered...
\nWe are now left with **${member.guild.memberCount} senshi warriors** to continue the fight.`)
.setColor(0x008080) // You can change the color
.setImage('https://tenor.com/en-GB/view/sailor-moon-sad-anime-alone-gif-17542952.gif') // Use a goodbye GIF
.setFooter({ text: `Goodbye from ${member.guild.name}` });
// Send the message in the goodbye channel
try {
await goodbyeChannel.send({ embeds: [goodbyeEmbed] });
} catch (err) {
console.error('Error sending goodbye message:', err);
}
});
// //
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand() && interaction.componentType !== 3) return;
const { commandName, options, guildId } = interaction;
// Community commands
if (commandName === 'profile') { console.log(`profile command ran`); await communityCommands.profile.execute(interaction); }
if (commandName ==='setbio') { console.log(`setbio command ran`); await communityCommands.setBio.execute(interaction, options); }
// Admin commands
if (commandName === 'mod-ban') { console.log(`ban command ran`); await adminCommands.ban.execute(interaction, options); }
if (commandName === 'mod-unban') { console.log(`unban command ran`); await adminCommands.unban.execute(interaction, options) }
// //
if (commandName === 'mod-kick') { console.log(`kick command ran`); await adminCommands.kick.execute(interaction, options); }
// //
if (commandName === 'mod-warn') { console.log(`warn command ran`); await adminCommands.warn.execute(interaction, options); }
if (commandName ==='mod-remove-warning') { console.log(`rm warn command ran`); await adminCommands.removeWarning.execute(interaction, options); }
// //
if (commandName === 'mod-timeout') { console.log(`timeout command ran`); await adminCommands.timeout.execute(client, interaction, options, guildId); }
// //
if (commandName === 'mod-mute') { console.log(`mute command ran`); await adminCommands.mute.execute(interaction, options, guildId); }
if (commandName === 'mod-unmute') { console.log(`unmute command ran`); await adminCommands.unmute.execute(interaction, options); }
// //
if (commandName === 'setup-mute-role') { console.log(`setup mute command ran`); await configCommands.setupMuteRole.execute(interaction, options); }
if (commandName ==='setup-logging-channel') { console.log(`setup log command ran`); await configCommands.setupLoggingChannel.execute(interaction, options); }
if (commandName === 'setup-welcome-channel') { console.log(`setup welcome command ran`); await configCommands.setupWelcomeChannel.execute(interaction, options); }
if (commandName === 'setup-reaction-role') { console.log(`setup reaction command ran`); await configCommands.setupReactionRole.execute(interaction, options); }
if (commandName === 'setup-milestone') { console.log(`setup milestone command ran`); await milestoneCommands.setupMilestone.execute(interaction, options); }
if (commandName === 'setup-levelup-channel') { console.log(`setup levelup command ran`); await configCommands.setLevelupChannel.execute(interaction, options); }
// //
if (commandName === 'refresh-reactions') { console.log(`refresh reaction command ran`); await adminCommands.refreshReactions.execute(interaction); }
// //
if (commandName === 'remove-milestone') { console.log(`rm milestone command ran`); await milestoneCommands.removeMilestone.execute(interaction, options); }
if (commandName === 'view-milestones') { console.log(`list milestone command ran`); await milestoneCommands.viewMilestones.execute(interaction); }
// //
if (commandName === 'help') { console.log(`help command ran`); await communityCommands.help.execute(interaction); }
// //
if (commandName === 'server-call') { console.log(`server call command ran`); await ownerCommands.serverCall.execute(interaction); }
if (interaction.customId === 'help_menu') { await help_menu_selected.help_menu_selected.execute(interaction) }
});
// //
// When message is created, add xp
const cooldowns = new Map();
client.on('messageCreate', async (message) => {
try {
// Skip if user is bot
console.log('Message Create Event Triggered');
if (message.author.bot) return;
console.log('Processing user message...');
const userId = message.author.id;
const guildId = message.guild.id;
// Fetch or create user data from the database
let userData = await getUserData(guildId, userId);
try {
if (!userData) {
userData = await User.create({
userId: userId,
username: message.author.username,
guildId: guildId,
bio: null,
level: 0,
xp: 0,
warnings: null,
});
}
} catch(err) {
console.error('Error getting or creating user data:', err);
}
await giveRoleToUserIfNoneArrange(message.member, guildId, userData.level);
// Check for cooldowns (60 seconds)
const now = Date.now();
const cooldownAmount = 60 * 1000; // Cooldown set to 1 minute
if (cooldowns.has(userId)) {
const expirationTime = cooldowns.get(userId) + cooldownAmount;
if (now < expirationTime) {
const timeLeft = (expirationTime - now) / 1000;
console.log(`User ${message.author.username} is on cooldown. ${timeLeft.toFixed(1)} seconds remaining.`);
return;
}
}
// Set new cooldown
cooldowns.set(userId, now);
// Gain XP (5-10 XP randomly)
const xpGain = Math.floor(Math.random() * 5) + 5;
// Add XP and message count
userData.xp += xpGain;
userData.totalMessages += 1;
// Level calculation logic
const level = userData.level;
const baseMultiplier = 100; // Base XP multiplier
const scalingFactor = 1.1; // Scaling factor for level progression
// XP needed for current and next level
const xpNeededForCurrentLevel = Math.floor(level * baseMultiplier * Math.pow(scalingFactor, level));
const xpNeededForNextLevel = Math.floor((level + 1) * baseMultiplier * Math.pow(scalingFactor, level + 1));
const xpForNextLevel = xpNeededForNextLevel - xpNeededForCurrentLevel;
if (userData.xp >= xpForNextLevel) {
userData.level += 1;
userData.xp = 0; // Reset XP after leveling up
await checkAndGrantMilestoneRoles(message.member, guildId, userData.level, message);
}
// Save updated user data to the database
console.log("Processing user message complete");
await userData.save();
} catch (error) {
console.error('Error handling message:', error);
}
});
// //
client.on(Events.MessageReactionAdd, async (reaction, user) => {
// // Check if the reaction is in a guild and not from a bot
if (user.bot) return;
try {
// Fetch partial reactions and messages
if (reaction.partial) await reaction.fetch();
if (reaction.message.partial) await reaction.message.fetch();
} catch (err) {
console.error('Error fetching partial reaction or message:', err);
return; // Skip further processing if fetch fails
}
const guildId = reaction.message.guild.id;
try {
// Fetch all reaction roles for this message
const existingRoles = await ReactionRole.findAll({
where: {
guildId: guildId,
messageId: reaction.message.id,
},
});
// If no reaction roles exist for this message, return
if (!existingRoles || existingRoles.length === 0) {
console.log(`No reaction role configuration found for message ID: ${reaction.message.id} in guild: ${guildId}`);
return;
}
// Get the emoji identifier (custom vs standard)
const emojiIdentifier = reaction.emoji.id ? `<:${reaction.emoji.name}:${reaction.emoji.id}>` : reaction.emoji.name;
// Find the role associated with the emoji
const roleEntry = existingRoles.find(entry => entry.emoji === emojiIdentifier);
if (!roleEntry) {
console.log(`No role associated with emoji ${emojiIdentifier} for message ID: ${reaction.message.id}`);
return;
}
// Get the role from the guild
const role = reaction.message.guild.roles.cache.get(roleEntry.roleId);
if (!role) {
console.log(`Role not found in guild ${guildId} for role ID: ${roleEntry.roleId}`);
return;
}
// Fetch the member who reacted
const member = await reaction.message.guild.members.fetch(user.id);
// Fetch the bot's highest role to compare with the role to be added
const botMember = await reaction.message.guild.members.fetch(client.user.id);
const botHighestRole = botMember.roles.highest;
// Check if the bot has permission to manage roles and if its role is higher than the target role
if (!reaction.message.guild.members.me.permissions.has(PermissionsBitField.Flags.ManageRoles)) {
console.log(`Bot lacks 'Manage Roles' permission in guild ${guildId}`);
return;
}
if (botHighestRole.comparePositionTo(role) <= 0) {
console.log(`Bot's role (${botHighestRole.name}) is not higher than the role to be added (${role.name})`);
return; // Prevent adding the role to avoid the permission error
}
// Add the role to the member
await member.roles.add(role);
console.log(`Added role ${role.name} to user ${user.username}`);
} catch (error) {
console.error('Error adding role based on reaction:', error);
return;
}
});
client.on(Events.MessageReactionRemove, async (reaction, user) => {
// Check if the reaction is in a guild and not from a bot
if (reaction.message.partial) await reaction.message.fetch();
if (reaction.partial) await reaction.fetch();
if (user.bot) return;
if (!reaction.message.guild) return;
const guildId = reaction.message.guild.id;
try {
// Fetch all reaction roles for this message
const existingRoles = await ReactionRole.findAll({
where: {
guildId: guildId,
messageId: reaction.message.id,
},
});
// If no reaction roles exist for this message, return
if (!existingRoles || existingRoles.length === 0) {
console.log(`No reaction role configuration found for message ID: ${reaction.message.id} in guild: ${guildId}`);
return;
}
// Get the emoji identifier (custom vs standard)
const emojiIdentifier = reaction.emoji.id ? `<:${reaction.emoji.name}:${reaction.emoji.id}>` : reaction.emoji.name;
// Find the role associated with the emoji
const roleEntry = existingRoles.find(entry => entry.emoji === emojiIdentifier);
if (!roleEntry) {
console.log(`No role associated with emoji ${emojiIdentifier} for message ID: ${reaction.message.id}`);
return;
}
// Get the role from the guild
const role = reaction.message.guild.roles.cache.get(roleEntry.roleId);
if (!role) {
console.log(`Role not found in guild ${guildId} for role ID: ${roleEntry.roleId}`);
return;
}
// Fetch the member who reacted
const member = await reaction.message.guild.members.fetch(user.id);
// Fetch the bot's highest role to compare with the role to be added
const botMember = await reaction.message.guild.members.fetch(client.user.id);
const botHighestRole = botMember.roles.highest;
// Check if the bot has permission to manage roles and if its role is higher than the target role
if (!reaction.message.guild.members.me.permissions.has(PermissionsBitField.Flags.ManageRoles)) {
console.log(`Bot lacks 'Manage Roles' permission in guild ${guildId}`);
return;
}
if (botHighestRole.comparePositionTo(role) <= 0) {
console.log(`Bot's role (${botHighestRole.name}) is not higher than the role to be added (${role.name})`);
return; // Prevent adding the role to avoid the permission error
}
// Add the role to the member
await member.roles.remove(role);
console.log(`Added role ${role.name} to user ${user.username}`);
} catch (error) {
console.error('Error adding role based on reaction:', error);
return;
}
});
// //
setInterval(() => processLogs(client), 180000);
client.login(process.env.LIVE_TOKEN);