Repository navigation
feat(plugin-gateway): request guild members over the gateway - #126
Conversation
Add `client.members.request(guildId, options)` (and `guild.requestMembers`), sending REQUEST_GUILD_MEMBERS on the guild's shard and resolving with the members once the last GUILD_MEMBERS_CHUNK of its nonce is cached. It rejects with `GuildMembersTimeoutError` when the chunks stop arriving, or `GuildMembersRateLimitError` on a matching RATE_LIMITED. Every chunk is also emitted as the new `guildMembersChunk` event. Refs #121 Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01RBWEHLtNzWNykJMPqeqqbB
◈ PR LensNote This drawing shows
Architecture 4 components touched across 5 lanes. Play the interactive walkthrough Inside the changed components — 1 viewComponent view — Member request pipeline Internal modules managing request tracking, chunk dispatching, cache hydration, and timeout handling. Data flow
Follow each request, response and payload The other flows — 1 sequence
View
Tip Switch GitHub to dark mode and the diagrams follow. The moving dots are this pull request's data in motion 🪧 More tips
Thanks for using PR Lens! It's built by Coldtea, free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. |
There was a problem hiding this comment.
ℹ️ No critical issues — one minor nitpick below, otherwise looks solid.
Reviewed changes
Reviewed the full diff adding gateway-based member requests to @wolfstar/plugin-gateway, including the manager logic, chunk/rate-limit correlation, the new public event, error types, docs, and tests.
GuildMemberManager#request: validates options (query/userIdsmutually exclusive, ≤100userIds, nonce ≤32 bytes), computes the guild's shard ((guildId >> 22) % shardCount), sendsREQUEST_GUILD_MEMBERSviaclient.gateway.send, and tracks a pending request by nonce until it settles.handleChunk: correlatesGUILD_MEMBERS_CHUNKdispatches to the pending request by nonce (+guild_idcross-check), and completes it once the received-chunk count reacheschunk_count.handleRateLimited: wired intoGatewayClient.handleDispatchahead of the normal action pipeline, rejecting the matching request immediately on aRATE_LIMITEDdispatch instead of waiting out the timeout.guildMembersChunkevent: emitted fromutil/dispatch.tsonce the chunk is cached, carrying the hydratedmembers, the resolvedguild(ornull), and the raw dispatch data.Guild#requestMembers: thin wrapper delegating toclient.members.request, following thefetchOwnerpattern.- New errors:
GuildMembersTimeoutErrorandGuildMembersRateLimitError, matching the existingDispatchTimeoutErrorshape. - Tests: 13 new tests covering shard selection, payload shape, option validation, multi-chunk resolution ordering against cache writes, concurrent-request nonce correlation, timeout and timer-restart behavior,
RATE_LIMITED,send()failure cleanup, and the event with/without a cache. - Docs: README events table and members section, plus a
minorchangeset.
I verified the Discord-facing claims against [email protected] directly rather than trusting recollection: GatewayRateLimitedDispatchData's meta.nonce/opcode/retry_after (seconds) all match the usage in handleRateLimited, and the GUILD_MEMBERS_CHUNK cache write this relies on already existed in @wolfstar/plugin-cache's hydrateGuildMembersChunk before this PR. I also checked the completion logic (counting received chunks against chunk_count rather than tracking chunk_index) against discord.js's own GuildMemberManager#_fetchMany, which uses the same "index === chunk.count" approach — this is parity, not a shortcut. What initially looked like a TOCTOU race in the nonce-uniqueness check (request() awaits getShardCount() before reserving the nonce) isn't one: JS's run-to-completion semantics between awaits make the check-then-reserve sequence atomic against other concurrent request() calls.
ℹ️ Nitpicks
GuildMembersRequestOptions.userIdsis checked with a plain truthy test (userIds ? ... : ...). An explicituserIds: []is truthy, so it goes down the "request by IDs" branch (user_ids: []) instead of falling back to the default "every member" query — probably not what a caller passing a dynamically-built empty array would expect. Low impact, butuserIds && userIds.length > 0would avoid the surprise.
Claude Sonnet | 𝕏
@wolfstar/plugin-api
@wolfstar/plugin-cache
@wolfstar/plugin-gateway
@wolfstar/plugin-i18next
@wolfstar/plugin-logger
@wolfstar/plugin-sharder
@wolfstar/plugin-subcommands-advanced
commit: |
…equests `userIds: []` is truthy, so it sent a request by an empty list of IDs instead of falling back to requesting every member. Refs #121 Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01RBWEHLtNzWNykJMPqeqqbB
|
Addressed the Generated by Claude Code |
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
Reviewed the single new commit (e26317c) since the prior pullfrog review, which fixes the nitpick raised on 4a5db69.
- Fixed
userIds: []handling:GuildMemberManager#requestnow doesoptions.userIds?.length ? options.userIds : undefinedinstead of a plain truthy check, so an emptyuserIdsarray falls back to the default "every member" query branch instead of incorrectly taking the "request by IDs" branch with an emptyuser_ids. - Updated JSDoc:
GuildMembersRequestOptions.userIdsnow documents that an empty array is treated like omitting it. - Added a regression test: "GIVEN an empty userIds THEN every member is requested" asserts the payload sent is the default query (
query: "", limit: 0), notuser_ids: [].
Confirmed the reassigned userIds local is still what feeds the ≤100/mutual-exclusivity validation and the payload construction below it, so the fix doesn't disturb either.
Claude Sonnet | 𝕏
# Conflicts: # packages/plugin-gateway/src/managers/GuildMemberManager.ts

