Repository navigation
Expand file tree
/
Copy pathbot.js
More file actions
656 lines (579 loc) · 26.4 KB
/
Copy pathbot.js
File metadata and controls
656 lines (579 loc) · 26.4 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
/**
* Combat Pubs Discord Bot
*
* Polls the Nakama matchmaker and active match endpoints, then posts/edits
* pinned Discord embeds showing:
* - Who is currently queued for Combat
* - One pinned message per active Combat match (supports multiple games)
*
* Uses a Discord webhook for posting/editing.
* An optional DISCORD_BOT_TOKEN enables automatic pinning.
* Message IDs are persisted to state.json so edits survive restarts.
*/
import 'dotenv/config';
import { WebhookClient, EmbedBuilder } from 'discord.js';
import { readFileSync, writeFileSync, existsSync } from 'fs';
import { NakamaSession } from './nakama-auth.js';
// ── Config ────────────────────────────────────────────────────────────────────
const DISCORD_WEBHOOK_URL = process.env.DISCORD_WEBHOOK_URL || '';
const DISCORD_BOT_TOKEN = process.env.DISCORD_BOT_TOKEN || ''; // optional — enables pinning
const POLL_INTERVAL_MS = parseInt(process.env.POLL_INTERVAL_MS || '10000', 10);
const STATE_FILE = process.env.STATE_FILE || './state.json';
const FILTER_GUILD_ID = process.env.FILTER_GUILD_ID || '';
// Public status endpoint — no auth required, fallback when Nakama auth is unavailable
const STATUS_URL = 'https://g.echovrce.com/status/matches';
// Nakama authentication (optional — enables authenticated API access)
const NAKAMA_URL = process.env.NAKAMA_URL || '';
const NAKAMA_HTTP_KEY = process.env.NAKAMA_HTTP_KEY || '';
const NAKAMA_USERNAME = process.env.NAKAMA_USERNAME || '';
const NAKAMA_PASSWORD = process.env.NAKAMA_PASSWORD || '';
if (!DISCORD_WEBHOOK_URL) { console.error('[config] DISCORD_WEBHOOK_URL is required'); process.exit(1); }
// Parse webhook ID + token from URL (needed to resolve channel ID for pinning)
const _webhookParts = DISCORD_WEBHOOK_URL.match(/webhooks\/(\d+)\/([^/?]+)/);
const WEBHOOK_ID = _webhookParts?.[1] || '';
const WEBHOOK_TOKEN = _webhookParts?.[2] || '';
// ── Persistent state ──────────────────────────────────────────────────────────
/**
* @type {{
* queueMessageId: string|null,
* matchMessages: Record<string, string>,
* nakamaToken: string|null,
* nakamaRefreshToken: string|null
* }}
*/
let state = { queueMessageId: null, matchMessages: {}, nakamaToken: null, nakamaRefreshToken: null };
function loadState() {
try {
if (existsSync(STATE_FILE)) {
const saved = JSON.parse(readFileSync(STATE_FILE, 'utf8'));
state = { ...state, ...saved };
// Migrate legacy single matchMessageId to new map
if (state.matchMessageId) {
console.log('[state] Migrating legacy matchMessageId — deleting old message');
deleteDiscordMessage(state.matchMessageId).catch(() => { });
delete state.matchMessageId;
}
if (!state.matchMessages) state.matchMessages = {};
console.log('[state] Loaded state from state.json');
}
} catch (e) {
console.warn('[state] Failed to load state.json, starting fresh:', e.message);
}
if (!state.matchMessages) state.matchMessages = {};
}
function saveState() {
try {
writeFileSync(STATE_FILE, JSON.stringify(state, null, 2));
} catch (e) {
console.warn('[state] Failed to save state.json:', e.message);
}
}
// ── Nakama session ───────────────────────────────────────────────────────────
/** @type {NakamaSession|null} */
let nakamaSession = null;
let authDisabledPermanently = false;
function disableAuthPermanently(reason) {
authDisabledPermanently = true;
nakamaSession = null;
console.warn(`[nakama] Auth permanently disabled: ${reason}`);
}
function initNakamaSession() {
if (!NAKAMA_URL || !NAKAMA_HTTP_KEY || !NAKAMA_USERNAME || !NAKAMA_PASSWORD) {
console.log('[nakama] Auth not configured — using public status endpoint only');
return;
}
nakamaSession = new NakamaSession({
nakamaUrl: NAKAMA_URL,
httpKey: NAKAMA_HTTP_KEY,
username: NAKAMA_USERNAME,
password: NAKAMA_PASSWORD,
onTokensChanged({ token, refreshToken }) {
// Persist tokens alongside message IDs so they survive restarts
state.nakamaToken = token;
state.nakamaRefreshToken = refreshToken;
saveState();
},
});
// Restore tokens from previous session (avoids re-auth on restart)
if (state.nakamaToken || state.nakamaRefreshToken) {
nakamaSession.restoreTokens(state.nakamaToken, state.nakamaRefreshToken);
console.log('[nakama] Restored session tokens from state.json');
}
}
// ── Discord client + pinning ──────────────────────────────────────────────────
const webhook = new WebhookClient({ url: DISCORD_WEBHOOK_URL });
let channelId = null;
async function fetchChannelId() {
if (!WEBHOOK_ID || !WEBHOOK_TOKEN) return;
try {
const res = await fetch(`https://discord.com/api/v10/webhooks/${WEBHOOK_ID}/${WEBHOOK_TOKEN}`);
const data = await res.json().catch(() => ({}));
channelId = data.channel_id || null;
if (channelId) console.log(`[discord] Channel ID resolved: ${channelId}`);
} catch (e) {
console.warn('[discord] Could not resolve channel ID:', e.message);
}
}
async function pinMessage(messageId) {
if (!DISCORD_BOT_TOKEN || !channelId || !messageId) return;
try {
const res = await fetch(
`https://discord.com/api/v10/channels/${channelId}/pins/${messageId}`,
{ method: 'PUT', headers: { Authorization: `Bot ${DISCORD_BOT_TOKEN}` } }
);
if (res.ok || res.status === 204) {
console.log('[discord] Pinned message:', messageId);
// Small delay so the system notification has time to appear
await new Promise((r) => setTimeout(r, 1000));
await deletePinSystemMessage();
} else {
const text = await res.text().catch(() => '');
console.warn(`[discord] Could not pin ${messageId}:`, res.status, text);
}
} catch (e) {
console.warn('[discord] Pin error:', e.message);
}
}
async function deletePinSystemMessage() {
if (!DISCORD_BOT_TOKEN || !channelId) return;
try {
const res = await fetch(
`https://discord.com/api/v10/channels/${channelId}/messages?limit=5`,
{ headers: { Authorization: `Bot ${DISCORD_BOT_TOKEN}` } }
);
if (!res.ok) return;
const messages = await res.json().catch(() => []);
// Type 6 = channel pinned message system notification
const sysMsg = messages.find((m) => m.type === 6);
if (!sysMsg) return;
await fetch(
`https://discord.com/api/v10/channels/${channelId}/messages/${sysMsg.id}`,
{ method: 'DELETE', headers: { Authorization: `Bot ${DISCORD_BOT_TOKEN}` } }
);
console.log('[discord] Deleted pin system message:', sysMsg.id);
} catch (e) {
console.warn('[discord] Could not delete pin system message:', e.message);
}
}
async function unpinMessage(messageId) {
if (!DISCORD_BOT_TOKEN || !channelId || !messageId) return;
try {
await fetch(
`https://discord.com/api/v10/channels/${channelId}/pins/${messageId}`,
{ method: 'DELETE', headers: { Authorization: `Bot ${DISCORD_BOT_TOKEN}` } }
);
} catch (_) { }
}
async function deleteDiscordMessage(messageId) {
if (!messageId) return;
try {
await unpinMessage(messageId);
await webhook.deleteMessage(messageId);
console.log('[discord] Deleted message:', messageId);
} catch (e) {
if (e.code !== 10008 && !String(e.message).includes('Unknown Message')) {
console.warn('[discord] Could not delete message:', e.message);
}
}
}
// ── Nakama API ────────────────────────────────────────────────────────────────
/**
* Fetch match status via authenticated Nakama RPC.
* Uses the match/public RPC which returns the same data as the public status
* endpoint but through the authenticated API path.
*/
async function fetchAuthenticatedStatus() {
if (!nakamaSession || authDisabledPermanently) return null;
try {
const data = await nakamaSession.callRpc('match/public');
return data;
} catch (e) {
const message = e.message || 'authenticated fetch failed';
if (/HTTP 401/.test(message)) {
disableAuthPermanently(message);
} else {
console.error('[status] Authenticated fetch failed:', message);
disableAuthPermanently(message);
}
return null;
}
}
/**
* Fetch matchmaker ticket data via authenticated RPC.
* Returns an array of combat tickets, or null if unavailable.
*/
async function fetchMatchmakerTickets() {
if (!nakamaSession) return null;
try {
const data = await nakamaSession.callRpc('matchmaker/state', {});
const tickets = Array.isArray(data?.index) ? data.index : [];
return tickets.filter((t) => isCombat(t?.StringProperties?.game_mode));
} catch (e) {
// Non-fatal — queue embed falls back to count-only
return null;
}
}
/** Public endpoint — no auth required, used as fallback */
async function fetchPublicStatus() {
try {
const res = await fetch(STATUS_URL);
if (!res.ok) { console.error(`[status] HTTP ${res.status}`); return null; }
return res.json().catch(() => null);
} catch (e) { console.error('[status] Fetch error:', e.message); return null; }
}
/**
* Fetch status data. Prefers authenticated API when available,
* falls back to public endpoint on failure or when auth is not configured.
*/
async function fetchStatus() {
if (authDisabledPermanently) {
return {
statusData: await fetchPublicStatus(),
usedAuthenticatedPath: false,
};
}
if (nakamaSession) {
const data = await fetchAuthenticatedStatus();
if (data) {
return { statusData: data, usedAuthenticatedPath: true };
}
console.warn('[status] Falling back to public endpoint');
}
return {
statusData: await fetchPublicStatus(),
usedAuthenticatedPath: false,
};
}
// ── Data helpers ──────────────────────────────────────────────────────────────
function isCombat(modeStr) {
return String(modeStr || '').toLowerCase().includes('combat');
}
function normalizeTeam(player) {
const t = player?.team ?? player?.team_id ?? player?.teamId ?? player?.role ?? null;
if (typeof t === 'string') return t.toLowerCase();
if (typeof t === 'number') {
switch (t) {
case 0: return 'blue';
case 1: return 'orange';
case 2: return 'spectator';
default: return 'other';
}
}
return 'other';
}
function playerName(p) {
return p?.display_name || p?.displayName || p?.username || p?.user_id?.slice(0, 8) || 'Unknown';
}
function toFinite(v) { const n = Number(v); return Number.isFinite(n) ? n : null; }
const MAP_NAMES = {
mpl_combat_fission: 'Fission',
mpl_combat_combustion: 'Combustion',
mpl_combat_dyson: 'Dyson',
mpl_combat_gauss: 'Surge',
mpl_combat_rise: 'Rise',
mpl_combat_archaic: 'Archaic',
mpl_combat_harvest: 'Harvest',
};
function formatMapName(level) {
if (!level) return null;
return MAP_NAMES[level] || level.replace(/^mpl_combat_/, '').replace(/_/g, ' ').replace(/\b\w/g, (c) => c.toUpperCase());
}
// ── Embed builders ────────────────────────────────────────────────────────────
const COMBAT_RED = 0xdc2626;
const COMBAT_ORANGE = 0xf97316;
const MATCH_BLUE = 0x3b82f6;
function formatWaitTime(createdAtNs) {
if (!createdAtNs) return '';
const secs = Math.floor((Date.now() - createdAtNs / 1e6) / 1000);
if (secs < 0) return '';
if (secs < 60) return `${secs}s`;
const m = Math.floor(secs / 60);
const s = secs % 60;
return `${m}m ${s}s`;
}
/**
* @param {number} playerCount
* @param {Array|null} tickets - authenticated ticket data, or null for count-only mode
*/
function buildQueueEmbed(playerCount, tickets = null) {
const embed = new EmbedBuilder()
.setColor(COMBAT_RED)
.setTitle('⚔️ Combat Queue')
.setFooter({ text: 'Last updated' })
.setTimestamp(new Date());
if (tickets && tickets.length > 0) {
embed.setDescription(`# ${playerCount}\nplayer${playerCount === 1 ? '' : 's'} searching for a Combat match`);
const lines = tickets.map((t) => {
const name = t?.StringProperties?.display_name
|| t?.Presences?.[0]?.username
|| 'Unknown';
const wait = formatWaitTime(t?.CreatedAt);
const partySize = t?.Presences?.length || 1;
const partyStr = partySize > 1 ? ` (+${partySize - 1})` : '';
return wait ? `• **${name}**${partyStr} — ${wait}` : `• **${name}**${partyStr}`;
}).join('\n');
embed.addFields({ name: 'Searching', value: lines.slice(0, 1024) || '—', inline: false });
} else {
embed.setDescription(`# ${playerCount}\nplayer${playerCount === 1 ? '' : 's'} searching for a Combat match`);
}
return embed;
}
function buildMatchEmbed(game) {
const label = game.label;
const players = Array.isArray(label?.players) ? label.players : [];
const blueTeam = players.filter((p) => normalizeTeam(p) === 'blue');
const orangeTeam = players.filter((p) => normalizeTeam(p) === 'orange');
const spectators = players.filter((p) => ['spectator', 'other'].includes(normalizeTeam(p)));
const fullMatchId = game.match_id || '';
const matchUuid = fullMatchId.split('.')[0] || fullMatchId;
const matchIdShort = matchUuid.slice(0, 8) || '?';
const taxiUrl = matchUuid ? `https://echo.taxi/spark://c/${matchUuid}` : null;
const embed = new EmbedBuilder()
.setColor(COMBAT_RED)
.setTitle('🎮 Active Combat Match')
.setFooter({ text: `Match ${matchIdShort} · Last updated` })
.setTimestamp(new Date());
const gs = label?.game_state;
const infoLines = [`**Players:** ${label?.player_count || players.length} / ${label?.player_limit || '?'}`];
if (gs?.session_scoreboard?.game_time_ns != null) {
const secs = Math.floor(gs.session_scoreboard.game_time_ns / 1_000_000_000);
const m = Math.floor(Math.abs(secs) / 60);
const s = Math.abs(secs) % 60;
const timeStr = `${m}:${String(s).padStart(2, '0')}`;
infoLines.push(`**Time:** ${secs < 0 ? `Starting in ${timeStr}` : timeStr}`);
}
if (label?.open != null) infoLines.push(`**Lobby:** ${label.open ? '🟢 Open' : '🔴 Closed'}`);
const mapName = formatMapName(label?.level);
if (mapName) infoLines.push(`**Map:** ${mapName}`);
const region = label?.broadcaster?.region;
if (region) infoLines.push(`**Region:** ${region}`);
// Add payload fields if available (distance, checkpoint, scores)
const payload = label?.payload;
if (payload != null) {
const payloadLines = [];
if (payload.distance != null) payloadLines.push(`**Distance:** ${payload.distance.toFixed(2)}`);
if (payload.checkpoint != null) payloadLines.push(`**Checkpoint:** ${payload.checkpoint}`);
// Capture Point scores
if (payload.blue_points != null || payload.orange_points != null) {
const blueScore = payload.blue_points ?? 0;
const orangeScore = payload.orange_points ?? 0;
const round = (payload.total_round_count ?? 0) + 1;
payloadLines.push(`**Score:** 🔵 ${blueScore} - 🟠 ${orangeScore} (Round ${round})`);
}
if (payloadLines.length > 0) {
infoLines.push(payloadLines.join('\n'));
}
}
embed.addFields({ name: 'Match Info', value: infoLines.join('\n'), inline: false });
if (blueTeam.length > 0) embed.addFields({
name: `🔵 Blue Team (${blueTeam.length})`,
value: blueTeam.map((p) => `• ${playerName(p)}`).join('\n').slice(0, 1024) || '—',
inline: true,
});
if (orangeTeam.length > 0) embed.addFields({
name: `🟠 Orange Team (${orangeTeam.length})`,
value: orangeTeam.map((p) => `• ${playerName(p)}`).join('\n').slice(0, 1024) || '—',
inline: true,
});
if (spectators.length > 0) embed.addFields({
name: `👁 Spectators (${spectators.length})`,
value: spectators.map((p) => `• ${playerName(p)}`).join('\n').slice(0, 1024) || '—',
inline: false,
});
return { embed, taxiUrl, fullMatchId };
}
// ── Discord message management ────────────────────────────────────────────────
// Guard against concurrent queue message posts
let queuePostInProgress = false;
async function handleQueueMessage(playerCount, tickets = null) {
if (playerCount === 0) {
if (state.queueMessageId) {
await deleteDiscordMessage(state.queueMessageId);
state.queueMessageId = null;
saveState();
}
return;
}
const embed = buildQueueEmbed(playerCount, tickets);
try {
if (state.queueMessageId) {
await webhook.editMessage(state.queueMessageId, { embeds: [embed] });
} else {
if (queuePostInProgress) return; // another poll is already posting
queuePostInProgress = true;
try {
const msg = await webhook.send({ embeds: [embed] });
state.queueMessageId = msg.id;
saveState();
console.log('[discord] Posted queue message:', msg.id);
await pinMessage(msg.id);
} finally {
queuePostInProgress = false;
}
}
} catch (e) {
if (e.code === 10008 || String(e.message).includes('Unknown Message')) {
console.warn('[discord] Queue message deleted externally, will re-post next poll.');
state.queueMessageId = null;
saveState();
} else {
console.error('[discord] Queue embed error:', e.message);
}
}
}
async function handleAllMatchMessages(activeResults) {
const activeIds = new Set(activeResults.map((r) => r.fullMatchId));
// Delete messages for games that ended
for (const [matchId, msgId] of Object.entries(state.matchMessages)) {
if (!activeIds.has(matchId)) {
await deleteDiscordMessage(msgId);
delete state.matchMessages[matchId];
saveState();
}
}
// Post or edit a message for each active game
for (const { embed, taxiUrl, fullMatchId } of activeResults) {
const content = taxiUrl ? `🚕 **Join:** ${taxiUrl}` : '';
const existingMsgId = state.matchMessages[fullMatchId];
try {
if (existingMsgId) {
await webhook.editMessage(existingMsgId, { content, embeds: [embed] });
} else {
const msg = await webhook.send({ content, embeds: [embed] });
state.matchMessages[fullMatchId] = msg.id;
saveState();
console.log(`[discord] Posted match message for ${fullMatchId.slice(0, 8)}:`, msg.id);
await pinMessage(msg.id);
}
} catch (e) {
if (e.code === 10008 || String(e.message).includes('Unknown Message')) {
console.warn(`[discord] Match message for ${fullMatchId.slice(0, 8)} deleted externally, will re-post next poll.`);
delete state.matchMessages[fullMatchId];
saveState();
} else {
console.error(`[discord] Match embed error for ${fullMatchId.slice(0, 8)}:`, e.message);
}
}
}
}
// ── Main poll loop ────────────────────────────────────────────────────────────
const lastPoll = { tickets: -1, games: -1 };
let pollRunning = false;
async function poll() {
if (pollRunning) return; // skip if previous poll hasn't finished
pollRunning = true;
try {
const { statusData, usedAuthenticatedPath } = await fetchStatus();
if (!statusData) return;
// ── Queue count ───────────────────────────────────────────────────────
const mmCounts = statusData.active_matchmaking_counts || {};
let queueCount = 0;
const source = (FILTER_GUILD_ID && mmCounts[FILTER_GUILD_ID]) ? mmCounts[FILTER_GUILD_ID] : null;
if (source) {
for (const [mode, count] of Object.entries(source)) {
if (isCombat(mode)) queueCount += count;
}
} else {
for (const groupCounts of Object.values(mmCounts)) {
for (const [mode, count] of Object.entries(groupCounts)) {
if (isCombat(mode)) queueCount += count;
}
}
}
if (queueCount !== lastPoll.tickets) {
console.log(`[poll] Combat queue: ${queueCount}`);
lastPoll.tickets = queueCount;
}
const tickets = (usedAuthenticatedPath && queueCount > 0)
? await fetchMatchmakerTickets()
: null;
await handleQueueMessage(queueCount, tickets);
// ── Active matches ────────────────────────────────────────────────────
if (statusData?.labels) {
let combatGames = statusData.labels
.filter((label) =>
isCombat(label.mode) &&
label.lobby_type !== 'private' &&
!String(label.mode || '').toLowerCase().includes('private') &&
(label.player_count || label.players?.length || 0) > 0 &&
(!FILTER_GUILD_ID || label.group_id === FILTER_GUILD_ID)
)
.map((label) => ({ match_id: label.id, label }));
if (combatGames.length !== lastPoll.games) {
console.log(`[poll] Active combat games: ${combatGames.length}`);
lastPoll.games = combatGames.length;
}
await handleAllMatchMessages(combatGames.map((g) => buildMatchEmbed(g)));
}
} catch (e) {
console.error('[poll] Unhandled error:', e.message);
} finally {
pollRunning = false;
}
}
// ── Startup cleanup ───────────────────────────────────────────────────────────
/**
* On startup, scan the channel for any existing Combat Queue embeds posted by
* this webhook. If exactly one is found, adopt its ID so the bot edits it going
* forward instead of posting a new one. If multiple exist (e.g. from a previous
* double-post), delete the extras and keep the newest. This means the bot
* recovers cleanly even when state.json is wiped (e.g. Render ephemeral disk).
*/
async function recoverQueueMessageId() {
if (!DISCORD_BOT_TOKEN || !channelId || !WEBHOOK_ID) return;
try {
const res = await fetch(
`https://discord.com/api/v10/channels/${channelId}/messages?limit=50`,
{ headers: { Authorization: `Bot ${DISCORD_BOT_TOKEN}` } }
);
if (!res.ok) return;
const messages = await res.json().catch(() => []);
const queueMsgs = messages.filter((msg) =>
msg.webhook_id === WEBHOOK_ID &&
msg.embeds?.[0]?.title?.includes('Combat Queue')
);
if (queueMsgs.length === 0) {
state.queueMessageId = null;
} else {
// Sort newest first (Discord snowflake IDs are chronological)
queueMsgs.sort((a, b) => (BigInt(b.id) > BigInt(a.id) ? 1 : -1));
// Delete duplicates, keep the newest
for (const msg of queueMsgs.slice(1)) {
console.log('[discord] Deleting duplicate queue message:', msg.id);
await deleteDiscordMessage(msg.id);
}
state.queueMessageId = queueMsgs[0].id;
console.log('[discord] Adopted existing queue message:', queueMsgs[0].id);
}
saveState();
} catch (e) {
console.warn('[discord] Could not recover queue message ID:', e.message);
}
}
// ── Startup ───────────────────────────────────────────────────────────────────
console.log(`[bot] Combat Pubs Bot starting…`);
const nakamaConfigured = NAKAMA_URL && NAKAMA_HTTP_KEY && NAKAMA_USERNAME && NAKAMA_PASSWORD;
console.log(`[bot] Status: ${nakamaConfigured ? `${NAKAMA_URL} (authenticated)` : `${STATUS_URL} (public)`}`);
console.log(`[bot] Poll interval: ${POLL_INTERVAL_MS}ms`);
console.log(`[bot] Pinning: ${DISCORD_BOT_TOKEN ? 'enabled' : 'disabled (set DISCORD_BOT_TOKEN to enable)'}`);
loadState();
initNakamaSession();
fetchChannelId().then(async () => {
// Establish Nakama session before first poll (if configured)
if (nakamaSession) {
if (state.nakamaToken || state.nakamaRefreshToken) {
console.log('[bot] Nakama session ready (restored from state.json)');
} else {
try {
await nakamaSession.ensureSession();
console.log('[bot] Nakama session established');
} catch (e) {
disableAuthPermanently(e.message || 'initial authentication failed');
console.warn('[bot] Could not establish Nakama session, falling back to public endpoint');
}
}
}
await recoverQueueMessageId();
poll();
setInterval(poll, POLL_INTERVAL_MS);
});