Spaces:
Sleeping
Sleeping
Upload 12 files
Browse files- src/bot.js +108 -26
src/bot.js
CHANGED
|
@@ -5,40 +5,122 @@ import { fileURLToPath, pathToFileURL } from 'node:url'
|
|
| 5 |
|
| 6 |
const __dirname = dirname(fileURLToPath(import.meta.url))
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
export async function startBot() {
|
| 19 |
const token = process.env.DISCORD_TOKEN
|
| 20 |
if (!token) throw new Error('Missing required environment variable: DISCORD_TOKEN')
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
// Dynamically load all event files from src/events/
|
| 25 |
-
const eventsDir = resolve(__dirname, 'events')
|
| 26 |
const eventFiles = readdirSync(eventsDir).filter((f) => f.endsWith('.js'))
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
const filePath = pathToFileURL(resolve(eventsDir, file)).href
|
| 30 |
-
const event = await import(filePath)
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
client.on(event.name, (...args) => event.execute(...args))
|
| 36 |
-
}
|
| 37 |
-
}
|
| 38 |
|
| 39 |
-
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
}
|
|
|
|
| 5 |
|
| 6 |
const __dirname = dirname(fileURLToPath(import.meta.url))
|
| 7 |
|
| 8 |
+
const RACE_CONCURRENCY = 5 // number of parallel attempts per round
|
| 9 |
+
const MAX_RETRIES = 10 // total rounds before giving up (-1 = infinite)
|
| 10 |
+
const RETRY_DELAY_MS = 5_000 // wait between rounds (ms)
|
| 11 |
+
|
| 12 |
+
function createClient() {
|
| 13 |
+
return new Client({
|
| 14 |
+
intents: [
|
| 15 |
+
GatewayIntentBits.Guilds,
|
| 16 |
+
GatewayIntentBits.GuildMembers,
|
| 17 |
+
GatewayIntentBits.GuildMessages,
|
| 18 |
+
GatewayIntentBits.MessageContent,
|
| 19 |
+
],
|
| 20 |
+
partials: [Partials.GuildMember],
|
| 21 |
+
})
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
// Single connection attempt β resolves with the ready client or rejects on failure
|
| 25 |
+
function attemptLogin(token, attemptNum) {
|
| 26 |
+
return new Promise((resolve, reject) => {
|
| 27 |
+
const c = createClient()
|
| 28 |
+
|
| 29 |
+
const timeout = setTimeout(() => {
|
| 30 |
+
c.destroy()
|
| 31 |
+
reject(new Error(`Attempt ${attemptNum} timed out`))
|
| 32 |
+
}, 10_000)
|
| 33 |
+
|
| 34 |
+
c.once('ready', () => {
|
| 35 |
+
clearTimeout(timeout)
|
| 36 |
+
resolve(c)
|
| 37 |
+
})
|
| 38 |
+
|
| 39 |
+
c.login(token).catch((err) => {
|
| 40 |
+
clearTimeout(timeout)
|
| 41 |
+
c.destroy()
|
| 42 |
+
reject(err)
|
| 43 |
+
})
|
| 44 |
+
})
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
// Race RACE_CONCURRENCY attempts β first to succeed wins, losers are destroyed
|
| 48 |
+
function raceConnections(token, round) {
|
| 49 |
+
return new Promise((resolve, reject) => {
|
| 50 |
+
let settled = false
|
| 51 |
+
let failures = 0
|
| 52 |
+
|
| 53 |
+
const attempts = Array.from({ length: RACE_CONCURRENCY }, (_, i) =>
|
| 54 |
+
attemptLogin(token, `round=${round} slot=${i + 1}`)
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
attempts.forEach((p, i) => {
|
| 58 |
+
p.then((winner) => {
|
| 59 |
+
if (settled) {
|
| 60 |
+
// Another slot already won β destroy this one
|
| 61 |
+
winner.destroy()
|
| 62 |
+
return
|
| 63 |
+
}
|
| 64 |
+
settled = true
|
| 65 |
+
console.log(`[bot] Connection established (round=${round} slot=${i + 1})`)
|
| 66 |
+
|
| 67 |
+
// Destroy all other pending clients by rejecting won't help (they're
|
| 68 |
+
// fire-and-forget), but the timeout inside attemptLogin will clean them up.
|
| 69 |
+
resolve(winner)
|
| 70 |
+
}).catch((err) => {
|
| 71 |
+
failures++
|
| 72 |
+
console.warn(`[bot] Attempt failed (round=${round} slot=${i + 1}): ${err.message}`)
|
| 73 |
+
if (!settled && failures === RACE_CONCURRENCY) {
|
| 74 |
+
reject(new Error(`All ${RACE_CONCURRENCY} attempts failed in round ${round}`))
|
| 75 |
+
}
|
| 76 |
+
})
|
| 77 |
+
})
|
| 78 |
+
})
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
// Exported client reference β populated once connected
|
| 82 |
+
export let client = null
|
| 83 |
|
| 84 |
export async function startBot() {
|
| 85 |
const token = process.env.DISCORD_TOKEN
|
| 86 |
if (!token) throw new Error('Missing required environment variable: DISCORD_TOKEN')
|
| 87 |
|
| 88 |
+
// Load events once (they'll be registered on whichever client wins)
|
| 89 |
+
const eventsDir = resolve(__dirname, 'events')
|
|
|
|
|
|
|
| 90 |
const eventFiles = readdirSync(eventsDir).filter((f) => f.endsWith('.js'))
|
| 91 |
+
const events = await Promise.all(
|
| 92 |
+
eventFiles.map((file) => import(pathToFileURL(resolve(eventsDir, file)).href))
|
| 93 |
+
)
|
| 94 |
|
| 95 |
+
let round = 0
|
|
|
|
|
|
|
| 96 |
|
| 97 |
+
while (MAX_RETRIES === -1 || round < MAX_RETRIES) {
|
| 98 |
+
round++
|
| 99 |
+
console.log(`[bot] Starting Discord client... (round ${round}/${MAX_RETRIES === -1 ? 'β' : MAX_RETRIES})`)
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
+
try {
|
| 102 |
+
const winner = await raceConnections(token, round)
|
| 103 |
|
| 104 |
+
// Register all events on the winning client
|
| 105 |
+
for (const event of events) {
|
| 106 |
+
if (event.once) {
|
| 107 |
+
winner.once(event.name, (...args) => event.execute(...args))
|
| 108 |
+
} else {
|
| 109 |
+
winner.on(event.name, (...args) => event.execute(...args))
|
| 110 |
+
}
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
client = winner
|
| 114 |
+
return
|
| 115 |
+
} catch (err) {
|
| 116 |
+
console.error(`[bot] Round ${round} failed: ${err.message}`)
|
| 117 |
+
|
| 118 |
+
if (MAX_RETRIES !== -1 && round >= MAX_RETRIES) {
|
| 119 |
+
throw new Error(`[bot] Giving up after ${MAX_RETRIES} rounds`)
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
console.log(`[bot] Retrying in ${RETRY_DELAY_MS / 1000}s...`)
|
| 123 |
+
await new Promise((r) => setTimeout(r, RETRY_DELAY_MS))
|
| 124 |
+
}
|
| 125 |
+
}
|
| 126 |
}
|