File size: 10,483 Bytes
b12287d
8bc96db
e489270
 
 
6faeb24
e489270
 
 
6faeb24
 
 
 
 
 
 
 
 
 
073d3b4
b12287d
 
 
073d3b4
3f751c5
073d3b4
 
 
 
 
 
 
8bc96db
b12287d
8bc96db
073d3b4
3f751c5
 
 
 
 
 
073d3b4
 
 
b12287d
073d3b4
 
 
b12287d
 
 
073d3b4
b12287d
 
073d3b4
e60447d
b12287d
073d3b4
 
 
 
b12287d
073d3b4
 
 
 
 
 
 
 
 
8bc96db
 
073d3b4
b12287d
 
 
 
 
073d3b4
b12287d
 
 
 
073d3b4
b12287d
073d3b4
 
 
b12287d
073d3b4
 
 
 
 
 
 
 
 
 
e489270
6faeb24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f751c5
 
 
 
 
 
 
 
 
 
 
 
6faeb24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f751c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e489270
 
 
 
6faeb24
073d3b4
e489270
073d3b4
 
 
e489270
b12287d
 
 
 
 
 
 
 
6faeb24
 
b12287d
6faeb24
 
 
b12287d
 
 
 
 
 
 
 
 
 
f21767a
b12287d
e489270
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
import { Client, GatewayIntentBits, Partials } from 'discord.js'
import { Agent } from 'undici'
import { readdirSync } from 'node:fs'
import { resolve, dirname } from 'node:path'
import { fileURLToPath, pathToFileURL } from 'node:url'
import { loadGuildConfig } from './storage/cache.js'

const __dirname = dirname(fileURLToPath(import.meta.url))

const RACE_CONCURRENCY   = 5        // parallel attempts per round
const MAX_RETRIES        = -1       // rounds before giving up (-1 = infinite)
const RETRY_DELAY_MS     = 5_000    // wait between rounds (ms)
const CONNECT_TIMEOUT    = 60_000   // undici connect timeout per attempt (ms)

// How often to ping Discord's gateway to confirm the connection is alive.
// discord.js already sends its own heartbeats, but this lets US detect a
// zombie connection independently and trigger a reconnect if needed.
const KEEPALIVE_INTERVAL_MS = 30_000  // ping every 30 s
const KEEPALIVE_TIMEOUT_MS  = 10_000  // if no pong within 10 s β†’ reconnect

// Passing a custom undici Agent via the `rest` option is the only way to
// override discord.js's hardcoded 10 s connect timeout β€” env vars and
// top-level Client options are ignored by undici entirely.
function createClient() {
  const c = new Client({
    intents: [
      GatewayIntentBits.Guilds,
      GatewayIntentBits.GuildMembers,
      GatewayIntentBits.GuildMessages,
      GatewayIntentBits.MessageContent,
    ],
    partials: [Partials.GuildMember],
    rest: {
      agent: new Agent({ connect: { timeout: CONNECT_TIMEOUT } }),
    },
  })

  c.on('error', (err) => {
    console.warn('[bot] Client error event:', err.message)
  })

  return c
}

// Single connection attempt β€” resolves with the ready client or rejects on failure
function attemptLogin(token, attemptNum) {
  return new Promise((resolve, reject) => {
    const c = createClient()

    // Our own outer guard β€” slightly longer than CONNECT_TIMEOUT so the
    // undici error surfaces first with a meaningful message when possible.
    const timeout = setTimeout(() => {
      c.destroy()
      reject(new Error(`Attempt ${attemptNum} timed out`))
    }, CONNECT_TIMEOUT + 5_000)

    c.once('clientReady', () => {
      clearTimeout(timeout)
      resolve(c)
    })

    c.login(token).catch((err) => {
      clearTimeout(timeout)
      c.destroy()
      reject(err)
    })
  })
}

