Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .cursor/rules/discord-bot/RULE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
4 changes: 2 additions & 2 deletions .cursor/rules/ops-gotchas/RULE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
11 changes: 6 additions & 5 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand All @@ -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)
```

Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
6 changes: 6 additions & 0 deletions src/Classes/MintBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 },
Expand Down
184 changes: 113 additions & 71 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ async function waitForDiscordGateway(): Promise<boolean> {
latencyMs,
retryAfterSec,
waitMs,
waitHours: Math.round((waitMs / 3_600_000) * 10) / 10,
},
'Discord gateway rate-limited (429) — waiting before login attempt'
)
Expand Down Expand Up @@ -337,68 +338,86 @@ async function runPreLoginDiagnostics(): Promise<void> {
}
}

async function attemptDiscordLogin() {
logger.info({ attempt: loginRetryCount + 1, MAX_LOGIN_RETRIES }, 'Attempting Discord login')
function sleep(ms: number): Promise<void> {
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<void> {
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)
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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')
Expand Down
Loading