From 6bf7c445224127702f282c0fd95cf5bc304375c0 Mon Sep 17 00:00:00 2001 From: Lindsay Gilbert Date: Tue, 25 Aug 2026 12:29:37 -0400 Subject: [PATCH] wait for discord login to start work --- .cursor/rules/discord-bot/RULE.md | 4 +- .cursor/rules/ops-gotchas/RULE.md | 4 +- AGENTS.md | 11 +- README.md | 2 +- src/Classes/MintBot.ts | 6 + src/index.ts | 184 ++++++++++++++++++------------ 6 files changed, 130 insertions(+), 81 deletions(-) diff --git a/.cursor/rules/discord-bot/RULE.md b/.cursor/rules/discord-bot/RULE.md index bb416e9a..41f4e37f 100644 --- a/.cursor/rules/discord-bot/RULE.md +++ b/.cursor/rules/discord-bot/RULE.md @@ -79,8 +79,8 @@ Non-`#` → `smartBotResponse.ts`. Channel must be present in active `channels*. ## OpenSea Dual Path -1. **Stream** (`@opensea/stream-js`): primary listings + sales; gated by `PRODUCTION_MODE` in handlers -2. **REST poll** (`OpenSeaEventsPollBot`): sales backfill; registers stream sale IDs to dedupe +1. **Stream** (`@opensea/stream-js`): primary listings + sales; started only after Discord login succeeds (`startProductionBots` in `index.ts`). Handlers also no-op when `PRODUCTION_MODE` is false. +2. **REST poll** (`OpenSeaEventsPollBot`): sales backfill; starts after Discord ready; registers stream sale IDs to dedupe Activity posts go through `activityTriager.ts` (hardcoded artist/platform → channel mapping + ban list). New sale destinations usually need code there, not only JSON. diff --git a/.cursor/rules/ops-gotchas/RULE.md b/.cursor/rules/ops-gotchas/RULE.md index 7eb18ef9..c5622698 100644 --- a/.cursor/rules/ops-gotchas/RULE.md +++ b/.cursor/rules/ops-gotchas/RULE.md @@ -13,7 +13,7 @@ Read `AGENTS.md` for the full matrix. This rule is the short always-on checklist |------|---------| | `ARTBOT_IS_PROD=true` | Load `channels.json` + `projectBots.json` | | `ARTBOT_IS_PROD` unset/false | Load `*_dev.json` (a-t test server) | -| `PRODUCTION_MODE=true` | Discord login + process OpenSea events + start poll bot | +| `PRODUCTION_MODE=true` | Discord login, then OpenSea stream/poll + scheduler | | `PRODUCTION_MODE=false` | Boot Express/indexers; skip Discord login (CI) | Never assume one flag implies the other. @@ -22,7 +22,7 @@ Never assume one flag implies the other. - Host: **Render.com**, command `yarn start`, binds `0.0.0.0:$PORT` - No in-repo Dockerfile / health route; `/update` is a stub -- Shared Render IPs can hit Discord 429 — login retries/backoff exist in `index.ts` +- Shared Render IPs can hit Discord 429 — login retries/backoff exist in `index.ts`. OpenSea/scheduler wait until Discord is ready; do not restart to "kick" a 429. - Secrets live in the host dashboard; keep `.env` out of git ## Codegen diff --git a/AGENTS.md b/AGENTS.md index 0a913fb7..57d3bf9d 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -34,7 +34,7 @@ These are **independent**. Mis-setting them is the most common ops footgun. | Env var | Controls | Prod value | Local Discord testing | |---------|----------|------------|------------------------| | `ARTBOT_IS_PROD` | Which JSON configs load (`channels.json` / `projectBots.json` vs `*_dev.json`) | `true` | `false` (a-t test server channels) | -| `PRODUCTION_MODE` | Discord login, OpenSea event handling, OpenSea REST poll bot | `true` | `true` if you want live Discord/OpenSea; CI uses `false` | +| `PRODUCTION_MODE` | Discord login, then OpenSea event handling / REST poll bot | `true` | `true` if you want live Discord/OpenSea; CI uses `false` | Local pattern that works: `ARTBOT_IS_PROD=false` + `PRODUCTION_MODE=true` + test-server `DISCORD_TOKEN` + join [a-t Discord](https://discord.gg/W6eYPpEk3a). @@ -46,12 +46,12 @@ src/index.ts │ POST /new-mint ← Hasura mint webhook (header: webhook_secret) │ GET /callback ← Twitter OAuth │ GET|POST /update ← stub OK responses (not a real health check) - ├── Discord client (if PRODUCTION_MODE) + ├── Discord client (if PRODUCTION_MODE) — must login before feeds start │ MessageCreate → # commands OR smartBotResponse ├── OpenSea WebSocket stream → OpenSeaListBot / OpenSeaSaleBot → activityTriager - ├── OpenSeaEventsPollBot (sales backfill, 30s) when PRODUCTION_MODE - ├── MintBot (queue + media-proxy poll → Discord) - ├── ScheduleBot (birthdays + optional trivia) + ├── OpenSeaEventsPollBot (sales backfill, 30s) after Discord ready + ├── MintBot (queue + media-proxy poll → Discord; posts only when client is ready) + ├── ScheduleBot (birthdays + optional trivia; starts after Discord ready) └── TwitterBot (optional, TWITTER_ENABLED) ``` @@ -137,6 +137,7 @@ Token/mint data can include chain IDs (Ethereum, Arbitrum, Base). **OpenSea stre 10. **CODEOWNERS**: `@ArtBlocks/Eng-Approvers-Product`. Do not assign personal Discord handles from the old README. 11. **OpenSea stream reconnect must `disconnect()` the previous Phoenix client** before constructing a new one. Do not reset `reconnectAttempts` until a stream event confirms the socket is live. Keep wildcard `*` subscriptions (OpenSea topics are collection slugs, not contracts; ArtBot filters by contract address). 12. **Do not add urql `cacheExchange`** — this process is long-lived; the document cache never expires. App-level caches (project dicts, wallet TTL) are the right layer. +13. **OpenSea stream, sales poll, and ScheduleBot start only after Discord login succeeds.** Express binds PORT immediately (Render). Mint webhooks may queue, but MintBot does not post until `client.isReady()`. Do not start feed handlers in parallel with `login()`. ## Common tasks diff --git a/README.md b/README.md index 20002c06..58ced9e0 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ yarn install | Variable | Local testing | Production | |----------|---------------|------------| | `ARTBOT_IS_PROD` | `false` → `channels_dev.json` | `true` → `channels.json` | -| `PRODUCTION_MODE` | `true` to connect Discord / process OpenSea | `true` | +| `PRODUCTION_MODE` | `true` to connect Discord, then process OpenSea | `true` | - Start: diff --git a/src/Classes/MintBot.ts b/src/Classes/MintBot.ts index 4a555117..3de5e26f 100644 --- a/src/Classes/MintBot.ts +++ b/src/Classes/MintBot.ts @@ -100,6 +100,9 @@ export class MintBot { // Go through all mints in the queue and make sure the media image exists. // Uses event data only (no token API call). Polls media URL until available, then posts. async checkAndPostMints() { + if (!this.bot.isReady()) { + return + } if (Object.keys(this.mintsToPost).length === 0) { return } @@ -222,6 +225,9 @@ export class MintBot { // tries to report any new mints to the discord! startRoutine() { this.intervalId = setInterval(async () => { + if (!this.bot.isReady()) { + return + } if (Object.keys(this.mintsToPost).length > 0) { logger.info( { count: Object.keys(this.mintsToPost).length }, diff --git a/src/index.ts b/src/index.ts index 49d66d9c..6f085459 100644 --- a/src/index.ts +++ b/src/index.ts @@ -285,6 +285,7 @@ async function waitForDiscordGateway(): Promise { latencyMs, retryAfterSec, waitMs, + waitHours: Math.round((waitMs / 3_600_000) * 10) / 10, }, 'Discord gateway rate-limited (429) — waiting before login attempt' ) @@ -337,68 +338,86 @@ async function runPreLoginDiagnostics(): Promise { } } -async function attemptDiscordLogin() { - logger.info({ attempt: loginRetryCount + 1, MAX_LOGIN_RETRIES }, 'Attempting Discord login') +function sleep(ms: number): Promise { + return new Promise((resolve) => setTimeout(resolve, ms)) +} - await runPreLoginDiagnostics() +/** Resolves only after Discord login succeeds. Exits the process after max retries. */ +async function attemptDiscordLogin(): Promise { + while (true) { + logger.info( + { attempt: loginRetryCount + 1, MAX_LOGIN_RETRIES }, + 'Attempting Discord login' + ) - // Capture Discord.js internal debug output during the login window so we can - // see exactly where the handshake stalls if it times out. - const debugLogs: string[] = [] - const debugListener = (msg: string) => { - debugLogs.push(msg) - logger.debug({ discordInternalDebug: msg }, 'Discord.js debug') - } - discordClient.on('debug', debugListener) + await runPreLoginDiagnostics() - let timeoutHandle: NodeJS.Timeout | undefined + // Capture Discord.js internal debug output during the login window so we can + // see exactly where the handshake stalls if it times out. + const debugLogs: string[] = [] + const debugListener = (msg: string) => { + debugLogs.push(msg) + logger.debug({ discordInternalDebug: msg }, 'Discord.js debug') + } + discordClient.on('debug', debugListener) - try { - const loginStartMs = Date.now() - const loginPromise = discordClient.login(DISCORD_TOKEN) - const timeoutPromise = new Promise((_, reject) => { - timeoutHandle = setTimeout(() => { - const elapsedMs = Date.now() - loginStartMs - logger.error( - { - elapsedMs, - wsStatus: discordClient.ws.status, - wsStatusName: Status[discordClient.ws.status] ?? 'unknown', - shardCount: discordClient.ws.shards.size, - recentDebugLogs: debugLogs.slice(-20), - }, - 'Discord login timed out — WebSocket state at timeout' - ) - reject(new Error('Discord login timed out')) - }, DISCORD_LOGIN_TIMEOUT) - }) + let timeoutHandle: NodeJS.Timeout | undefined - await Promise.race([loginPromise, timeoutPromise]) - clearTimeout(timeoutHandle) - logger.info({ elapsedMs: Date.now() - loginStartMs }, 'Discord login attempt successful') - loginRetryCount = 0 - } catch (error) { - clearTimeout(timeoutHandle) - logger.error({ err: error }, 'Discord login failed') - - if (loginRetryCount < MAX_LOGIN_RETRIES) { - loginRetryCount++ - // Exponential backoff: 5s, 10s, 20s, 40s, 80s - const backoffTime = Math.min( - 5000 * Math.pow(2, loginRetryCount - 1), - 80000 - ) + try { + const loginStartMs = Date.now() + const loginPromise = discordClient.login(DISCORD_TOKEN) + const timeoutPromise = new Promise((_, reject) => { + timeoutHandle = setTimeout(() => { + const elapsedMs = Date.now() - loginStartMs + logger.error( + { + elapsedMs, + wsStatus: discordClient.ws.status, + wsStatusName: Status[discordClient.ws.status] ?? 'unknown', + shardCount: discordClient.ws.shards.size, + recentDebugLogs: debugLogs.slice(-20), + }, + 'Discord login timed out — WebSocket state at timeout' + ) + reject(new Error('Discord login timed out')) + }, DISCORD_LOGIN_TIMEOUT) + }) + + await Promise.race([loginPromise, timeoutPromise]) + clearTimeout(timeoutHandle) logger.info( - { loginRetryCount, MAX_LOGIN_RETRIES, backoffSeconds: backoffTime / 1000 }, - 'Retrying Discord login attempt' + { elapsedMs: Date.now() - loginStartMs }, + 'Discord login attempt successful' ) - setTimeout(attemptDiscordLogin, backoffTime) - } else { - logger.error('Max login retries reached. Giving up.') - process.exit(1) + loginRetryCount = 0 + return + } catch (error) { + clearTimeout(timeoutHandle) + logger.error({ err: error }, 'Discord login failed') + + if (loginRetryCount < MAX_LOGIN_RETRIES) { + loginRetryCount++ + // Exponential backoff: 5s, 10s, 20s, 40s, 80s + const backoffTime = Math.min( + 5000 * Math.pow(2, loginRetryCount - 1), + 80000 + ) + logger.info( + { + loginRetryCount, + MAX_LOGIN_RETRIES, + backoffSeconds: backoffTime / 1000, + }, + 'Retrying Discord login attempt' + ) + await sleep(backoffTime) + } else { + logger.error('Max login retries reached. Giving up.') + process.exit(1) + } + } finally { + discordClient.off('debug', debugListener) } - } finally { - discordClient.off('debug', debugListener) } } @@ -433,9 +452,6 @@ discordClient.on('disconnect', () => { export const triviaBot = new TriviaBot(discordClient) -const scheduleBot = new ScheduleBot(discordClient.channels.cache, projectConfig) -botsToCleanup.push(scheduleBot) - discordClient.on(Events.MessageCreate, async (msg) => { const msgAuthor = msg.author.username const msgContent = msg.content @@ -838,17 +854,7 @@ if (typeof global !== 'undefined') { ).getOpenSeaConnectionStats = getOpenSeaConnectionStats } -// Initialize the stream client -logger.info('Starting OpenSea WebSocket Stream integration') -openseaStreamClient = initializeOpenSeaStreamClient() -setupStreamEventHandlers(openseaStreamClient) - -logger.info( - { MAX_RECONNECT_ATTEMPTS, INITIAL_RECONNECT_DELAY, MAX_RECONNECT_DELAY }, - 'OpenSea Stream Config' -) - -// OpenSea Stream Bots - Primary handlers, API polling for sales backfill +// OpenSea Stream Bots - constructed here; stream/poll start after Discord login. const openSeaListBot = new OpenSeaListBot(discordClient) const openSeaSaleBot = new OpenSeaSaleBot(discordClient, abTwitterBot) // Primary sales handler botsToCleanup.push(openSeaListBot) @@ -917,14 +923,50 @@ const isTrackedContract = ( ) } -if (PRODUCTION_MODE) { - attemptDiscordLogin() - // Initialize OpenSea API polling bots after Discord login +/** + * Express stays up so Render can bind PORT. Discord-dependent work + * (OpenSea stream, sales poll, scheduler) starts only after login succeeds. + */ +async function startProductionBots() { + logger.info('Waiting for Discord login before starting OpenSea and scheduled bots') + await attemptDiscordLogin() + + logger.info( + { + tag: discordClient.user?.tag, + guildCount: discordClient.guilds.cache.size, + channelCount: discordClient.channels.cache.size, + blockTalkCached: discordClient.channels.cache.has(CHANNEL_BLOCK_TALK), + }, + 'Discord ready — starting OpenSea and scheduled bots' + ) + + logger.info('Starting OpenSea WebSocket Stream integration') + openseaStreamClient = initializeOpenSeaStreamClient() + setupStreamEventHandlers(openseaStreamClient) + logger.info( + { MAX_RECONNECT_ATTEMPTS, INITIAL_RECONNECT_DELAY, MAX_RECONNECT_DELAY }, + 'OpenSea Stream Config' + ) + + const scheduleBot = new ScheduleBot( + discordClient.channels.cache, + projectConfig + ) + botsToCleanup.push(scheduleBot) + initOpenSeaApiPollingBots().catch((err) => { logger.error({ err }, 'Error initializing OpenSea API polling bots') }) } +if (PRODUCTION_MODE) { + startProductionBots().catch((err) => { + logger.error({ err }, 'Failed to start production bots') + process.exit(1) + }) +} + // Cleanup function for OpenSea WebSocket connections function cleanupOpenSeaConnections() { logger.info('Cleaning up OpenSea WebSocket connections')