-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathraid.js
More file actions
1058 lines (894 loc) · 38.6 KB
/
raid.js
File metadata and controls
1058 lines (894 loc) · 38.6 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
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const { EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle, StringSelectMenuBuilder, StringSelectMenuOptionBuilder, ModalBuilder, TextInputBuilder, TextInputStyle } = require('discord.js');
const fs = require('fs').promises;
const path = require('path');
// Store active raid signups (raidId -> raid data)
const activeRaids = new Map();
const raidSignups = new Map(); // raidId -> Map of userId -> signup data
const currentRaidId = 'dragon_shackles_current'; // Use a consistent ID for the current raid
const raidMetadata = new Map(); // raidId -> { title, times, type, raid }
// Raid configuration
const RAID_CONFIG = {
minPlayers: 20,
healerSlots: 4,
tankSlots: 4,
dpsSlots: 12,
videoGuide: 'https://www.youtube.com/watch?v=m2Z3BJcwRdo'
};
// Store voting data (raidId -> voting data)
const raidVoting = new Map(); // raidId -> { votes: Map<optionNumber, Set<username>> }
// File paths for persistence
const DATA_DIR = path.join(__dirname, 'data');
const SIGNUPS_FILE = path.join(DATA_DIR, 'raid_signups.json');
const VOTING_FILE = path.join(DATA_DIR, 'raid_voting.json');
const METADATA_FILE = path.join(DATA_DIR, 'raid_metadata.json');
// Initialize data directory
async function initializeDataDirectory() {
try {
await fs.mkdir(DATA_DIR, { recursive: true });
console.log('[RAID] Data directory initialized');
} catch (error) {
console.error('[RAID] Error creating data directory:', error);
}
}
// Save raid data to files
async function saveRaidData() {
try {
// Convert Maps to serializable objects
const signupsData = {};
for (const [raidId, signups] of raidSignups.entries()) {
signupsData[raidId] = {};
for (const [userId, signup] of signups.entries()) {
signupsData[raidId][userId] = signup;
}
}
const votingData = {};
for (const [raidId, voting] of raidVoting.entries()) {
votingData[raidId] = {};
for (const [optionNumber, usernames] of voting.votes.entries()) {
votingData[raidId][optionNumber] = Array.from(usernames);
}
}
const metadataData = {};
for (const [raidId, metadata] of raidMetadata.entries()) {
metadataData[raidId] = metadata;
}
// Save to files
await fs.writeFile(SIGNUPS_FILE, JSON.stringify(signupsData, null, 2));
await fs.writeFile(VOTING_FILE, JSON.stringify(votingData, null, 2));
await fs.writeFile(METADATA_FILE, JSON.stringify(metadataData, null, 2));
console.log('[RAID] Data saved successfully');
} catch (error) {
console.error('[RAID] Error saving data:', error);
}
}
// Load raid data from files
async function loadRaidData() {
try {
// Load signups
try {
const signupsContent = await fs.readFile(SIGNUPS_FILE, 'utf8');
const signupsData = JSON.parse(signupsContent);
for (const [raidId, signups] of Object.entries(signupsData)) {
const signupsMap = new Map();
for (const [userId, signup] of Object.entries(signups)) {
signupsMap.set(userId, signup);
}
raidSignups.set(raidId, signupsMap);
}
console.log('[RAID] Signups data loaded');
} catch (error) {
console.log('[RAID] No signups data to load');
}
// Load voting
try {
const votingContent = await fs.readFile(VOTING_FILE, 'utf8');
const votingData = JSON.parse(votingContent);
for (const [raidId, voting] of Object.entries(votingData)) {
const votesMap = new Map();
for (const [optionNumber, usernames] of Object.entries(voting)) {
votesMap.set(parseInt(optionNumber), new Set(usernames));
}
raidVoting.set(raidId, { votes: votesMap });
}
console.log('[RAID] Voting data loaded');
} catch (error) {
console.log('[RAID] No voting data to load');
}
// Load metadata
try {
const metadataContent = await fs.readFile(METADATA_FILE, 'utf8');
const metadataData = JSON.parse(metadataContent);
for (const [raidId, metadata] of Object.entries(metadataData)) {
raidMetadata.set(raidId, metadata);
}
console.log('[RAID] Metadata loaded');
} catch (error) {
console.log('[RAID] No metadata to load');
}
} catch (error) {
console.error('[RAID] Error loading data:', error);
}
}
// Initialize data on startup
initializeDataDirectory().then(() => {
loadRaidData();
});
// Periodic save every 5 minutes
setInterval(async () => {
await saveRaidData();
}, 5 * 60 * 1000); // 5 minutes
// Save data on process exit
process.on('SIGINT', async () => {
console.log('[RAID] Saving data before exit...');
await saveRaidData();
process.exit(0);
});
process.on('SIGTERM', async () => {
console.log('[RAID] Saving data before exit...');
await saveRaidData();
process.exit(0);
});
// Helper function to parse Discord time codes with raid association
function parseTimeCodes(timeString) {
// Split by comma and extract time codes
const timeCodes = timeString.split(',').map(t => t.trim()).filter(t => t);
const parsedTimes = [];
for (const timeCode of timeCodes) {
// Match Discord time format: <t:timestamp:format> (raid) or <t:timestamp:format>
const match = timeCode.match(/<t:(\d+):([FfDdTtR])>(\s*\((\w+)\))?/);
if (match) {
const timestamp = parseInt(match[1]);
const format = match[2];
const raidTag = match[4] ? match[4].toLowerCase() : null; // Extract raid from (bone) or (ice)
// Determine which raid this time is for
let raidType = null;
if (raidTag) {
if (raidTag.includes('bone')) {
raidType = 'bone dragon';
} else if (raidTag.includes('ice')) {
raidType = 'ice dragon';
}
}
parsedTimes.push({
timestamp: timestamp,
display: `<t:${timestamp}:${format}>`,
raw: timeCode,
raid: raidType
});
}
}
return parsedTimes;
}
// Helper function to parse minimum ability scores per raid
function parseMinimumAbilityScores(scoreString) {
// Format: "15500(ice), 17000(bone)" or "15500(ice), 17000(bone dragon)"
const scores = {};
const parts = scoreString.split(',').map(s => s.trim()).filter(s => s);
for (const part of parts) {
// Match format: number(raid)
const match = part.match(/(\d+)\s*\(([^)]+)\)/);
if (match) {
const score = parseInt(match[1]);
const raidTag = match[2].toLowerCase().trim();
// Normalize raid names
if (raidTag.includes('ice')) {
scores['ice dragon'] = score;
} else if (raidTag.includes('bone')) {
scores['bone dragon'] = score;
}
}
}
return scores;
}
// Helper function to format raid title
function formatRaidTitle(raidName, raidType, difficultyType) {
// Capitalize first letter of each word
let formattedRaid;
if (raidName.toLowerCase() === 'both') {
formattedRaid = 'Ice Dragon & Bone Dragon';
} else {
formattedRaid = raidName.split(' ').map(word =>
word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()
).join(' ');
}
// Handle difficulty type
let formattedType;
if (difficultyType.toLowerCase() === 'both') {
formattedType = 'Normal & Hard';
} else {
formattedType = difficultyType.charAt(0).toUpperCase() + difficultyType.slice(1).toLowerCase();
}
return `Blue Protocol - ${formattedRaid} ${formattedType}`;
}
async function handleRaidCommand(interaction) {
console.log('[RAID] Command started by', interaction.user.tag);
// Get parameters from interaction
const timesParam = interaction.options.getString('times');
const difficultyType = interaction.options.getString('type');
const raidName = interaction.options.getString('raid');
const minimumAbilityScoreParam = interaction.options.getString('minimum_ability_score');
// Parse time codes (with raid associations)
const parsedTimes = parseTimeCodes(timesParam);
if (parsedTimes.length === 0) {
await interaction.reply({
content: '❌ Invalid time codes provided. Please use Discord time format: `<t:timestamp:F> (bone)` or `<t:timestamp:F> (ice)` separated by commas.',
ephemeral: true
});
return;
}
// Parse minimum ability scores per raid
const minimumAbilityScores = parseMinimumAbilityScores(minimumAbilityScoreParam);
if (Object.keys(minimumAbilityScores).length === 0) {
await interaction.reply({
content: '❌ Invalid minimum ability score format. Please use format: `15500(ice), 17000(bone)`',
ephemeral: true
});
return;
}
// Build dynamic title
const raidTitle = formatRaidTitle(raidName, difficultyType, difficultyType);
// Format minimum ability scores for display
const minScoreDisplay = Object.entries(minimumAbilityScores)
.map(([raid, score]) => `${score}(${raid.split(' ')[0]})`)
.join(', ');
// Store metadata for this raid
const raidId = currentRaidId;
raidMetadata.set(raidId, {
title: raidTitle,
times: parsedTimes,
type: difficultyType,
raid: raidName,
minimumAbilityScores: minimumAbilityScores
});
// Create the raid embed
const embed = new EmbedBuilder()
.setTitle(raidTitle)
.setColor(0x8B0000) // Dark red for dragon theme
.setDescription(`\n**Raid Information:**\n> • **Minimum Players:** 20 (unlimited signups)\n> • **Minimum Ability Score:** ${minScoreDisplay}\n> • **Healers:** 4 slots\n> • **Tanks:** 3-4 slots\n> • **DPS:** 12-13 slots\n> • **Video Guide:** [Click here for strategy guide](${RAID_CONFIG.videoGuide})`)
.addFields(
{
name: '**How to Sign Up**',
value: '> Click "Sign Up" below\n> Select your role (Tank/Healer/DPS)\n> Enter your IGN and ability score\n> Submit your signup\n> *Note: You can update your signup by signing up again*'
},
{
name: '**Current Signups**',
value: '> No signups yet - be the first!'
}
)
.setFooter({ text: 'Dawn | Starlight - Blue Protocol', iconURL: interaction.guild.iconURL() })
.setTimestamp()
.setThumbnail('https://cdn.discordapp.com/emojis/1234567890123456789.png'); // You can add a dragon emoji URL here
// Create signup button
const signupButton = new ButtonBuilder()
.setCustomId('raid_signup_button')
.setLabel('Sign Up for Raid')
.setEmoji('⚔️')
.setStyle(ButtonStyle.Primary);
// Create view roster button
const rosterButton = new ButtonBuilder()
.setCustomId('raid_view_roster')
.setLabel('View Roster')
.setEmoji('📋')
.setStyle(ButtonStyle.Secondary);
const row = new ActionRowBuilder().addComponents(signupButton, rosterButton);
// Send first message with signup
await interaction.reply({ embeds: [embed], components: [row] });
// Initialize voting data if it doesn't exist
if (!raidVoting.has(raidId)) {
raidVoting.set(raidId, {
votes: new Map()
});
}
// Get current vote counts
const voteCounts = new Array(parsedTimes.length).fill(0);
const votingData = raidVoting.get(raidId);
for (let i = 0; i < parsedTimes.length; i++) {
voteCounts[i] = votingData.votes.get(i)?.size || 0;
}
// Create voting embed with raid information
const votingEmbed = new EmbedBuilder()
.setTitle('⏰ Vote for Raid Times')
.setColor(0x0099ff)
.setDescription('Vote for the times that best suit your availability. You can select multiple options!')
.addFields(
parsedTimes.map((time, index) => {
const raidLabel = time.raid ? ` (${time.raid.split(' ')[0]})` : '';
const minScore = time.raid && minimumAbilityScores[time.raid]
? ` Min: ${minimumAbilityScores[time.raid]}`
: '';
return {
name: `Option ${index + 1}${raidLabel} (${voteCounts[index]} votes)${minScore}`,
value: `${time.display}`,
inline: true
};
})
)
.setFooter({ text: 'Click the buttons below to vote for your preferred times' })
.setTimestamp();
// Create voting buttons
const votingButtons = parsedTimes.map((time, index) =>
new ButtonBuilder()
.setCustomId(`raid_vote_${index}`)
.setLabel(`Vote ${index + 1}`)
.setStyle(ButtonStyle.Primary)
);
const votingRows = [];
for (let i = 0; i < votingButtons.length; i += 4) {
votingRows.push(new ActionRowBuilder().addComponents(votingButtons.slice(i, i + 4)));
}
// Save metadata
await saveRaidData();
// Send second message with voting
await interaction.channel.send({ embeds: [votingEmbed], components: votingRows });
console.log('[RAID] Raid embeds posted (2 messages)');
}
async function handleRaidSignupButton(interaction) {
console.log('[RAID] Signup button clicked by', interaction.user.tag);
// Get raid title from metadata
const raidId = currentRaidId;
const metadata = raidMetadata.get(raidId);
const raidTitle = metadata ? metadata.title : 'the raid';
// Create role selection menu
const roleSelect = new StringSelectMenuBuilder()
.setCustomId('raid_role_select')
.setPlaceholder('Select your role for the raid')
.addOptions([
new StringSelectMenuOptionBuilder()
.setLabel('Tank')
.setDescription('Protect the team and control aggro')
.setValue('tank')
.setEmoji('🛡️'),
new StringSelectMenuOptionBuilder()
.setLabel('Healer')
.setDescription('Keep the team alive and healthy')
.setValue('healer')
.setEmoji('💚'),
new StringSelectMenuOptionBuilder()
.setLabel('DPS')
.setDescription('Deal damage and eliminate enemies')
.setValue('dps')
.setEmoji('⚔️')
]);
const row = new ActionRowBuilder().addComponents(roleSelect);
await interaction.reply({
content: `**Select your role for ${raidTitle}:**`,
components: [row],
ephemeral: true
});
}
async function handleRoleSelection(interaction) {
const selectedRole = interaction.values[0];
console.log(`[RAID] Role selected by ${interaction.user.tag}: ${selectedRole}`);
// Create modal for availability input
const modal = new ModalBuilder()
.setCustomId(`raid_availability_${selectedRole}`)
.setTitle('Raid Availability');
const ignInput = new TextInputBuilder()
.setCustomId('ign')
.setLabel('In-Game Name (IGN)')
.setStyle(TextInputStyle.Short)
.setPlaceholder('Your Blue Protocol character name')
.setRequired(true)
.setMaxLength(50);
const powerLevelInput = new TextInputBuilder()
.setCustomId('ability_score')
.setLabel('Ability Score')
.setStyle(TextInputStyle.Short)
.setPlaceholder('Your current ability score')
.setRequired(true)
.setMaxLength(10);
const additionalInfoInput = new TextInputBuilder()
.setCustomId('additional_info')
.setLabel('Additional Information (Optional)')
.setStyle(TextInputStyle.Paragraph)
.setPlaceholder('Any special notes, experience with the raid, preferred sub-role, etc.')
.setRequired(false)
.setMaxLength(500);
const firstActionRow = new ActionRowBuilder().addComponents(ignInput);
const secondActionRow = new ActionRowBuilder().addComponents(powerLevelInput);
const thirdActionRow = new ActionRowBuilder().addComponents(additionalInfoInput);
modal.addComponents(firstActionRow, secondActionRow, thirdActionRow);
await interaction.showModal(modal);
}
async function handleRaidAvailabilitySubmit(interaction) {
const role = interaction.customId.split('_')[2]; // Extract role from customId
const ign = interaction.fields.getTextInputValue('ign');
const powerLevel = interaction.fields.getTextInputValue('ability_score');
const additionalInfo = interaction.fields.getTextInputValue('additional_info') || 'None';
console.log(`[RAID] Signup submitted by ${interaction.user.tag} for role: ${role}`);
// Use the consistent current raid ID
const raidId = currentRaidId;
// Get metadata to check minimum ability scores
const metadata = raidMetadata.get(raidId);
const minimumAbilityScores = metadata ? metadata.minimumAbilityScores : {};
// Parse ability score and check against minimums
const abilityScoreNum = parseInt(powerLevel);
const isBelowMinimum = Object.values(minimumAbilityScores).some(minScore =>
minScore !== null && !isNaN(abilityScoreNum) && abilityScoreNum < minScore
);
// Get the lowest minimum to show in warning
const lowestMinimum = Object.values(minimumAbilityScores).length > 0
? Math.min(...Object.values(minimumAbilityScores))
: null;
// Store the signup data
if (!raidSignups.has(raidId)) {
raidSignups.set(raidId, new Map());
}
const signupData = {
userId: interaction.user.id,
username: interaction.user.tag,
ign: ign,
role: role,
powerLevel: powerLevel,
additionalInfo: additionalInfo,
signupTime: Date.now()
};
// Check if user already signed up
const existingSignup = raidSignups.get(raidId).has(interaction.user.id);
raidSignups.get(raidId).set(interaction.user.id, signupData);
// Save data to file
await saveRaidData();
// Get raid title from metadata
const raidTitle = metadata ? metadata.title : 'the raid';
// Create confirmation embed with warning if below minimum
const confirmEmbed = new EmbedBuilder()
.setTitle(existingSignup ? '✅ Raid Signup Updated!' : '✅ Raid Signup Confirmed!')
.setColor(isBelowMinimum ? 0xff9900 : 0x00ff00) // Orange if below minimum, green if okay
.setDescription(existingSignup ?
`You've successfully updated your signup for **${raidTitle}**!` :
`You've successfully signed up for **${raidTitle}**!`)
.addFields(
{ name: '🎭 Role', value: `${getRoleEmoji(role)} ${role.toUpperCase()}`, inline: true },
{ name: '👤 IGN', value: ign, inline: true },
{ name: '⚡ Ability Score', value: powerLevel, inline: true },
{ name: '📝 Additional Info', value: additionalInfo, inline: false }
)
.setFooter({ text: 'Dawn | Starlight - Blue Protocol' })
.setTimestamp();
// Add warning field if below minimum
if (isBelowMinimum && lowestMinimum !== null) {
const belowRaids = Object.entries(minimumAbilityScores)
.filter(([raid, minScore]) => !isNaN(abilityScoreNum) && abilityScoreNum < minScore)
.map(([raid, minScore]) => `${minScore} (${raid.split(' ')[0]})`)
.join(', ');
confirmEmbed.addFields({
name: '⚠️ Warning',
value: `Your ability score (${powerLevel}) is below the minimum required for some raids: ${belowRaids}. You may not be able to vote for those raid times.`,
inline: false
});
}
await interaction.reply({ embeds: [confirmEmbed], ephemeral: true });
// Update the main raid embed with new signup count
await updateRaidEmbed(interaction);
}
async function handleViewRoster(interaction) {
console.log('[RAID] View roster clicked by', interaction.user.tag);
// Use the current raid ID
const raidId = currentRaidId;
if (!raidSignups.has(raidId) || raidSignups.get(raidId).size === 0) {
await interaction.reply({
content: '❌ No raid signups found!',
ephemeral: true
});
return;
}
const signups = raidSignups.get(raidId);
const signupArray = Array.from(signups.values());
// Group by role
const tanks = signupArray.filter(s => s.role === 'tank');
const healers = signupArray.filter(s => s.role === 'healer');
const dps = signupArray.filter(s => s.role === 'dps');
// Helper function to split a long string into chunks that fit within Discord's 1024 character limit
function chunkString(str, maxLength = 1024) {
if (str.length <= maxLength) return [str];
const chunks = [];
const lines = str.split('\n');
let currentChunk = '';
for (const line of lines) {
if ((currentChunk + line + '\n').length > maxLength) {
if (currentChunk) chunks.push(currentChunk.trim());
currentChunk = line + '\n';
} else {
currentChunk += line + '\n';
}
}
if (currentChunk) chunks.push(currentChunk.trim());
return chunks;
}
// Build the embeds array
const embeds = [];
// Create main roster embed
const mainEmbed = new EmbedBuilder()
.setTitle('Raid Roster')
.setColor(0x8B0000)
.setDescription(`**Total Signups:** ${signupArray.length} (${signupArray.length >= RAID_CONFIG.minPlayers ? '✅ Ready!' : `Need ${RAID_CONFIG.minPlayers - signupArray.length} more`})`)
.setFooter({ text: 'Dawn | Starlight - Blue Protocol' })
.setTimestamp();
embeds.push(mainEmbed);
// Add Tanks
if (tanks.length > 0) {
const tanksText = tanks.map(t => `• **${t.ign}** (${t.powerLevel}) - <@${t.userId}>`).join('\n');
const tankChunks = chunkString(tanksText);
tankChunks.forEach((chunk, index) => {
const embed = new EmbedBuilder()
.setTitle(`🛡️ Tanks ${index === 0 ? `(${tanks.length}/${RAID_CONFIG.tankSlots})` : ''}`)
.setColor(0x8B0000)
.setDescription(chunk);
embeds.push(embed);
});
} else {
const embed = new EmbedBuilder()
.setTitle(`🛡️ Tanks (0/${RAID_CONFIG.tankSlots})`)
.setColor(0x8B0000)
.setDescription('No tanks signed up');
embeds.push(embed);
}
// Add Healers
if (healers.length > 0) {
const healersText = healers.map(h => `• **${h.ign}** (${h.powerLevel}) - <@${h.userId}>`).join('\n');
const healerChunks = chunkString(healersText);
healerChunks.forEach((chunk, index) => {
const embed = new EmbedBuilder()
.setTitle(`💚 Healers ${index === 0 ? `(${healers.length}/${RAID_CONFIG.healerSlots})` : ''}`)
.setColor(0x8B0000)
.setDescription(chunk);
embeds.push(embed);
});
} else {
const embed = new EmbedBuilder()
.setTitle(`💚 Healers (0/${RAID_CONFIG.healerSlots})`)
.setColor(0x8B0000)
.setDescription('No healers signed up');
embeds.push(embed);
}
// Add DPS
if (dps.length > 0) {
const dpsText = dps.map(d => `• **${d.ign}** (${d.powerLevel}) - <@${d.userId}>`).join('\n');
const dpsChunks = chunkString(dpsText);
dpsChunks.forEach((chunk, index) => {
const embed = new EmbedBuilder()
.setTitle(`⚔️ DPS ${index === 0 ? `(${dps.length}/${RAID_CONFIG.dpsSlots})` : ''}`)
.setColor(0x8B0000)
.setDescription(chunk);
embeds.push(embed);
});
} else {
const embed = new EmbedBuilder()
.setTitle(`⚔️ DPS (0/${RAID_CONFIG.dpsSlots})`)
.setColor(0x8B0000)
.setDescription('No DPS signed up');
embeds.push(embed);
}
await interaction.reply({ embeds: embeds, ephemeral: true });
}
async function updateRaidEmbed(interaction) {
try {
// Get raid title from metadata
const raidId = currentRaidId;
const metadata = raidMetadata.get(raidId);
const raidTitle = metadata ? metadata.title : 'Blue Protocol';
// Find the original raid message and update it
const messages = await interaction.channel.messages.fetch({ limit: 10 });
const raidMessage = messages.find(msg =>
msg.embeds.length > 0 &&
msg.embeds[0].title?.includes(raidTitle) &&
msg.author.bot
);
if (raidMessage) {
// Get current signup count for the current raid
const currentSignups = raidSignups.get(raidId);
const totalSignups = currentSignups ? currentSignups.size : 0;
const updatedEmbed = EmbedBuilder.from(raidMessage.embeds[0])
.setFields(
{
name: '**Current Signups**',
value: `> **${totalSignups}** players signed up (${totalSignups >= RAID_CONFIG.minPlayers ? '✅ Ready!' : `Need ${RAID_CONFIG.minPlayers - totalSignups} more`})\n> Use "View Roster" to see details`
}
);
await raidMessage.edit({ embeds: [updatedEmbed] });
}
} catch (error) {
console.error('[RAID] Error updating raid embed:', error);
}
}
async function handleSuggestTimes(interaction) {
console.log('[RAID] Suggest times clicked by', interaction.user.tag);
// Use the current raid ID
const raidId = currentRaidId;
if (!raidSignups.has(raidId) || raidSignups.get(raidId).size === 0) {
await interaction.reply({
content: '❌ No raid signups found!',
ephemeral: true
});
return;
}
const signups = raidSignups.get(raidId);
const signupArray = Array.from(signups.values());
// Get times from metadata
const metadata = raidMetadata.get(raidId);
if (!metadata || !metadata.times || metadata.times.length === 0) {
await interaction.reply({
content: '❌ No raid times configured! Please create a raid first.',
ephemeral: true
});
return;
}
const raidTimes = metadata.times;
// Initialize voting data if it doesn't exist
if (!raidVoting.has(raidId)) {
raidVoting.set(raidId, {
votes: new Map()
});
}
// Get current vote counts
const voteCounts = new Array(raidTimes.length).fill(0);
const votingData = raidVoting.get(raidId);
for (let i = 0; i < raidTimes.length; i++) {
voteCounts[i] = votingData.votes.get(i)?.size || 0;
}
// Get minimum ability scores from metadata
const minimumAbilityScores = metadata ? metadata.minimumAbilityScores : {};
// Create voting embed
const votingEmbed = new EmbedBuilder()
.setTitle('⏰ Raid Time Selection')
.setColor(0x0099ff)
.setDescription(`Vote for your preferred raid time! (${signupArray.length} signup${signupArray.length === 1 ? '' : 's'} so far)`)
.addFields(
raidTimes.map((time, index) => {
const raidLabel = time.raid ? ` (${time.raid.split(' ')[0]})` : '';
const minScore = time.raid && minimumAbilityScores[time.raid]
? `\nMin: ${minimumAbilityScores[time.raid]}`
: '';
return {
name: `Option ${index + 1}${raidLabel}`,
value: `${time.display}${minScore}\nVotes: ${voteCounts[index]}`,
inline: true
};
})
)
.setFooter({ text: 'Vote for your preferred time!' })
.setTimestamp();
// Create voting buttons
const votingButtons = raidTimes.map((time, index) =>
new ButtonBuilder()
.setCustomId(`raid_vote_${index}`)
.setLabel(`Vote ${index + 1}`)
.setStyle(ButtonStyle.Primary)
);
const rows = [];
for (let i = 0; i < votingButtons.length; i += 4) {
rows.push(new ActionRowBuilder().addComponents(votingButtons.slice(i, i + 4)));
}
await interaction.reply({ embeds: [votingEmbed], components: rows, ephemeral: true });
}
async function handleTimeVote(interaction) {
try {
console.log(`[RAID] Vote button clicked with customId: ${interaction.customId}`);
// Parse the option number from customId (format: raid_vote_0, raid_vote_1, etc.)
const optionNumber = parseInt(interaction.customId.split('_')[2]);
console.log(`[RAID] Parsed option number: ${optionNumber}`);
// Get metadata to check valid option range
const raidId = currentRaidId;
const metadata = raidMetadata.get(raidId);
const maxOptions = metadata && metadata.times ? metadata.times.length : 0;
if (isNaN(optionNumber) || optionNumber < 0 || (maxOptions > 0 && optionNumber >= maxOptions)) {
await interaction.reply({
content: '❌ Invalid vote option! Please try again.',
ephemeral: true
});
return;
}
// Get the time option and its associated raid
const timeOption = metadata.times[optionNumber];
const raidForTime = timeOption ? timeOption.raid : null;
const minimumAbilityScores = metadata ? metadata.minimumAbilityScores : {};
// Check if user has signed up and get their ability score
const userSignup = raidSignups.get(raidId)?.get(interaction.user.id);
if (!userSignup) {
await interaction.reply({
content: '❌ You must sign up for the raid first before voting!',
ephemeral: true
});
return;
}
// If this time option is for a specific raid, check ability score
if (raidForTime && minimumAbilityScores[raidForTime]) {
const userAbilityScore = parseInt(userSignup.powerLevel);
const requiredScore = minimumAbilityScores[raidForTime];
if (isNaN(userAbilityScore) || userAbilityScore < requiredScore) {
await interaction.reply({
content: `❌ Your ability score (${userSignup.powerLevel}) is below the minimum required (${requiredScore}) for **${raidForTime}**. You cannot vote for this time option.`,
ephemeral: true
});
return;
}
}
// Initialize voting data if it doesn't exist
if (!raidVoting.has(currentRaidId)) {
raidVoting.set(currentRaidId, {
votes: new Map()
});
}
const votingData = raidVoting.get(currentRaidId);
// Initialize the vote set for this option if it doesn't exist
if (!votingData.votes.has(optionNumber)) {
votingData.votes.set(optionNumber, new Set());
}
// Check if user already voted for this option - toggle it
if (votingData.votes.get(optionNumber).has(interaction.user.username)) {
// Remove the vote (undo)
votingData.votes.get(optionNumber).delete(interaction.user.username);
// Save data to file
await saveRaidData();
// Update voting results
await updateVotingResults(interaction);
const raidLabel = raidForTime ? ` for **${raidForTime}**` : '';
await interaction.reply({
content: `🗑️ Removed vote for **Option ${optionNumber + 1}**${raidLabel}!`,
ephemeral: true
});
return;
}
// Add the vote
votingData.votes.get(optionNumber).add(interaction.user.username);
// Save data to file
await saveRaidData();
// Update voting results
await updateVotingResults(interaction);
const raidLabel = raidForTime ? ` for **${raidForTime}**` : '';
await interaction.reply({
content: `✅ Voted for **Option ${optionNumber + 1}**${raidLabel}!`,
ephemeral: true
});
} catch (error) {
console.error('[RAID] Error in handleTimeVote:', error);
await interaction.reply({
content: '❌ There was an error processing your vote. Please try again.',
ephemeral: true
});
}
}
async function updateVotingResults(interaction) {
try {
const raidId = currentRaidId;
const votingData = raidVoting.get(raidId);
if (!votingData) return;
// Get times from metadata
const metadata = raidMetadata.get(raidId);
if (!metadata || !metadata.times || metadata.times.length === 0) {
return;
}
const raidTimes = metadata.times;
const minimumAbilityScores = metadata ? metadata.minimumAbilityScores : {};
// Find the voting message (should be the second message with Vote for Raid Times title)
const messages = await interaction.channel.messages.fetch({ limit: 10 });
const votingMessage = messages.find(msg =>
msg.embeds.length > 0 &&
msg.embeds[0].title?.includes('Vote for Raid Times') &&
msg.author.bot
);
if (votingMessage) {
// Calculate vote counts (keep original order, don't sort)
const voteCounts = raidTimes.map((time, index) => ({
display: time.display,
votes: votingData.votes.get(index)?.size || 0,
raid: time.raid
}));
const updatedVotingEmbed = new EmbedBuilder()
.setTitle('⏰ Vote for Raid Times')
.setColor(0x0099ff)
.setDescription('Vote for the times that best suit your availability. You can select multiple options!')
.addFields(
voteCounts.map((time, index) => {
const raidLabel = time.raid ? ` (${time.raid.split(' ')[0]})` : '';
const minScore = time.raid && minimumAbilityScores[time.raid]
? ` Min: ${minimumAbilityScores[time.raid]}`
: '';
return {
name: `Option ${index + 1}${raidLabel} (${time.votes} votes)${minScore}`,
value: `${time.display}`,
inline: true
};
})
)
.setFooter({ text: 'Click the buttons below to vote for your preferred times' })
.setTimestamp();
// Replace the voting embed
await votingMessage.edit({ embeds: [updatedVotingEmbed] });
}
} catch (error) {
console.error('[RAID] Error updating voting results:', error);
}
}
async function handleResetRaid(interaction) {
console.log('[RAID] Reset raid clicked by', interaction.user.tag);
// Check if user has admin permissions
if (!interaction.member.permissions.has('Administrator')) {
await interaction.reply({
content: '❌ You need Administrator permissions to reset the raid!',
ephemeral: true
});
return;
}
// Clear all raid data
raidSignups.clear();
raidVoting.clear();
raidMetadata.clear();
activeRaids.clear();
// Save cleared data
await saveRaidData();
await interaction.reply({
content: '✅ Raid data has been reset! All signups, voting data, and metadata have been cleared.',
ephemeral: true
});
console.log('[RAID] Raid data reset by', interaction.user.tag);
}
async function handleRaidStatus(interaction) {
console.log('[RAID] Status requested by', interaction.user.tag);
// Check if user has admin permissions
if (!interaction.member.permissions.has('Administrator')) {
await interaction.reply({
content: '❌ You need Administrator permissions to view raid status!',
ephemeral: true
});
return;
}
const raidId = currentRaidId;
const signups = raidSignups.get(raidId);
const voting = raidVoting.get(raidId);
const metadata = raidMetadata.get(raidId);
const signupCount = signups ? signups.size : 0;
// Get vote counts dynamically based on metadata