From cd7973273e68ca5cd93ae7094766d91636a1607f Mon Sep 17 00:00:00 2001 From: Ayyan Date: Sat, 9 Jan 2021 21:39:25 +0400 Subject: [PATCH] fix(misc): check whether guilds' loaded every 2s (#353) * fix(misc): check whether guilds' loaded every 2s * unavailable_guilds -> guilds * guilds -> unavailable_guilds * Separate unavailable guild type * Clean up and apply suggested changes --- src/api/controllers/misc.ts | 29 +++++++++++++++++++---------- src/api/handlers/webhook.ts | 2 +- src/types/discord.ts | 22 ++++++++++++++++++---- 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/src/api/controllers/misc.ts b/src/api/controllers/misc.ts index fb455657a..7275ad007 100644 --- a/src/api/controllers/misc.ts +++ b/src/api/controllers/misc.ts @@ -28,17 +28,26 @@ export async function handleInternalReady( // Triggered on each shard eventHandlers.shardReady?.(shardID); if (payload.shard && shardID === payload.shard[1] - 1) { - // Wait for 5 seconds to allow all guild create events to be processed - await delay(5000); - cache.isReady = true; - eventHandlers.ready?.(); + const loadedAllGuilds = async () => { + if (payload.guilds.some((g) => !cache.guilds.has(g.id))) { + setTimeout(() => loadedAllGuilds, 2000); + } else { + // The bot has already started, the last shard is resumed, however. + if (cache.isReady) return; - // All the members that came in on guild creates should now be processed 1 by 1 - for (const [guildID, members] of initialMemberLoadQueue.entries()) { - await Promise.all( - members.map((member) => structures.createMember(member, guildID)), - ); - } + cache.isReady = true; + eventHandlers.ready?.(); + + // All the members that came in on guild creates should now be processed 1 by 1 + for (const [guildID, members] of initialMemberLoadQueue.entries()) { + await Promise.all( + members.map((member) => structures.createMember(member, guildID)), + ); + } + } + }; + + setTimeout(() => loadedAllGuilds(), 2000); } // Wait 5 seconds to spawn next shard diff --git a/src/api/handlers/webhook.ts b/src/api/handlers/webhook.ts index 5713c8ded..4ae1b61a0 100644 --- a/src/api/handlers/webhook.ts +++ b/src/api/handlers/webhook.ts @@ -46,7 +46,7 @@ export async function createWebhook( throw new Error(Errors.INVALID_WEBHOOK_NAME); } - return await RequestManager.post( + return RequestManager.post( endpoints.CHANNEL_WEBHOOKS(channelID), { ...options, diff --git a/src/types/discord.ts b/src/types/discord.ts index f1da39f39..904dcb3fb 100644 --- a/src/types/discord.ts +++ b/src/types/discord.ts @@ -1,6 +1,6 @@ -import { PartialUser, UserPayload } from "./guild.ts"; +import { CreateGuildPayload, PartialUser, UserPayload } from "./guild.ts"; import { MemberCreatePayload } from "./member.ts"; -import { Activity } from "./message.ts"; +import { Activity, Application } from "./message.ts"; import { ClientStatusPayload } from "./presence.ts"; export interface DiscordPayload { @@ -281,9 +281,23 @@ export interface VoiceStateUpdatePayload { } export interface ReadyPayload { + /** gateway version */ + v: number; + /** information about the user including email */ + user: UserPayload; + /** empty array */ + private_channels: []; + /** the guilds the user is in */ + guilds: UnavailableGuildPayload[]; /** used for resuming connections */ session_id: string; - /** (shard_id, num_shards) the shard information associated with this session, if sent when identifying */ + /** (shard_id, num_shards) the shard information associated with this session, if sent when identifying */ shard?: [number, number]; - user: UserPayload; + /** contains id and flags */ + application: Pick; } + +export type UnavailableGuildPayload = Pick< + CreateGuildPayload, + "id" | "unavailable" +>;