-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
1224 lines (1014 loc) · 48.5 KB
/
bot.js
File metadata and controls
1224 lines (1014 loc) · 48.5 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
// bot.js - Complete Discord bot with AI and GitHub integration
const {
Client,
Events,
GatewayIntentBits,
Partials,
EmbedBuilder,
AttachmentBuilder
} = require('discord.js');
const Anthropic = require('@anthropic-ai/sdk');
const { Octokit } = require('@octokit/rest');
const axios = require('axios');
const sharp = require('sharp');
const winston = require('winston');
require('dotenv').config();
class DiscordGitHubBot {
constructor() {
// Initialize Discord client
this.client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMessageReactions
],
partials: [
Partials.Message,
Partials.Channel,
Partials.Reaction
]
});
// Initialize AI client (Claude)
this.claude = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY
});
// Initialize GitHub client
this.github = new Octokit({
auth: process.env.GITHUB_TOKEN
});
// Monitored channels
this.MONITORED_CHANNELS = [
'ui-bugs',
'console-errors',
'feature-tests',
'suggested-features'
];
// Channel to label mapping
this.CHANNEL_LABELS = {
'ui-bugs': ['ui-bug', 'needs-triage'],
'console-errors': ['console-error', 'bug'],
'feature-tests': ['testing', 'needs-review'],
'suggested-features': ['enhancement', 'feature-request']
};
this.setupLogging();
this.setupEventListeners();
}
setupLogging() {
this.logger = winston.createLogger({
level: 'info',
format: winston.format.combine(
winston.format.timestamp(),
winston.format.json()
),
transports: [
new winston.transports.File({
filename: 'error.log',
level: 'error'
}),
new winston.transports.Console({
format: winston.format.simple()
})
]
});
}
setupEventListeners() {
this.client.once(Events.ClientReady, this.onReady.bind(this));
this.client.on(Events.MessageCreate, this.onMessage.bind(this));
this.client.on(Events.MessageReactionAdd, this.onReactionAdd.bind(this));
// Error handling
this.client.on(Events.Error, error => {
this.logger.error('Discord.js error:', error);
});
process.on('unhandledRejection', (reason, promise) => {
this.logger.error('Unhandled Rejection:', reason);
});
}
onReady(readyClient) {
this.logger.info(`✅ Bot ready! Logged in as ${readyClient.user.tag}`);
this.logger.info(`📡 Monitoring channels: ${this.MONITORED_CHANNELS.join(', ')}`);
}
async onMessage(message) {
// Ignore bot messages
if (message.author.bot) return;
// Handle bot commands (must start with !)
if (message.content.startsWith('!')) {
await this.handleCommand(message);
return;
}
// Check if message is in monitored channels
if (this.MONITORED_CHANNELS.includes(message.channel.name)) {
this.logger.info(`📝 Message in #${message.channel.name} by ${message.author.username}`);
// Auto-react to encourage confirmation
await message.react('✅');
// Extract images for preview
const images = await this.extractImages(message);
if (images.length > 0) {
this.logger.info(`🖼️ Found ${images.length} images in message`);
}
}
}
async handleCommand(message) {
const args = message.content.slice(1).trim().split(/ +/);
const command = args.shift().toLowerCase();
this.logger.info(`🤖 Command received: !${command} from ${message.author.username}`);
switch (command) {
case 'scan':
await this.handleScanCommand(message, args);
break;
case 'scanthread':
await this.handleScanThreadCommand(message, args);
break;
case 'help':
await this.handleHelpCommand(message);
break;
default:
await message.reply('❓ Unknown command. Use `!help` for available commands.');
}
}
async handleHelpCommand(message) {
const helpEmbed = new EmbedBuilder()
.setTitle('🤖 Discord GitHub Bot Commands')
.setDescription('Available commands for managing GitHub issue creation')
.setColor(0x5865F2)
.addFields(
{
name: '!scan [channel] [days]',
value: 'Scan for unprocessed messages\n• `!scan` - Scan all monitored channels (last 7 days)\n• `!scan ui-bugs` - Scan specific channel (last 7 days)\n• `!scan ui-bugs 14` - Scan specific channel (last 14 days)',
inline: false
},
{
name: '!scanthread [days]',
value: 'Scan current thread only\n• `!scanthread` - Scan current thread (last 7 days)\n• `!scanthread 14` - Scan current thread (last 14 days)\n**Note:** Only works when used inside a thread',
inline: false
},
{
name: '!help',
value: 'Show this help message',
inline: false
},
{
name: 'Regular Usage',
value: 'React with ✅ to any message in monitored channels to create a GitHub issue',
inline: false
}
)
.addFields(
{
name: 'Monitored Channels',
value: this.MONITORED_CHANNELS.map(c => `#${c}`).join(', '),
inline: false
}
)
.setTimestamp();
await message.reply({ embeds: [helpEmbed] });
}
async handleScanThreadCommand(message, args) {
const days = parseInt(args[0]) || 7;
// Check if we're in a thread
if (!message.channel.isThread()) {
await message.reply('❌ This command only works inside a thread. Use `!scan` for regular channel scanning.');
return;
}
// Check if thread is in a monitored channel
const parentChannel = message.channel.parent;
if (!parentChannel || !this.MONITORED_CHANNELS.includes(parentChannel.name)) {
await message.reply(`❌ This thread is not in a monitored channel. Monitored channels: ${this.MONITORED_CHANNELS.map(c => `\`${c}\``).join(', ')}`);
return;
}
// Validate days parameter
if (days < 1 || days > 30) {
await message.reply('❌ Days must be between 1 and 30.');
return;
}
const scanEmbed = new EmbedBuilder()
.setTitle('🧵 Scanning Current Thread')
.setDescription(`Scanning this thread for unprocessed messages...`)
.setColor(0xFFDD00)
.addFields(
{ name: '🧵 Thread', value: message.channel.name, inline: true },
{ name: '📁 Parent Channel', value: `#${parentChannel.name}`, inline: true },
{ name: '📅 Time Range', value: `Last ${days} day(s)`, inline: true }
)
.setTimestamp();
const statusMessage = await message.reply({ embeds: [scanEmbed] });
try {
const results = await this.scanThreadForUnprocessed(message.channel, days);
await this.displayThreadScanResults(statusMessage, results, message.channel, days);
} catch (error) {
this.logger.error('Error during thread scan:', error);
const errorEmbed = new EmbedBuilder()
.setTitle('❌ Thread Scan Failed')
.setDescription(`Error occurred during thread scan: ${error.message}`)
.setColor(0xFF6B6B)
.setTimestamp();
await statusMessage.edit({ embeds: [errorEmbed] });
}
}
async scanThreadForUnprocessed(thread, days) {
const cutoffDate = new Date();
cutoffDate.setDate(cutoffDate.getDate() - days);
const results = {
totalMessages: 0,
unprocessedMessages: [],
processedMessages: 0
};
this.logger.info(`🧵 Scanning thread: ${thread.name}...`);
try {
// Fetch messages from the thread
let lastMessageId = null;
let hasMoreMessages = true;
while (hasMoreMessages) {
const fetchOptions = { limit: 100 };
if (lastMessageId) {
fetchOptions.before = lastMessageId;
}
const messages = await thread.messages.fetch(fetchOptions);
if (messages.size === 0) {
hasMoreMessages = false;
break;
}
for (const [messageId, msg] of messages) {
// Stop if message is older than cutoff
if (msg.createdAt < cutoffDate) {
hasMoreMessages = false;
break;
}
// Skip bot messages
if (msg.author.bot) continue;
// Skip empty messages or very short messages
if (!msg.content || msg.content.trim().length < 10) continue;
results.totalMessages++;
// Check if message has been processed (has ✅ reaction from bot)
const checkmarkReactions = msg.reactions.cache.filter(reaction =>
reaction.emoji.name === '✅'
);
let processedByBot = false;
for (const [reactionId, reaction] of checkmarkReactions) {
const users = await reaction.users.fetch();
if (users.has(this.client.user.id)) {
processedByBot = true;
break;
}
}
if (processedByBot) {
results.processedMessages++;
} else {
results.unprocessedMessages.push({
id: msg.id,
channel: thread.parent.name,
threadName: thread.name,
content: msg.content.substring(0, 100) + (msg.content.length > 100 ? '...' : ''),
author: msg.author.username,
createdAt: msg.createdAt,
url: msg.url,
attachments: msg.attachments.size
});
}
lastMessageId = messageId;
}
}
} catch (error) {
this.logger.error(`Error scanning thread ${thread.name}:`, error);
}
this.logger.info(`🧵 Thread scan complete: ${results.totalMessages} total, ${results.unprocessedMessages.length} unprocessed`);
return results;
}
async displayThreadScanResults(statusMessage, results, thread, days) {
const { totalMessages, unprocessedMessages, processedMessages } = results;
// Create summary embed
const summaryEmbed = new EmbedBuilder()
.setTitle('🧵 Thread Scan Results')
.setDescription(`Scanned thread "${thread.name}" for the last ${days} day(s)`)
.setColor(unprocessedMessages.length > 0 ? 0xFFDD00 : 0x57F287)
.addFields(
{ name: '📝 Total Messages', value: totalMessages.toString(), inline: true },
{ name: '✅ Already Processed', value: processedMessages.toString(), inline: true },
{ name: '⏳ Unprocessed', value: unprocessedMessages.length.toString(), inline: true },
{ name: '🧵 Thread', value: thread.name, inline: true },
{ name: '📁 Parent Channel', value: `#${thread.parent.name}`, inline: true },
{ name: '🔒 Thread Status', value: thread.archived ? 'Archived' : 'Active', inline: true }
)
.setTimestamp();
await statusMessage.edit({ embeds: [summaryEmbed] });
// If there are unprocessed messages, show them
if (unprocessedMessages.length > 0) {
await this.displayUnprocessedMessages(statusMessage, unprocessedMessages.slice(0, 10)); // Show first 10
if (unprocessedMessages.length > 10) {
const moreEmbed = new EmbedBuilder()
.setDescription(`📝 Showing first 10 of ${unprocessedMessages.length} unprocessed messages in this thread.`)
.setColor(0x99AAB5);
await statusMessage.channel.send({ embeds: [moreEmbed] });
}
}
}
async handleScanCommand(message, args) {
const channelName = args[0];
const days = parseInt(args[1]) || 7;
// Validate days parameter
if (days < 1 || days > 30) {
await message.reply('❌ Days must be between 1 and 30.');
return;
}
let channelsToScan = [];
if (channelName) {
// Scan specific channel
if (!this.MONITORED_CHANNELS.includes(channelName)) {
await message.reply(`❌ Channel \`${channelName}\` is not monitored. Monitored channels: ${this.MONITORED_CHANNELS.map(c => `\`${c}\``).join(', ')}`);
return;
}
channelsToScan = [channelName];
} else {
// Scan all monitored channels
channelsToScan = this.MONITORED_CHANNELS;
}
const scanEmbed = new EmbedBuilder()
.setTitle('🔍 Starting Channel Scan')
.setDescription(`Scanning for unprocessed messages...`)
.setColor(0xFFDD00)
.addFields(
{ name: '📁 Channels', value: channelsToScan.map(c => `#${c}`).join(', '), inline: true },
{ name: '📅 Time Range', value: `Last ${days} day(s)`, inline: true },
{ name: '⏳ Status', value: 'In Progress...', inline: true }
)
.setTimestamp();
const statusMessage = await message.reply({ embeds: [scanEmbed] });
try {
const results = await this.scanChannelsForUnprocessed(channelsToScan, days);
await this.displayScanResults(statusMessage, results, channelsToScan, days);
} catch (error) {
this.logger.error('Error during channel scan:', error);
const errorEmbed = new EmbedBuilder()
.setTitle('❌ Scan Failed')
.setDescription(`Error occurred during scan: ${error.message}`)
.setColor(0xFF6B6B)
.setTimestamp();
await statusMessage.edit({ embeds: [errorEmbed] });
}
}
async scanChannelsForUnprocessed(channelNames, days) {
const cutoffDate = new Date();
cutoffDate.setDate(cutoffDate.getDate() - days);
const results = {
totalMessages: 0,
unprocessedMessages: [],
processedMessages: 0,
channelStats: {}
};
for (const channelName of channelNames) {
this.logger.info(`🔍 Scanning #${channelName}...`);
const channel = this.client.channels.cache.find(ch => ch.name === channelName);
if (!channel) {
this.logger.warn(`Channel #${channelName} not found`);
continue;
}
const channelStats = {
total: 0,
unprocessed: 0,
processed: 0
};
try {
// Fetch messages from the channel
let lastMessageId = null;
let hasMoreMessages = true;
while (hasMoreMessages) {
const fetchOptions = { limit: 100 };
if (lastMessageId) {
fetchOptions.before = lastMessageId;
}
const messages = await channel.messages.fetch(fetchOptions);
if (messages.size === 0) {
hasMoreMessages = false;
break;
}
for (const [messageId, msg] of messages) {
// Stop if message is older than cutoff
if (msg.createdAt < cutoffDate) {
hasMoreMessages = false;
break;
}
// Skip bot messages
if (msg.author.bot) continue;
// Skip empty messages or very short messages
if (!msg.content || msg.content.trim().length < 10) continue;
// Skip messages that are in archived/closed threads
if (msg.hasThread && msg.thread && msg.thread.archived) {
this.logger.info(`Skipping message in archived thread: ${msg.thread.name}`);
continue;
}
channelStats.total++;
results.totalMessages++;
// Check if message has been processed (has ✅ reaction from bot)
const checkmarkReactions = msg.reactions.cache.filter(reaction =>
reaction.emoji.name === '✅'
);
let processedByBot = false;
for (const [reactionId, reaction] of checkmarkReactions) {
const users = await reaction.users.fetch();
if (users.has(this.client.user.id)) {
processedByBot = true;
break;
}
}
if (processedByBot) {
channelStats.processed++;
results.processedMessages++;
} else {
channelStats.unprocessed++;
results.unprocessedMessages.push({
id: msg.id,
channel: channelName,
content: msg.content.substring(0, 100) + (msg.content.length > 100 ? '...' : ''),
author: msg.author.username,
createdAt: msg.createdAt,
url: msg.url,
attachments: msg.attachments.size
});
}
lastMessageId = messageId;
}
}
} catch (error) {
this.logger.error(`Error scanning #${channelName}:`, error);
}
results.channelStats[channelName] = channelStats;
this.logger.info(`📊 #${channelName}: ${channelStats.total} total, ${channelStats.unprocessed} unprocessed`);
}
return results;
}
async displayScanResults(statusMessage, results, channelsScanned, days) {
const { totalMessages, unprocessedMessages, processedMessages, channelStats } = results;
// Create summary embed
const summaryEmbed = new EmbedBuilder()
.setTitle('📊 Scan Results Summary')
.setDescription(`Scanned ${channelsScanned.length} channel(s) for the last ${days} day(s)`)
.setColor(unprocessedMessages.length > 0 ? 0xFFDD00 : 0x57F287)
.addFields(
{ name: '📝 Total Messages', value: totalMessages.toString(), inline: true },
{ name: '✅ Already Processed', value: processedMessages.toString(), inline: true },
{ name: '⏳ Unprocessed', value: unprocessedMessages.length.toString(), inline: true }
)
.setTimestamp();
// Add channel breakdown
let channelBreakdown = '';
for (const [channel, stats] of Object.entries(channelStats)) {
channelBreakdown += `**#${channel}**: ${stats.unprocessed}/${stats.total} unprocessed\n`;
}
if (channelBreakdown) {
summaryEmbed.addFields({ name: '📁 Channel Breakdown', value: channelBreakdown });
}
await statusMessage.edit({ embeds: [summaryEmbed] });
// If there are unprocessed messages, show them with reaction options
if (unprocessedMessages.length > 0) {
await this.displayUnprocessedMessages(statusMessage, unprocessedMessages.slice(0, 10)); // Show first 10
if (unprocessedMessages.length > 10) {
const moreEmbed = new EmbedBuilder()
.setDescription(`📝 Showing first 10 of ${unprocessedMessages.length} unprocessed messages. React with 🔄 to see more.`)
.setColor(0x99AAB5);
await statusMessage.channel.send({ embeds: [moreEmbed] });
}
}
}
async displayUnprocessedMessages(statusMessage, messages) {
const embed = new EmbedBuilder()
.setTitle('⏳ Unprocessed Messages')
.setDescription('React with ✅ to process these messages into GitHub issues:')
.setColor(0xFFDD00);
messages.forEach((msg, index) => {
embed.addFields({
name: `${index + 1}. #${msg.channel} - @${msg.author}`,
value: `${msg.content}\n[Jump to message](${msg.url}) • ${msg.createdAt.toLocaleDateString()} • ${msg.attachments > 0 ? `📎 ${msg.attachments} attachment(s)` : 'No attachments'}`,
inline: false
});
});
const messageReply = await statusMessage.channel.send({ embeds: [embed] });
// Add reactions for batch processing
await messageReply.react('✅'); // Process all
await messageReply.react('❌'); // Cancel
return messageReply;
}
async onReactionAdd(reaction, user) {
// Handle partial reactions (for older messages)
if (reaction.partial) {
try {
await reaction.fetch();
} catch (error) {
this.logger.error('Error fetching reaction:', error);
return;
}
}
// Ignore bot reactions
if (user.bot) return;
// Handle batch processing reactions (from scan results)
if (reaction.message.author.id === this.client.user.id &&
reaction.message.embeds.length > 0 &&
reaction.message.embeds[0].title === '⏳ Unprocessed Messages') {
if (reaction.emoji.name === '✅') {
await this.handleBatchProcessing(reaction, user);
return;
} else if (reaction.emoji.name === '❌') {
await this.handleBatchCancel(reaction, user);
return;
}
}
// Check for checkmark emoji and monitored channels
if (reaction.emoji.name === '✅' &&
this.MONITORED_CHANNELS.includes(reaction.message.channel.name)) {
this.logger.info(`✅ Checkmark confirmed by ${user.username} in #${reaction.message.channel.name}`);
await this.handleConfirmation(reaction, user);
}
}
async handleBatchProcessing(reaction, user) {
this.logger.info(`🔄 Batch processing initiated by ${user.username}`);
const embed = reaction.message.embeds[0];
const messageUrls = [];
// Extract message URLs from embed fields
embed.fields.forEach(field => {
const urlMatch = field.value.match(/\[Jump to message\]\((https:\/\/[^\)]+)\)/);
if (urlMatch) {
messageUrls.push(urlMatch[1]);
}
});
if (messageUrls.length === 0) {
await reaction.message.reply('❌ No messages found to process.');
return;
}
const statusEmbed = new EmbedBuilder()
.setTitle('🔄 Batch Processing Started')
.setDescription(`Processing ${messageUrls.length} messages...`)
.setColor(0xFFDD00)
.addFields(
{ name: '⏳ Status', value: 'Starting batch processing...', inline: false },
{ name: '📊 Progress', value: '0 / ' + messageUrls.length, inline: true },
{ name: '✅ Completed', value: '0', inline: true },
{ name: '❌ Failed', value: '0', inline: true }
)
.setTimestamp();
const statusMessage = await reaction.message.reply({ embeds: [statusEmbed] });
let completed = 0;
let failed = 0;
for (let i = 0; i < messageUrls.length; i++) {
const messageUrl = messageUrls[i];
try {
// Parse Discord message URL to get guild, channel, and message ID
const urlParts = messageUrl.split('/');
const messageId = urlParts[urlParts.length - 1];
const channelId = urlParts[urlParts.length - 2];
// Find the channel and message
const channel = this.client.channels.cache.get(channelId);
if (!channel) {
throw new Error('Channel not found');
}
const message = await channel.messages.fetch(messageId);
if (!message) {
throw new Error('Message not found');
}
// Create a mock reaction object for handleConfirmation
const mockReaction = {
message: message,
emoji: { name: '✅' }
};
await this.handleConfirmation(mockReaction, user);
completed++;
this.logger.info(`✅ Processed message ${i + 1}/${messageUrls.length}`);
} catch (error) {
failed++;
this.logger.error(`❌ Failed to process message ${i + 1}:`, error);
}
// Update progress every 3 messages or at the end
if ((i + 1) % 3 === 0 || i === messageUrls.length - 1) {
const updatedEmbed = new EmbedBuilder()
.setTitle('🔄 Batch Processing in Progress')
.setDescription(`Processing ${messageUrls.length} messages...`)
.setColor(0xFFDD00)
.addFields(
{ name: '⏳ Status', value: `Processing message ${i + 1} of ${messageUrls.length}...`, inline: false },
{ name: '📊 Progress', value: `${i + 1} / ${messageUrls.length}`, inline: true },
{ name: '✅ Completed', value: completed.toString(), inline: true },
{ name: '❌ Failed', value: failed.toString(), inline: true }
)
.setTimestamp();
await statusMessage.edit({ embeds: [updatedEmbed] });
}
// Small delay to avoid rate limits
await new Promise(resolve => setTimeout(resolve, 1000));
}
// Final status update
const finalEmbed = new EmbedBuilder()
.setTitle('🎉 Batch Processing Complete')
.setDescription(`Processed ${messageUrls.length} messages`)
.setColor(failed > 0 ? 0xFFDD00 : 0x57F287)
.addFields(
{ name: '📊 Total', value: messageUrls.length.toString(), inline: true },
{ name: '✅ Completed', value: completed.toString(), inline: true },
{ name: '❌ Failed', value: failed.toString(), inline: true }
)
.setTimestamp();
await statusMessage.edit({ embeds: [finalEmbed] });
}
async handleBatchCancel(reaction, user) {
this.logger.info(`❌ Batch processing cancelled by ${user.username}`);
const cancelEmbed = new EmbedBuilder()
.setTitle('❌ Batch Processing Cancelled')
.setDescription('Batch processing was cancelled by user request.')
.setColor(0xFF6B6B)
.addFields(
{ name: '👤 Cancelled by', value: user.username, inline: true }
)
.setTimestamp();
await reaction.message.reply({ embeds: [cancelEmbed] });
}
async handleConfirmation(reaction, user) {
const message = reaction.message;
const channelName = message.channel.name;
this.logger.info(`🔄 Starting handleConfirmation for message in #${channelName}`);
try {
// Create or get existing thread
let thread;
if (message.hasThread) {
thread = message.thread;
this.logger.info('📝 Using existing thread');
} else {
thread = await message.startThread({
name: `✅ Processing: ${message.content.substring(0, 40)}...`,
autoArchiveDuration: 1440 // 24 hours
});
this.logger.info('📝 Created new thread');
}
// Send initial confirmation
const confirmEmbed = new EmbedBuilder()
.setTitle('🔄 Processing Confirmation')
.setDescription('Creating GitHub issue from Discord message...')
.setColor(0xFFDD00)
.addFields(
{ name: '👤 Confirmed by', value: user.username, inline: true },
{ name: '📁 Channel', value: `#${channelName}`, inline: true },
{ name: '🔗 Original', value: `[Jump to message](${message.url})` }
)
.setTimestamp();
const processingMessage = await thread.send({ embeds: [confirmEmbed] });
// Collect thread messages and images
this.logger.info('� Analyzing entire thread...');
const threadData = await this.collectThreadMessages(message);
this.logger.info(`� Found ${threadData.messages.length} messages and ${threadData.images.length} images in thread`);
// Process with AI
this.logger.info('🤖 Processing thread with AI...');
const aiSummary = await this.processWithAI(message, channelName, threadData);
this.logger.info('🤖 AI processing completed');
// Create GitHub issue
this.logger.info('🐙 Creating GitHub issue...');
const githubIssue = await this.createGitHubIssue(message, channelName, aiSummary, threadData.images);
this.logger.info('🐙 GitHub issue created successfully');
// Update with success
const successEmbed = new EmbedBuilder()
.setTitle('✅ GitHub Issue Created Successfully')
.setDescription(aiSummary.title)
.setColor(0x57F287)
.addFields(
{ name: '🎫 Issue Number', value: `#${githubIssue.number}`, inline: true },
{ name: '🏷️ Labels', value: githubIssue.labels.map(l => l.name).join(', '), inline: true },
{ name: '🧵 Thread Messages', value: threadData.messages.length.toString(), inline: true },
{ name: '🖼️ Images', value: threadData.images.length.toString(), inline: true },
{ name: '� Participants', value: [...new Set(threadData.messages.map(m => m.author))].join(', '), inline: false },
{ name: '�🔗 GitHub Link', value: `[View Issue](${githubIssue.html_url})` },
{ name: '📋 Discord Link', value: `[Original Message](${message.url})` }
)
.setTimestamp();
await processingMessage.edit({ embeds: [successEmbed] });
// Process and display images in thread
if (threadData.images.length > 0) {
await this.processImagesInThread(thread, threadData.images);
}
} catch (error) {
this.logger.error('Error handling confirmation:', error);
this.logger.error('Error stack:', error.stack);
// Send error message to thread if thread was created
try {
const errorEmbed = new EmbedBuilder()
.setTitle('❌ Error Processing Request')
.setDescription(`Failed to create GitHub issue: ${error.message}`)
.setColor(0xFF6B6B)
.setTimestamp();
if (thread) {
await thread.send({ embeds: [errorEmbed] });
}
} catch (threadError) {
this.logger.error('Error sending error message to thread:', threadError);
}
}
}
async extractImages(message) {
const images = [];
// Extract from attachments
message.attachments.forEach(attachment => {
if (attachment.contentType?.startsWith('image/')) {
images.push({
url: attachment.url,
name: attachment.name,
size: attachment.size,
type: 'attachment'
});
}
});
// Extract from embedded images (URL patterns)
const imageUrlRegex = /(https?:\/\/.*\.(?:png|jpg|jpeg|gif|webp))/gi;
const imageUrls = message.content.match(imageUrlRegex) || [];
imageUrls.forEach(url => {
images.push({
url: url,
name: url.split('/').pop(),
type: 'embedded'
});
});
return images;
}
async collectThreadMessages(message) {
const threadMessages = [];
let allImages = [];
// Check if this message has a thread
if (message.hasThread) {
try {
const thread = message.thread;
const messages = await thread.messages.fetch({ limit: 100 });
// Sort messages by creation time
const sortedMessages = messages.sort((a, b) => a.createdAt - b.createdAt);
for (const [messageId, msg] of sortedMessages) {
if (!msg.author.bot) { // Skip bot messages
threadMessages.push({
author: msg.author.username,
content: msg.content,
createdAt: msg.createdAt,
attachments: msg.attachments.size
});
// Extract images from thread messages
const msgImages = await this.extractImages(msg);
allImages = allImages.concat(msgImages);
}
}
} catch (error) {
this.logger.error('Error fetching thread messages:', error);
}
}
// Add the original message
threadMessages.unshift({
author: message.author.username,
content: message.content,
createdAt: message.createdAt,
attachments: message.attachments.size
});
// Extract images from original message
const originalImages = await this.extractImages(message);
allImages = allImages.concat(originalImages);
return {
messages: threadMessages,
images: allImages
};
}
async processWithAI(message, channelName, threadData) {
const { messages, images } = threadData;
// Format all messages in the thread
let conversationText = '';
messages.forEach((msg, index) => {
conversationText += `Message ${index + 1} (${msg.author} at ${msg.createdAt.toISOString()}):\n${msg.content}\n\n`;
});
// Determine if this should be a bug report or feature request
const isFeatureRequest = channelName === 'suggested-features';
const templateType = isFeatureRequest ? 'Feature request' : 'Bug report';
const templateAbout = isFeatureRequest ? 'Suggest an idea for this project' : 'Create a report to help us improve';
const labelType = isFeatureRequest ? 'enhancement' : 'bug';
const prompt = `
Analyze this Discord thread from #${channelName} and create a structured GitHub issue following the standard ${templateType.toLowerCase()} template.
**Discord Thread:**
Channel: #${channelName}
Total Messages: ${messages.length}
Images Attached: ${images.length}
**Full Conversation:**
${conversationText}
Please analyze the entire conversation and create a comprehensive GitHub issue. Extract information from all messages in the thread to build a complete picture.
Generate a response in this EXACT format (use the GitHub ${templateType.toLowerCase()} template):
---
name: ${templateType}
about: ${templateAbout}
title: '[Brief descriptive title max 80 characters]'
labels: '${labelType}'
assignees: ''
---
${isFeatureRequest ? `**Is your feature request related to a problem? Please describe.**
[Clear and concise description of what the problem is based on the thread conversation]
**Describe the solution you'd like**
[Clear and concise description of what you want to happen based on the conversation]
**Describe alternatives you've considered**
[Any alternative solutions or features mentioned in the conversation]
**Additional context**
[Any other context or screenshots about the feature request from the thread]` : `**Describe the bug**
[Clear and concise description based on the thread conversation]
**To Reproduce**
Steps to reproduce the behavior:
1. [Step 1 from the thread]
2. [Step 2 from the thread]
3. [Step 3 from the thread]
4. [Final step that shows the error]
**Expected behavior**
[Clear description of what should happen based on the conversation]
**Screenshots**
${images.length > 0 ? `${images.length} screenshot(s) provided in the Discord thread` : 'No screenshots provided'}
**Environment Information:**
- Platform: [Extract from conversation if mentioned, otherwise "Not specified"]
- Browser: [Extract from conversation if mentioned, otherwise "Not specified"]
- Version: [Extract from conversation if mentioned, otherwise "Not specified"]
- Device: [Extract from conversation if mentioned, otherwise "Not specified"]
**Additional context**
[Any other relevant context from the thread conversation]`}
**Discord Thread Context**
- Original Channel: #${channelName}
- Thread Participants: ${[...new Set(messages.map(m => m.author))].join(', ')}
- Messages in Thread: ${messages.length}
- Date: ${messages[0].createdAt.toDateString()}
IMPORTANT: Respond with ONLY the formatted ${templateType.toLowerCase()} above, no JSON or other formatting.`;
try {
const response = await this.claude.messages.create({