// Race RACE_CONCURRENCY attempts β€” first to succeed wins, losers are destroyed
function raceConnections(token, round) {
  return new Promise((resolve, reject) => {
    let settled  = false
    let failures = 0

    const attempts = Array.from({ length: RACE_CONCURRENCY }, (_, i) =>
      attemptLogin(token, `round=${round} slot=${i + 1}`)
    )

    attempts.forEach((p, i) => {
      p.then((winner) => {
        if (settled) {
          winner.destroy()
          return
        }
        settled = true
        console.log(`[bot] Connection established (round=${round} slot=${i + 1})`)
        resolve(winner)
      }).catch((err) => {
        failures++
        console.warn(`[bot] Attempt failed (round=${round} slot=${i + 1}): ${err.message}`)
        if (!settled && failures === RACE_CONCURRENCY) {
          reject(new Error(`All ${RACE_CONCURRENCY} attempts failed in round ${round}`))
        }
      })
    })
  })
}

// Exported client reference β€” populated once connected
export let client = null

// -------------------------------------------------------------------
// Load guild configs for all guilds the client is currently in.
// Called after every successful connection or reconnect so the cache
// is always warm regardless of whether the ready event re-fires.
// -------------------------------------------------------------------
export async function loadAllGuildConfigs(c) {
  for (const [guildId, guild] of c.guilds.cache) {
    try {
      await loadGuildConfig(guild)
      console.log(`[bot] Loaded guild config for ${guild.name} (${guildId})`)
    } catch (err) {
      console.error(`[bot] Failed to load guild config for ${guildId}: ${err.message}`)
    }
  }
}

// -------------------------------------------------------------------
// Keepalive: ping the gateway WS every KEEPALIVE_INTERVAL_MS.
// If we don't get a response within KEEPALIVE_TIMEOUT_MS we consider
// the connection zombie and force a full reconnect.
// -------------------------------------------------------------------
let keepaliveTimer   = null
let reconnectPending = false

function startKeepalive(c, onZombie) {
  stopKeepalive()

  keepaliveTimer = setInterval(async () => {
    if (reconnectPending) return

    try {
      const shard = c.ws?.shards?.first?.() ?? c.ws
      if (!shard) return
      const latency = shard.ping
      if (latency === -1 || latency === null || latency === undefined) {
        throw new Error('gateway ping is -1, connection likely dead')
      }
      console.log('[bot] Keepalive ping OK β€” gateway latency ' + latency + ' ms')
    } catch (err) {
      console.warn(`[bot] Keepalive ping FAILED: ${err.message} β€” triggering reconnect`)
      reconnectPending = true
      stopKeepalive()
      onZombie()
    }
  }, KEEPALIVE_INTERVAL_MS)
}

function stopKeepalive() {
  if (keepaliveTimer) {
    clearInterval(keepaliveTimer)
    keepaliveTimer = null
  }
}

// -------------------------------------------------------------------
// Wire up disconnect/error listeners so we reconnect automatically.
// -------------------------------------------------------------------
function attachReconnectListeners(c, events, token) {
  // discord.js fires this when the gateway WS closes unexpectedly
  c.on('shardDisconnect', async (closeEvent, shardId) => {
    if (reconnectPending) return
    reconnectPending = true
    console.warn(`[bot] Shard ${shardId} disconnected (code ${closeEvent.code}) β€” reconnecting...`)
    stopKeepalive()
    await reconnect(events, token)
  })

  // Gateway-level errors (network drop, TLS failure, etc.)
  c.on('shardError', async (err, shardId) => {
    if (reconnectPending) return
    reconnectPending = true
    console.warn(`[bot] Shard ${shardId} error: ${err.message} β€” reconnecting...`)
    stopKeepalive()
    // Small delay so any in-flight cleanup can finish
    await new Promise((r) => setTimeout(r, 1_000))
    await reconnect(events, token)
  })

  c.on('error', (err) => {
    if (reconnectPending) return
    if (err.code === 'ECONNRESET' ||
        err.message.includes('TLS') ||
        err.message.includes('socket')) {
      reconnectPending = true
      console.warn('[bot] Client ECONNRESET detected β€” reconnecting...')
      stopKeepalive()
      reconnect(events, token)
    }
  })
}