🔗 Linked issue
Part of #121 (item 2: request guild members over the gateway).
🧭 Context
@wolfstar/plugin-cachealready writesGUILD_MEMBERS_CHUNKinto the cache, but nothing could ask Discord for those chunks.client.membersonly listed members through REST, one page at a time, and no event exposed the chunks. Bots that keep a full member cache need the gateway request, and the old WolfStar gateway process supported it.📚 Description
This adds
client.members.request(guildId, options), the equivalent of discord.js'sguild.members.fetch()with no user argument. It sendsREQUEST_GUILD_MEMBERS(opcode 8) on the guild's shard. It resolves with theGuildMembers once everyGUILD_MEMBERS_CHUNKwith its nonce has been cached, so aclient.members.getright after sees them. Each chunk is also emitted as a newguildMembersChunkevent.{ query?, limit?, userIds?, presences?, nonce?, time? }.queryanduserIdscannot be combined. With neither, every member is requested (query: "",limit: 0).userIds.(guildId >> 22) % shardCount, with the count fromclient.gateway.getShardCount().@discordjs/ws. ItsWebSocketShard#sendwaits until the shard is ready. After 115 payloads in the current minute on that shard, it queues the rest until the window resets (Discord's limit is 120).GuildMembersTimeoutErrorwhen no chunk arrives withintimems. The default is 120 s, and each chunk restarts the timer, as in discord.js. The timer starts only aftersendresolves, so waiting in the local rate-limit queue doesn't count.RATE_LIMITEDanswer: Discord replies to an over-limit opcode 8 with aRATE_LIMITEDdispatch and sends no chunks. The request then rejects immediately withGuildMembersRateLimitError, whoseretryAfteris in ms, instead of hanging until its timeout.sendthrows (for example, a shard this process doesn't run).data.not_foundon the event.Guild#requestMembers(options)is a thin wrapper that followsGuild#fetchOwner.Guildhas no members manager, so there's noguild.members.fetchAll().Deliberately left out:
GuildMembers/GuildPresencesrequirements are documented.destroy(); they time out.cacheFailure: "skip", a chunk whose cache write fails is dropped like any other dispatch, so its request ends with a timeout.Key changes
managers/GuildMemberManager.tsrequest,GuildMembersRequestOptions, internalhandleChunk/handleRateLimitedutil/dispatch.ts/util/events.tsGUILD_MEMBERS_CHUNKhandler →guildMembersChunk(members,guild | null,data); it hands the chunk to the pending request after it is cachedGatewayClient.tsRATE_LIMITEDto the member manager (2 lines)util/errors.tsGuildMembersTimeoutError,GuildMembersRateLimitErrorstructures/Guild.tsGuild#requestMemberstests/member-requests.test.tsRATE_LIMITED, send failure, the event with and without a cache,Guild#requestMembersminorType of Change
Pre-flight Checklist
main)pnpm changesetAlso run:
pnpm lint,pnpm typecheck, andpnpm --filter @wolfstar/plugin-gateway build(attw and publint clean).🤖 Generated with Claude Code
https://claude.ai/code/session_01RBWEHLtNzWNykJMPqeqqbB
Generated by Claude Code