-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
1156 lines (1001 loc) · 46.5 KB
/
index.js
File metadata and controls
1156 lines (1001 loc) · 46.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
// index.js — GitBot V3
// ─────────────────────────────────────────────────────────────────────────────
// What's new in V3 (Multi-Repository Support):
// - SQLite database for storing multiple repositories
// - /repo add/remove/list/info/enable - manage multiple repos
// - /admin add/remove/list - manage bot administrators
// - Per-repo webhook routing (/webhook/:repoId or /webhook/owner/repo)
// - Auto-generated webhook secrets per repository
// - Legacy mode still supported for config.json
// ─────────────────────────────────────────────────────────────────────────────
"use strict";
require("dotenv").config();
const {
Client,
GatewayIntentBits,
REST,
Routes,
SlashCommandBuilder,
ContextMenuCommandBuilder,
ApplicationCommandType,
EmbedBuilder,
ActionRowBuilder,
ButtonBuilder,
ButtonStyle,
ActivityType,
} = require("discord.js");
const express = require("express");
// Database and modules
const db = require("./database");
const { buildEmbed } = require("./embeds");
const { helpCommand, handleHelpInteraction } = require("./help");
const digest = require("./digest");
const mutes = require("./mutes");
const poller = require("./poller");
const {
repoCommands,
handleRepoCommand,
handleAdminCommand,
handleRepoInteraction,
setBotOwnerId,
} = require("./repoCommands");
const {
createWebhookRouter,
handlePolledEvent,
stats: webhookStats,
} = require("./multiWebhook");
// ─── Startup validation ───────────────────────────────────────────────────────
const REQUIRED_ENV = ["DISCORD_TOKEN", "DISCORD_GUILD_ID"];
const missingEnv = REQUIRED_ENV.filter(k => !process.env[k]);
if (missingEnv.length) {
console.error(`❌ Missing environment variables: ${missingEnv.join(", ")}`);
console.error(" Copy .env.example to .env and fill in your values.");
process.exit(1);
}
// ─── Config (Legacy - kept for compatibility) ───────────────────────────────────
function loadConfig() {
return { channels: {} };
}
function saveConfig() {
// No-op - config.json is deprecated
}
// ─── Stats ────────────────────────────────────────────────────────────────────
const stats = {
eventsReceived: 0,
eventsSent: 0,
eventsDropped: 0,
eventsIgnored: 0,
eventsMuted: 0,
startTime: Date.now(),
lastEvent: null,
lastEventTime: null,
eventCounts: {},
};
function recordEvent(eventType, outcome) {
stats.eventsReceived++;
stats.lastEvent = eventType;
stats.lastEventTime = new Date();
stats.eventCounts[eventType] = (stats.eventCounts[eventType] || 0) + 1;
if (outcome === "sent") stats.eventsSent++;
else if (outcome === "dropped") stats.eventsDropped++;
else if (outcome === "muted") stats.eventsMuted++;
else stats.eventsIgnored++;
}
function resetStats() {
stats.eventsReceived = 0;
stats.eventsSent = 0;
stats.eventsDropped = 0;
stats.eventsIgnored = 0;
stats.eventsMuted = 0;
stats.startTime = Date.now();
stats.lastEvent = null;
stats.lastEventTime = null;
stats.eventCounts = {};
}
// ─── Discord client ───────────────────────────────────────────────────────────
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
// ─── Shared embed builders ────────────────────────────────────────────────────
function buildStatusEmbed() {
const sec = Math.floor((Date.now() - stats.startTime) / 1000);
const h = Math.floor(sec / 3600);
const m = Math.floor((sec % 3600) / 60);
const s = sec % 60;
const activeMutes = mutes.list();
const muteStr = activeMutes.length
? activeMutes.map(mu => {
const left = Math.ceil((mu.expiresAt.getTime() - Date.now()) / 60_000);
return `\`${mu.eventType}\` (${left}m left)`;
}).join(", ")
: "_none_";
return new EmbedBuilder()
.setColor(0x5865F2)
.setTitle("🤖 GitBot V3 — Status")
.setThumbnail(client.user.displayAvatarURL())
.addFields(
{ name: "🟢 Bot", value: `**${client.user.tag}**`, inline: false },
{ name: "⏱️ Uptime", value: `${h}h ${m}m ${s}s`, inline: true },
{ name: "📡 WS Ping", value: `${client.ws.ping}ms`, inline: true },
{ name: "📦 Port", value: String(process.env.WEBHOOK_PORT || 3000), inline: true },
{ name: "📬 Received", value: String(stats.eventsReceived), inline: true },
{ name: "✉️ Sent", value: String(stats.eventsSent), inline: true },
{ name: "🔇 Muted", value: String(stats.eventsMuted), inline: true },
{ name: "🚫 Dropped", value: String(stats.eventsDropped), inline: true },
{ name: "⏭️ Ignored", value: String(stats.eventsIgnored), inline: true },
{ name: "🔕 Active mutes", value: muteStr, inline: false },
)
.setFooter({
text: stats.lastEvent
? `Last: ${stats.lastEvent} at ${stats.lastEventTime?.toLocaleTimeString()}`
: "No events yet",
})
.setTimestamp();
}
function buildEventsEmbed() {
if (stats.eventsReceived === 0) return null;
const rows = Object.entries(stats.eventCounts)
.sort((a, b) => b[1] - a[1])
.map(([evt, count]) => {
const pct = Math.round((count / stats.eventsReceived) * 10);
const bar = "█".repeat(pct) + "░".repeat(10 - pct);
const muted = mutes.isMuted(evt) ? " 🔇" : "";
return `\`${evt.padEnd(22)}\` **${count}** \`${bar}\`${muted}`;
})
.join("\n");
return new EmbedBuilder()
.setColor(0xF39C12)
.setTitle(`📊 Event Breakdown — ${stats.eventsReceived} total`)
.setDescription(rows)
.setFooter({
text: `${stats.eventsSent} sent · ${stats.eventsMuted} muted · ${stats.eventsDropped} dropped · ${stats.eventsIgnored} ignored`,
})
.setTimestamp();
}
// ─── Component factories ──────────────────────────────────────────────────────
function btnDismiss(id = "dismiss") {
return new ButtonBuilder()
.setCustomId(id)
.setLabel("Dismiss")
.setEmoji("🗑️")
.setStyle(ButtonStyle.Secondary);
}
function rowDismiss(id = "dismiss") {
return new ActionRowBuilder().addComponents(btnDismiss(id));
}
function rowRefreshDismiss(refreshId, dismissId = "dismiss") {
return new ActionRowBuilder().addComponents(
new ButtonBuilder()
.setCustomId(refreshId)
.setLabel("Refresh")
.setEmoji("🔄")
.setStyle(ButtonStyle.Secondary),
btnDismiss(dismissId),
);
}
// Shows a brief ✅ Refreshed state; buttons auto-revert to Refresh/Dismiss
function rowRefreshedDismiss(dismissId = "dismiss") {
return new ActionRowBuilder().addComponents(
new ButtonBuilder()
.setCustomId("__noop__")
.setLabel("Refreshed")
.setEmoji("✅")
.setStyle(ButtonStyle.Success)
.setDisabled(true),
btnDismiss(dismissId),
);
}
// ─── Command definitions ──────────────────────────────────────────────────────
const EVENT_CHOICES = [
{ name: "push", value: "push" },
{ name: "pull_request", value: "pull_request" },
{ name: "issues", value: "issues" },
{ name: "issue_comment", value: "issue_comment" },
{ name: "pull_request_review", value: "pull_request_review" },
{ name: "release", value: "release" },
{ name: "workflow_run", value: "workflow_run" },
{ name: "star", value: "star" },
{ name: "fork", value: "fork" },
{ name: "create", value: "create" },
{ name: "delete", value: "delete" },
{ name: "check_run", value: "check_run" },
{ name: "deployment_status", value: "deployment_status" },
];
const slashCommands = [
new SlashCommandBuilder()
.setName("ping")
.setDescription("Check if the bot is alive and measure latency"),
new SlashCommandBuilder()
.setName("status")
.setDescription("Show bot status, uptime, and event statistics"),
new SlashCommandBuilder()
.setName("events")
.setDescription("Show a breakdown of all events received since bot started"),
new SlashCommandBuilder()
.setName("test")
.setDescription("Send a test embed to verify a channel is set up correctly")
.addStringOption(o =>
o.setName("channel").setDescription("Channel name to test (default: first configured)").setRequired(false)
),
new SlashCommandBuilder()
.setName("mute")
.setDescription("Silence an event type for a set duration — it's received but not posted")
.addStringOption(o =>
o.setName("event").setDescription("Event type to mute").setRequired(true).addChoices(...EVENT_CHOICES)
)
.addStringOption(o =>
o.setName("reason").setDescription("Optional reason (shown in /watchlist)").setRequired(false)
),
new SlashCommandBuilder()
.setName("watchlist")
.setDescription("Show all active mutes with one-click Unmute buttons"),
new SlashCommandBuilder()
.setName("digest")
.setDescription("Show a live digest of recent GitHub activity")
.addIntegerOption(o =>
o.setName("count")
.setDescription("How many events to show (5–25, default 10)")
.setMinValue(5)
.setMaxValue(25)
.setRequired(false)
),
new SlashCommandBuilder()
.setName("clear-stats")
.setDescription("Reset all event counters — requires confirmation"),
].map(cmd => cmd.toJSON());
const contextMenus = [
new ContextMenuCommandBuilder()
.setName("📌 Pin to GitHub log")
.setType(ApplicationCommandType.Message)
.toJSON(),
new ContextMenuCommandBuilder()
.setName("🔁 Resend this embed")
.setType(ApplicationCommandType.Message)
.toJSON(),
];
const allCommands = [...slashCommands, ...repoCommands.map(c => c.toJSON()), helpCommand, ...contextMenus];
// ─── Initialize Database ───────────────────────────────────────────────────────
// Initialize database before starting
try {
db.init();
console.log("[db] Database initialized");
} catch (err) {
console.error("[db] Failed to initialize:", err.message);
}
// ─── GitHub Poller ────────────────────────────────────────────────────────────
const githubPoller = new poller.GitHubPoller({
interval: 60000, // Poll every minute
onEvent: (eventType, payload, repo) => {
handlePolledEvent(eventType, payload, repo, client);
},
});
// ─── Register ─────────────────────────────────────────────────────────────────
async function registerCommands() {
const rest = new REST({ version: "10" }).setToken(process.env.DISCORD_TOKEN);
try {
console.log("⏳ Registering commands…");
await rest.put(
Routes.applicationGuildCommands(client.user.id, process.env.DISCORD_GUILD_ID),
{ body: allCommands }
);
console.log(`✅ Registered ${allCommands.length} commands.`);
} catch (err) {
console.error("❌ Failed to register:", err.message);
}
}
// ─── Presence rotation ────────────────────────────────────────────────────────
const presenceMessages = [
() => ({ name: "GitHub webhooks · V3", type: ActivityType.Watching }),
() => ({ name: `${stats.eventsReceived} events`, type: ActivityType.Playing }),
() => {
const mins = Math.floor((Date.now() - stats.startTime) / 60_000);
return { name: `up ${mins}m`, type: ActivityType.Playing };
},
() => {
const last = stats.lastEvent;
return last
? { name: `last: ${last}`, type: ActivityType.Watching }
: { name: "awaiting events…", type: ActivityType.Watching };
},
];
let presenceIdx = 0;
function rotatePresence() {
const msg = presenceMessages[presenceIdx++ % presenceMessages.length]();
client.user.setPresence({ status: "online", activities: [msg] });
}
// ─── Channel resolver ─────────────────────────────────────────────────────────
async function getChannel(name) {
const guild = client.guilds.cache.get(process.env.DISCORD_GUILD_ID);
if (!guild) { console.error("[bot] Guild not found"); return null; }
let ch = guild.channels.cache.find(c => c.name === name && c.isTextBased());
if (!ch) {
try {
const all = await guild.channels.fetch();
ch = all.find(c => c?.name === name && c.isTextBased()) || null;
} catch (e) { console.error(`[bot] fetch channels: ${e.message}`); }
}
if (!ch) console.warn(`[bot] Channel "#${name}" not found.`);
return ch || null;
}
// ─────────────────────────────────────────────────────────────────────────────
// INTERACTION HANDLER
// ─────────────────────────────────────────────────────────────────────────────
client.on("interactionCreate", async (interaction) => {
// Let /help (with its dropdown + pagination) handle itself first
if (await handleHelpInteraction(interaction)) return;
// ══ Slash commands ══════════════════════════════════════════════════════════
if (interaction.isChatInputCommand()) {
const cmd = interaction.commandName;
// ── /ping ────────────────────────────────────────────────────────────────
if (cmd === "ping") {
const sent = await interaction.reply({ content: "🏓 Pinging…", fetchReply: true });
const latency = sent.createdTimestamp - interaction.createdTimestamp;
const ws = client.ws.ping;
// Colour-coded 10-block bar
const bar = (ms) => {
const blocks = Math.min(10, Math.max(1, Math.round(ms / 20)));
const color = ms < 80 ? "🟩" : ms < 200 ? "🟨" : "🟥";
return color.repeat(blocks) + "⬛".repeat(10 - blocks);
};
const embed = new EmbedBuilder()
.setColor(latency < 80 ? 0x2ECC71 : latency < 200 ? 0xF39C12 : 0xE74C3C)
.setTitle("🏓 Pong!")
.addFields(
{ name: "Round-trip", value: `${bar(latency)}\n**${latency}ms**`, inline: true },
{ name: "WebSocket", value: `${bar(ws)}\n**${ws}ms**`, inline: true },
)
.setTimestamp();
await interaction.editReply({
content: "",
embeds: [embed],
components: [rowDismiss("ping:dismiss")],
});
return;
}
// ── /status ──────────────────────────────────────────────────────────────
if (cmd === "status") {
await interaction.reply({
embeds: [buildStatusEmbed()],
components: [rowRefreshDismiss("status:refresh", "status:dismiss")],
});
return;
}
// ── /events ──────────────────────────────────────────────────────────────
if (cmd === "events") {
const embed = buildEventsEmbed();
if (!embed) {
return interaction.reply({ content: "📭 No events received yet since bot started.", ephemeral: true });
}
await interaction.reply({
embeds: [embed],
components: [rowRefreshDismiss("events:refresh", "events:dismiss")],
});
return;
}
// ── /test ────────────────────────────────────────────────────────────────
if (cmd === "test") {
let chName = (interaction.options.getString("channel") || "").replace(/^#/, "");
if (!chName) {
// Default to the first active repo's channel, or a fallback name
const repos = db.getAllRepositories();
const first = repos.find(r => r.channel_id);
if (first) {
const ch = client.channels.cache.get(first.channel_id);
chName = ch?.name || "github-general";
} else {
chName = "github-general";
}
}
const ch = await getChannel(chName);
if (!ch) {
return interaction.reply({
content: `❌ Channel **#${chName}** not found. Make sure it exists and I have access.`,
ephemeral: true,
});
}
const port = process.env.WEBHOOK_PORT || 3000;
const testEmbed = new EmbedBuilder()
.setColor(0x5865F2)
.setAuthor({ name: "GitBot V3", iconURL: client.user.displayAvatarURL() })
.setTitle("🧪 Test Notification")
.setDescription(
"If you can see this, GitBot can post to this channel.\n\n" +
"Use the buttons below to confirm or resend the test."
)
.addFields(
{ name: "Webhook URL", value: `\`http://YOUR_IP:${port}/webhook\``, inline: false },
{ name: "Health Check", value: `\`http://YOUR_IP:${port}/health\``, inline: false },
{ name: "Channel", value: `<#${ch.id}>`, inline: true },
{ name: "Tested by", value: `<@${interaction.user.id}>`, inline: true },
)
.setTimestamp();
// ✅ Looks good! deletes the embed. 🔁 Resend sends a fresh copy.
const testRow = new ActionRowBuilder().addComponents(
new ButtonBuilder()
.setCustomId(`test:ok:${ch.id}`)
.setLabel("Looks good!")
.setEmoji("✅")
.setStyle(ButtonStyle.Success),
new ButtonBuilder()
.setCustomId(`test:resend:${ch.id}:${chName}`)
.setLabel("Resend")
.setEmoji("🔁")
.setStyle(ButtonStyle.Secondary),
);
const sent = await ch.send({ embeds: [testEmbed], components: [testRow] });
await interaction.reply({
content: `✅ Test embed sent to **#${chName}** — [jump to it](${sent.url})\nClick **Looks good!** on the embed to dismiss it.`,
ephemeral: true,
});
return;
}
// ── /mute ────────────────────────────────────────────────────────────────
if (cmd === "mute") {
const eventArg = interaction.options.getString("event");
const reason = interaction.options.getString("reason") || "";
// Already muted? Show current state + Unmute button
const existing = mutes.getMute(eventArg);
if (existing) {
const left = Math.ceil((existing.expiresAt.getTime() - Date.now()) / 60_000);
const expires = `<t:${Math.floor(existing.expiresAt.getTime() / 1000)}:R>`;
const unmuteRow = new ActionRowBuilder().addComponents(
new ButtonBuilder()
.setCustomId(`mute:unmute:${eventArg}`)
.setLabel(`Unmute \`${eventArg}\` now`)
.setEmoji("🔔")
.setStyle(ButtonStyle.Danger),
new ButtonBuilder()
.setCustomId("mute:cancel")
.setLabel("Dismiss")
.setEmoji("🗑️")
.setStyle(ButtonStyle.Secondary),
);
return interaction.reply({
content: `🔇 **\`${eventArg}\`** is already muted for **${left}m** more (expires ${expires}).\nUnmute it now or let it expire.`,
components: [unmuteRow],
ephemeral: true,
});
}
// Duration picker
const muteEmbed = new EmbedBuilder()
.setColor(0xF39C12)
.setTitle(`🔇 Mute \`${eventArg}\``)
.setDescription(
`How long should **\`${eventArg}\`** be silenced?\n\n` +
"Events will still be received and counted — just not posted to Discord." +
(reason ? `\n\n**Reason:** ${reason}` : "")
)
.setFooter({ text: "Pick a duration below" });
const safeReason = encodeURIComponent(reason.slice(0, 50));
const durationRow = new ActionRowBuilder().addComponents(
new ButtonBuilder()
.setCustomId(`mute:apply:${eventArg}:900000:${safeReason}`)
.setLabel("15 min")
.setStyle(ButtonStyle.Secondary),
new ButtonBuilder()
.setCustomId(`mute:apply:${eventArg}:3600000:${safeReason}`)
.setLabel("1 hour")
.setStyle(ButtonStyle.Secondary),
new ButtonBuilder()
.setCustomId(`mute:apply:${eventArg}:21600000:${safeReason}`)
.setLabel("6 hours")
.setStyle(ButtonStyle.Secondary),
new ButtonBuilder()
.setCustomId(`mute:apply:${eventArg}:86400000:${safeReason}`)
.setLabel("24 hours")
.setStyle(ButtonStyle.Danger),
new ButtonBuilder()
.setCustomId("mute:cancel")
.setLabel("Cancel")
.setEmoji("❌")
.setStyle(ButtonStyle.Secondary),
);
await interaction.reply({ embeds: [muteEmbed], components: [durationRow], ephemeral: true });
return;
}
// ── /watchlist ────────────────────────────────────────────────────────────
if (cmd === "watchlist") {
const activeMutes = mutes.list();
if (activeMutes.length === 0) {
return interaction.reply({
embeds: [new EmbedBuilder()
.setColor(0x2ECC71)
.setTitle("✅ No Active Mutes")
.setDescription("All event types are currently active.")
.setTimestamp()],
components: [rowDismiss("watchlist:dismiss")],
ephemeral: true,
});
}
const embed = new EmbedBuilder()
.setColor(0x9B59B6)
.setTitle(`🔇 Active Mutes (${activeMutes.length})`)
.setDescription("Click an **Unmute** button to lift a mute early.")
.addFields(
activeMutes.map(mu => {
const left = Math.ceil((mu.expiresAt.getTime() - Date.now()) / 60_000);
const expires = `<t:${Math.floor(mu.expiresAt.getTime() / 1000)}:R>`;
return {
name: `\`${mu.eventType}\``,
value: `Expires ${expires} (${left}m left)\nBy <@${mu.mutedBy}>${mu.reason ? `\n> ${mu.reason}` : ""}`,
inline: true,
};
})
)
.setTimestamp();
// One Unmute button per muted event (5 per row, max 4 rows + 1 dismiss row)
const btnRows = [];
for (const ch of chunks(activeMutes, 5).slice(0, 4)) {
btnRows.push(new ActionRowBuilder().addComponents(
ch.map(mu =>
new ButtonBuilder()
.setCustomId(`mute:unmute:${mu.eventType}`)
.setLabel(`Unmute ${mu.eventType}`)
.setEmoji("🔔")
.setStyle(ButtonStyle.Danger)
)
));
}
btnRows.push(rowDismiss("watchlist:dismiss"));
await interaction.reply({ embeds: [embed], components: btnRows, ephemeral: true });
return;
}
// ── /digest ───────────────────────────────────────────────────────────────
if (cmd === "digest") {
const count = interaction.options.getInteger("count") ?? 10;
const entries = digest.recent(count);
await interaction.reply(buildDigestPayload(entries, count));
return;
}
// ── /clear-stats ──────────────────────────────────────────────────────────
if (cmd === "clear-stats") {
const embed = new EmbedBuilder()
.setColor(0xE74C3C)
.setTitle("⚠️ Reset All Statistics?")
.setDescription(
"This will zero out **all** counters and reset the uptime clock.\n\n" +
"The digest ring buffer is **not** cleared.\n\n" +
"_This action cannot be undone._"
)
.setFooter({ text: "This confirmation expires in 30 seconds" });
const confirmRow = new ActionRowBuilder().addComponents(
new ButtonBuilder()
.setCustomId("clearstats:confirm")
.setLabel("Yes, reset everything")
.setEmoji("🗑️")
.setStyle(ButtonStyle.Danger),
new ButtonBuilder()
.setCustomId("clearstats:cancel")
.setLabel("Never mind")
.setEmoji("❌")
.setStyle(ButtonStyle.Secondary),
);
await interaction.reply({ embeds: [embed], components: [confirmRow], ephemeral: true });
setTimeout(async () => {
try {
await interaction.editReply({
components: [new ActionRowBuilder().addComponents(
new ButtonBuilder().setCustomId("__n3__").setLabel("Yes, reset everything").setEmoji("🗑️").setStyle(ButtonStyle.Danger).setDisabled(true),
new ButtonBuilder().setCustomId("__n4__").setLabel("Never mind").setEmoji("❌").setStyle(ButtonStyle.Secondary).setDisabled(true),
)],
});
} catch { /* gone */ }
}, 30_000);
return;
}
// ── /repo commands ──────────────────────────────────────────────────────
if (cmd === "repo") {
return handleRepoCommand(interaction);
}
// ── /admin commands ──────────────────────────────────────────────────────
if (cmd === "admin") {
return handleAdminCommand(interaction);
}
}
// ══ Context menus ════════════════════════════════════════════════════════════
else if (interaction.isMessageContextMenuCommand()) {
const cmd = interaction.commandName;
const message = interaction.targetMessage;
// ── "📌 Pin to GitHub log" ──────────────────────────────────────────────
if (cmd === "📌 Pin to GitHub log") {
const cfg = loadConfig();
const logChName = cfg.log_channel || "github-log";
const logChannel = await getChannel(logChName);
if (!logChannel) {
return interaction.reply({
content: `❌ Log channel **#${logChName}** not found.\nAdd \`"log_channel": "channel-name"\` to \`config.json\`.`,
ephemeral: true,
});
}
// Build a pin-frame embed, then optionally include the original embed
const pinEmbed = new EmbedBuilder()
.setColor(0x5865F2)
.setAuthor({
name: `📌 Pinned by ${interaction.user.username}`,
iconURL: interaction.user.displayAvatarURL(),
})
.setDescription(message.content || "_No text content_")
.addFields(
{ name: "Source", value: `<#${message.channelId}>`, inline: true },
{ name: "Author", value: message.author ? `<@${message.author.id}>` : "_unknown_", inline: true },
{ name: "Jump", value: `[View original](${message.url})`, inline: true },
)
.setTimestamp(message.createdAt);
const toSend = message.embeds.length > 0
? [pinEmbed, EmbedBuilder.from(message.embeds[0])]
: [pinEmbed];
// Pin message has an "Acknowledged" button to mark it as reviewed
const ackRow = new ActionRowBuilder().addComponents(
new ButtonBuilder()
.setCustomId(`pin:ack:${interaction.user.id}`)
.setLabel("Acknowledged")
.setEmoji("✅")
.setStyle(ButtonStyle.Secondary),
);
await logChannel.send({ embeds: toSend, components: [ackRow] });
await interaction.reply({ content: `📌 Pinned to **#${logChName}**!`, ephemeral: true });
return;
}
// ── "🔁 Resend this embed" ───────────────────────────────────────────────
if (cmd === "🔁 Resend this embed") {
if (message.author?.id !== client.user.id) {
return interaction.reply({
content: "❌ Only GitBot's own messages can be resent.",
ephemeral: true,
});
}
if (message.embeds.length === 0) {
return interaction.reply({ content: "❌ That message has no embeds.", ephemeral: true });
}
// Build the channel list from active repos (V3 — DB-driven)
const repos = db.getAllRepositories();
const chNames = [...new Set(
repos
.map(r => client.channels.cache.get(r.channel_id)?.name)
.filter(Boolean)
)];
if (chNames.length === 0) {
return interaction.reply({ content: "❌ No monitored channels found. Add a repo with `/repo add` first.", ephemeral: true });
}
const pickerEmbed = new EmbedBuilder()
.setColor(0x3498DB)
.setTitle("🔁 Resend Embed — Pick a channel")
.setDescription("Choose which channel to resend this embed to:")
.setFooter({ text: "Shows channels for currently monitored repositories" });
// Up to 4 channel buttons + cancel
const pickerRow = new ActionRowBuilder().addComponents(
...chNames.slice(0, 4).map(ch =>
new ButtonBuilder()
.setCustomId(`resend:${message.id}:${ch}`)
.setLabel(`#${ch}`)
.setStyle(ButtonStyle.Primary)
),
new ButtonBuilder()
.setCustomId("resend:cancel")
.setLabel("Cancel")
.setEmoji("❌")
.setStyle(ButtonStyle.Secondary),
);
await interaction.reply({ embeds: [pickerEmbed], components: [pickerRow], ephemeral: true });
return;
}
}
// ══ Button handlers ══════════════════════════════════════════════════════════
else if (interaction.isButton()) {
const id = interaction.customId;
// ── Handle repo interactions (from /repo info) ─────────────────────────────
if (id.startsWith("repo:")) {
if (await handleRepoInteraction(interaction)) return;
}
// ── Generic / named dismissals ────────────────────────────────────────────
if (
id === "dismiss" ||
id.endsWith(":dismiss") ||
id === "ping:dismiss" ||
id === "mute:cancel" ||
id === "resend:cancel" ||
id === "clearstats:cancel"
) {
try { await interaction.message.delete(); } catch { /* already gone */ }
await interaction.deferUpdate().catch(() => {});
return;
}
// ── Status: Refresh ───────────────────────────────────────────────────────
if (id === "status:refresh") {
await interaction.update({
embeds: [buildStatusEmbed()],
components: [rowRefreshedDismiss("status:dismiss")],
});
setTimeout(async () => {
try {
await interaction.editReply({
components: [rowRefreshDismiss("status:refresh", "status:dismiss")],
});
} catch { /* gone */ }
}, 1500);
return;
}
// ── Events: Refresh ───────────────────────────────────────────────────────
if (id === "events:refresh") {
const embed = buildEventsEmbed();
if (!embed) {
await interaction.update({ content: "📭 No events yet.", embeds: [], components: [] });
return;
}
await interaction.update({
embeds: [embed],
components: [rowRefreshedDismiss("events:dismiss")],
});
setTimeout(async () => {
try {
await interaction.editReply({
components: [rowRefreshDismiss("events:refresh", "events:dismiss")],
});
} catch { /* gone */ }
}, 1500);
return;
}
// ── Test embed: Looks good! ────────────────────────────────────────────────
if (id.startsWith("test:ok:")) {
try { await interaction.message.delete(); } catch { /* gone */ }
await interaction.deferUpdate().catch(() => {});
return;
}
// ── Test embed: Resend ────────────────────────────────────────────────────
if (id.startsWith("test:resend:")) {
const [, , channelId, chName] = id.split(":");
const ch = client.channels.cache.get(channelId);
if (!ch) {
return interaction.reply({ content: "❌ Channel no longer found.", ephemeral: true });
}
const port = process.env.WEBHOOK_PORT || 3000;
const resendEmbed = new EmbedBuilder()
.setColor(0x5865F2)
.setAuthor({ name: "GitBot V3", iconURL: client.user.displayAvatarURL() })
.setTitle("🧪 Test Notification (Resent)")
.setDescription("Test embed resent on request.")
.addFields(
{ name: "Webhook URL", value: `\`http://YOUR_IP:${port}/webhook\``, inline: false },
{ name: "Resent by", value: `<@${interaction.user.id}>`, inline: true },
)
.setTimestamp();
const testRow = new ActionRowBuilder().addComponents(
new ButtonBuilder()
.setCustomId(`test:ok:${channelId}`)
.setLabel("Looks good!")
.setEmoji("✅")
.setStyle(ButtonStyle.Success),
new ButtonBuilder()
.setCustomId(`test:resend:${channelId}:${chName}`)
.setLabel("Resend")
.setEmoji("🔁")
.setStyle(ButtonStyle.Secondary),
);
await ch.send({ embeds: [resendEmbed], components: [testRow] });
await interaction.reply({ content: "🔁 Resent!", ephemeral: true });
return;
}
// ── Mute: Apply duration ──────────────────────────────────────────────────
if (id.startsWith("mute:apply:")) {
// mute:apply:<eventType>:<durationMs>:<safeReason>
const parts = id.split(":");
const eventType = parts[2];
const durationMs = parseInt(parts[3], 10);
const reason = parts[4] ? decodeURIComponent(parts[4]) : "";
mutes.mute(eventType, durationMs, interaction.user.id, reason);
const mins = Math.round(durationMs / 60_000);
const label = mins < 60 ? `${mins}m` : `${Math.round(mins / 60)}h`;
const expires = `<t:${Math.floor((Date.now() + durationMs) / 1000)}:R>`;
const muteSuccessEmbed = new EmbedBuilder()
.setColor(0x9B59B6)
.setTitle(`🔇 \`${eventType}\` muted for ${label}`)
.setDescription(
`Events of type **\`${eventType}\`** are silenced for **${label}**.\n` +
`Mute expires ${expires}.` +
(reason ? `\n\n**Reason:** ${reason}` : "")
)
.setFooter({ text: "Use /watchlist to see and manage all active mutes" })
.setTimestamp();
const unmuteRow = new ActionRowBuilder().addComponents(
new ButtonBuilder()
.setCustomId(`mute:unmute:${eventType}`)
.setLabel("Unmute now")
.setEmoji("🔔")
.setStyle(ButtonStyle.Danger),
new ButtonBuilder()
.setCustomId("mute:cancel")
.setLabel("Done")
.setEmoji("✅")
.setStyle(ButtonStyle.Secondary),
);
await interaction.update({ embeds: [muteSuccessEmbed], components: [unmuteRow] });
return;
}
// ── Mute: Unmute ──────────────────────────────────────────────────────────
if (id.startsWith("mute:unmute:")) {
const eventType = id.slice("mute:unmute:".length);
const removed = mutes.unmute(eventType);
const embed = new EmbedBuilder()
.setColor(0x2ECC71)
.setTitle(`🔔 \`${eventType}\` unmuted`)
.setDescription(
removed
? `**\`${eventType}\`** events will now be forwarded again.`
: `**\`${eventType}\`** wasn't muted.`
)
.setTimestamp();
await interaction.update({ embeds: [embed], components: [rowDismiss("mute:dismiss2")] });
return;
}
// ── Digest: Load more ─────────────────────────────────────────────────────
if (id.startsWith("digest:more:")) {
const current = parseInt(id.split(":")[2], 10);
const newCount = Math.min(current + 10, 50);
const entries = digest.recent(newCount);
await interaction.update(buildDigestPayload(entries, newCount));
return;
}
// ── Digest: Dismiss ────────────────────────────────────────────────────────
if (id === "digest:dismiss") {
try { await interaction.message.delete(); } catch { /* gone */ }
await interaction.deferUpdate().catch(() => {});
return;
}
// ── Clear-stats: Confirm ──────────────────────────────────────────────────
if (id === "clearstats:confirm") {
resetStats();
const embed = new EmbedBuilder()
.setColor(0x2ECC71)
.setTitle("✅ Statistics Reset")
.setDescription(
"All event counters and the uptime clock have been reset to zero.\n\n" +
"The digest ring buffer was preserved."
)
.setTimestamp();
await interaction.update({ embeds: [embed], components: [rowDismiss("clearstats:dismiss")] });
return;
}
// ── Pin: Acknowledge ──────────────────────────────────────────────────────
if (id.startsWith("pin:ack:")) {
await interaction.update({
components: [new ActionRowBuilder().addComponents(
new ButtonBuilder()
.setCustomId("__acked__")
.setLabel("Acknowledged")
.setEmoji("✅")
.setStyle(ButtonStyle.Secondary)
.setDisabled(true),
)],
});
return;
}
// ── Resend: Channel selected ──────────────────────────────────────────────
if (id.startsWith("resend:") && id !== "resend:cancel") {
const [, msgId, chName] = id.split(":");
const targetCh = await getChannel(chName);
if (!targetCh) {