// -------------------------------------------------------------------
// Reconnect β€” tear down current client, race new connections.
// -------------------------------------------------------------------
async function reconnect(events, token) {
  console.log('[bot] Attempting reconnect...')

  // Gracefully destroy the old client if it's still around
  try { client?.destroy() } catch (_) {}
  client = null

  let round = 0
  while (MAX_RETRIES === -1 || round < MAX_RETRIES) {
    round++
    console.log(`[bot] Reconnect round ${round}/${MAX_RETRIES === -1 ? '∞' : MAX_RETRIES}`)

    try {
      const winner = await raceConnections(token, round)
      registerEvents(winner, events)
      attachReconnectListeners(winner, events, token)
      client = winner
      reconnectPending = false
      await loadAllGuildConfigs(winner)
      startKeepalive(winner, () => reconnect(events, token))
      console.log('[bot] Reconnect successful')
      return
    } catch (err) {
      console.error(`[bot] Reconnect round ${round} failed: ${err.message}`)
      if (MAX_RETRIES !== -1 && round >= MAX_RETRIES) {
        console.error('[bot] Giving up β€” process will exit')
        process.exit(1)
      }
      console.log(`[bot] Retrying reconnect in ${RETRY_DELAY_MS / 1000}s...`)
      await new Promise((r) => setTimeout(r, RETRY_DELAY_MS))
    }
  }
}

// Register loaded event modules on a client instance
function registerEvents(c, events) {
  for (const event of events) {
    if (event.once) {
      c.once(event.name, (...args) => event.execute(...args))
    } else {
      c.on(event.name, (...args) => event.execute(...args))
    }
  }
}

process.on('uncaughtException', (err) => {
  console.error('[bot] Uncaught exception:', err.message)
  if (err.code === 'ECONNRESET' ||
      err.message.includes('TLS') ||
      err.message.includes('socket')) {
    console.warn('[bot] Network error β€” process will continue')
    return
  }
  console.error('[bot] Fatal error β€” exiting')
  process.exit(1)
})
process.on('unhandledRejection', (reason) => {
  console.error('[bot] Unhandled rejection:', reason?.message ?? reason)
})

export async function startBot() {
  const token = process.env.DISCORD_TOKEN
  if (!token) throw new Error('Missing required environment variable: DISCORD_TOKEN')

  // Load events once β€” registered on whichever client wins each race
  const eventsDir  = resolve(__dirname, 'events')
  const eventFiles = readdirSync(eventsDir).filter((f) => f.endsWith('.js'))
  const events     = await Promise.all(
    eventFiles.map((file) => import(pathToFileURL(resolve(eventsDir, file)).href))
  )

  let round = 0

  while (MAX_RETRIES === -1 || round < MAX_RETRIES) {
    round++
    console.log(`[bot] Starting Discord client... (round ${round}/${MAX_RETRIES === -1 ? '∞' : MAX_RETRIES})`)

    try {
      const winner = await raceConnections(token, round)
      registerEvents(winner, events)
      attachReconnectListeners(winner, events, token)
      client = winner
      reconnectPending = false
      await loadAllGuildConfigs(winner)
      startKeepalive(winner, () => reconnect(events, token))
      return
    } catch (err) {
      console.error(`[bot] Round ${round} failed: ${err.message}`)

      if (MAX_RETRIES !== -1 && round >= MAX_RETRIES) {
        throw new Error(`[bot] Giving up after ${MAX_RETRIES} rounds`)
      }

      console.log(`[bot] Retrying in ${RETRY_DELAY_MS / 1000}s...`)
      await new Promise((r) => setTimeout(r, RETRY_DELAY_MS))
    }
  }
}