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

View File

@@ -1,6 +1,7 @@
{
"imports": {
"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/promises": "https://deno.land/std@0.176.0/node/fs/promises.ts",
"node:events": "https://deno.land/std@0.176.0/node/events.ts",

View File

@@ -1,3 +1,4 @@
import { randomBytes } from 'node:crypto'
import {
type AtLeastOne,
type BigString,
@@ -506,22 +507,32 @@ export function createGatewayManager(options: CreateGatewayManagerOptions): Gate
options.limit = options.userIds.length
}
const members =
!gateway.cache.requestMembers.enabled || !options?.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
}
if (!options?.nonce) {
let nonce = ''
gateway.cache.requestMembers.pending.set(options.nonce, {
nonce: options.nonce,
resolve,
members: [],
})
while (!nonce || gateway.cache.requestMembers.pending.has(nonce)) {
nonce = randomBytes(16).toString('hex')
}
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, {
op: GatewayOpcodes.RequestGuildMembers,
@@ -807,7 +818,7 @@ export interface GatewayManager extends Required<CreateGatewayManagerOptions> {
*/
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 options - The parameters for the fetching of the members.