Skip to content

Chunk servers when needed instead of all at startup - #210

Merged
Maselkov merged 1 commit into
masterfrom
chunk-on-demand
Oct 25, 2026
Merged

Maselkov merged 1 commit into
masterfrom
chunk-on-demand

Conversation

@Maselkov

Copy link
Copy Markdown
Owner

Summary

With the members intent on, discord.py requested the member list of every server at startup. Each shard can send only about 120 gateway messages a minute, so a restart meant many minutes of WebSocket in shard ID N is ratelimited warnings. Ready, and with it every background loop, waited until all of it finished.

  • Startup: chunk_guilds_at_startup=False. The bot is ready as soon as it has connected.

  • Code that reads a server's members chunks that server first. Guild sync, key role sync and EVTC autoposting already did. Two places didn't, and now do:

    • forced account names, which saw only cached members;
    • /guild members, which would lose its mentions of people on the server.

    Only servers using these features get chunked, as they're reached, rather than all of them up front.

  • User lookups: without the startup chunk, most users aren't cached. Three places used bot.get_user with no fallback, and quietly skipped anyone not cached:

    • event reminders;
    • EVTC autoposts;
    • the reply to a guild sync authorization.

    They now use the new bot.get_or_fetch_user, which tries the cache, then fetches the user from Discord once and remembers them. That matters for reminders, whose loop runs every 10 seconds. The gem tracker and the owner DM already fell back to fetching.

  • EVTC: an autopost for a user who deleted their Discord account is now skipped without logging an error.

Test plan

  • New tests:
    • the bot doesn't chunk at startup;
    • get_or_fetch_user uses the cache, fetches once, and returns None for unknown users;
    • guild sync, key role sync and forced names each chunk an unchunked server first (forced names failed before the fix);
    • /guild members chunks the server before mentioning members (failed before the fix).
  • Full suite with MongoDB: 759 passed, command tree unchanged. ruff check clean.
  • After deploying: Ready, serving N guilds within a minute or so of connecting; then check that event reminders, guild sync and forced names still run.

🤖 Generated with Claude Code

With the members intent, discord.py chunked every server at startup, which
takes many minutes of gateway rate limits on every restart and holds back
ready (and every background loop) until it's done. Guild sync, key role
sync and EVTC autoposting already chunk a server before reading its
members; forced account names and /guild members' mentions now do too.

Without the startup chunk most users aren't cached, so event reminders,
EVTC autoposts and guild sync authorization replies look users up with
bot.get_or_fetch_user: the cache, or else Discord, fetched once and
remembered. Before, they skipped anyone not cached.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Maselkov
Maselkov merged commit 5f56ad7 into master Oct 25, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant