fix: nonce bug not resolving requests

This commit is contained in:
Skillz
2020-12-27 09:47:00 -05:00
parent 291d4a2f49
commit 65d3789742
4 changed files with 28 additions and 9 deletions
+15 -6
View File
@@ -7,6 +7,7 @@ import {
GuildMemberUpdatePayload,
} from "../../types/mod.ts";
import { cache } from "../../util/cache.ts";
import { Collection } from "../../util/collection.ts";
import { structures } from "../structures/structures.ts";
import { cacheHandlers } from "./cache.ts";
@@ -94,11 +95,11 @@ export async function handleInternalGuildMembersChunk(data: DiscordPayload) {
if (data.t !== "GUILD_MEMBERS_CHUNK") return;
const payload = data.d as GuildMemberChunkPayload;
const guild = await cacheHandlers.get("guilds", payload.guild_id);
if (!guild) return;
await Promise.all(
payload.members.map((member) => structures.createMember(member, guild.id)),
const members = await Promise.all(
payload.members.map((member) =>
structures.createMember(member, payload.guild_id)
),
);
// Check if its necessary to resolve the fetchmembers promise for this chunk or if more chunks will be coming
@@ -110,8 +111,16 @@ export async function handleInternalGuildMembersChunk(data: DiscordPayload) {
if (payload.chunk_index + 1 === payload.chunk_count) {
cache.fetchAllMembersProcessingRequests.delete(payload.nonce);
resolve(
await cacheHandlers.filter("members", (m) => m.guilds.has(guild.id)),
// Only 1 chunk most likely is all members or users only request a small amount of users
if (payload.chunk_count === 1) {
return resolve(new Collection(members.map((m) => [m.id, m])));
}
return resolve(
await cacheHandlers.filter(
"members",
(m) => m.guilds.has(payload.guild_id),
),
);
}
}
+6
View File
@@ -17,6 +17,7 @@ import {
} from "../structures/structures.ts";
import { cacheHandlers } from "./cache.ts";
/** This function is the internal handler for the ready event. Users can override this with controllers if desired. */
export async function handleInternalReady(
data: DiscordPayload,
shardID: number,
@@ -47,6 +48,7 @@ export async function handleInternalReady(
allowNextShard();
}
/** This function is the internal handler for the presence update event. Users can override this with controllers if desired. */
export async function handleInternalPresenceUpdate(data: DiscordPayload) {
if (data.t !== "PRESENCE_UPDATE") return;
@@ -57,11 +59,13 @@ export async function handleInternalPresenceUpdate(data: DiscordPayload) {
return eventHandlers.presenceUpdate?.(payload, oldPresence);
}
/** This function is the internal handler for the typings event. Users can override this with controllers if desired. */
export function handleInternalTypingStart(data: DiscordPayload) {
if (data.t !== "TYPING_START") return;
eventHandlers.typingStart?.(data.d as TypingStartPayload);
}
/** This function is the internal handler for the user update event. Users can override this with controllers if desired. */
export async function handleInternalUserUpdate(data: DiscordPayload) {
if (data.t !== "USER_UPDATE") return;
@@ -77,6 +81,7 @@ export async function handleInternalUserUpdate(data: DiscordPayload) {
return eventHandlers.botUpdate?.(userData);
}
/** This function is the internal handler for the voice state update event. Users can override this with controllers if desired. */
export async function handleInternalVoiceStateUpdate(data: DiscordPayload) {
if (data.t !== "VOICE_STATE_UPDATE") return;
@@ -127,6 +132,7 @@ export async function handleInternalVoiceStateUpdate(data: DiscordPayload) {
eventHandlers.voiceStateUpdate?.(member, payload);
}
/** This function is the internal handler for the webhooks update event. Users can override this with controllers if desired. */
export function handleInternalWebhooksUpdate(data: DiscordPayload) {
if (data.t !== "WEBHOOKS_UPDATE") return;
+6 -1
View File
@@ -62,10 +62,15 @@ export async function startBot(config: BotConfig) {
spawnShards(botGatewayData, identifyPayload, 0, botGatewayData.shards);
}
/** Allows you to dynamically update the event handlers by passing in new eventHandlers */
export function updateEventHandlers(newEventHandlers: EventHandlers) {
eventHandlers = newEventHandlers;
eventHandlers = {
...eventHandlers,
...newEventHandlers
}
}
/** INTERNAL LIB function used to set the bot ID once the READY event is sent by Discord. */
export function setBotID(id: string) {
if (botID !== id) botID = id;
}
+1 -2
View File
@@ -93,9 +93,8 @@ export async function requestAllMembers(
resolve: Function,
options?: FetchMembersOptions,
) {
const nonce = `${guild.id}-${Math.random().toString()}`;
const nonce = `${guild.id}-${Date.now()}`;
cache.fetchAllMembersProcessingRequests.set(nonce, resolve);
return requestGuildMembers(guild.id, guild.shardID, nonce, options);
}