fix(gateway): set a nonce if none is provided for gateway.requestMembers (#4057)

* fix(gateway): set a nonce if none is provided for gateway.requestMembers

* chore: update comment for gateway.requestMembers to clarify about gateway.cache.requestMembers.enabled

* chore: make the comment better

* fix: add node:crypto import to deno import map
This commit is contained in:
Awesome Stickz
2024-12-27 20:43:01 +05:30
committed by GitHub
parent dfd3ead620
commit 6abf453319
2 changed files with 27 additions and 15 deletions
+1
View File
@@ -1,6 +1,7 @@
{ {
"imports": { "imports": {
"node:buffer": "https://deno.land/std@0.176.0/node/buffer.ts", "node:buffer": "https://deno.land/std@0.176.0/node/buffer.ts",
"node:crypto": "https://deno.land/std@0.176.0/node/crypto.ts",
"node:fs": "https://deno.land/std@0.176.0/node/fs.ts", "node:fs": "https://deno.land/std@0.176.0/node/fs.ts",
"node:fs/promises": "https://deno.land/std@0.176.0/node/fs/promises.ts", "node:fs/promises": "https://deno.land/std@0.176.0/node/fs/promises.ts",
"node:events": "https://deno.land/std@0.176.0/node/events.ts", "node:events": "https://deno.land/std@0.176.0/node/events.ts",
+26 -15
View File
@@ -1,3 +1,4 @@
import { randomBytes } from 'node:crypto'
import { import {
type AtLeastOne, type AtLeastOne,
type BigString, type BigString,
@@ -506,22 +507,32 @@ export function createGatewayManager(options: CreateGatewayManagerOptions): Gate
options.limit = options.userIds.length options.limit = options.userIds.length
} }
const members = if (!options?.nonce) {
!gateway.cache.requestMembers.enabled || !options?.nonce let nonce = ''
? []
: new Promise<Camelize<DiscordMemberWithUser[]>>((resolve, reject) => {
// Should never happen.
if (!gateway.cache.requestMembers.enabled || !options?.nonce) {
reject(new Error("Can't request the members without the nonce or with the feature disabled."))
return
}
gateway.cache.requestMembers.pending.set(options.nonce, { while (!nonce || gateway.cache.requestMembers.pending.has(nonce)) {
nonce: options.nonce, nonce = randomBytes(16).toString('hex')
resolve, }
members: [],
}) options ??= { limit: 0 }
options.nonce = nonce
}
const members = !gateway.cache.requestMembers.enabled
? []
: new Promise<Camelize<DiscordMemberWithUser[]>>((resolve, reject) => {
// Should never happen.
if (!gateway.cache.requestMembers.enabled || !options?.nonce) {
reject(new Error("Can't request the members without the nonce or with the feature disabled."))
return
}
gateway.cache.requestMembers.pending.set(options.nonce, {
nonce: options.nonce,
resolve,
members: [],
}) })
})
await gateway.sendPayload(shardId, { await gateway.sendPayload(shardId, {
op: GatewayOpcodes.RequestGuildMembers, op: GatewayOpcodes.RequestGuildMembers,
@@ -807,7 +818,7 @@ export interface GatewayManager extends Required<CreateGatewayManagerOptions> {
*/ */
editShardStatus: (shardId: number, data: StatusUpdate) => Promise<void> editShardStatus: (shardId: number, data: StatusUpdate) => Promise<void>
/** /**
* Fetches the list of members for a guild over the gateway. * Fetches the list of members for a guild over the gateway. If `gateway.cache.requestMembers.enabled` is not set, this function will return an empty array and you'll have to handle the `GUILD_MEMBERS_CHUNK` events yourself.
* *
* @param guildId - The ID of the guild to get the list of members for. * @param guildId - The ID of the guild to get the list of members for.
* @param options - The parameters for the fetching of the members. * @param options - The parameters for the fetching of the members.