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
This commit is contained in:
Ayyan
2021-01-09 21:39:25 +04:00
committed by GitHub
parent e5a8f3b358
commit cd7973273e
3 changed files with 38 additions and 15 deletions

View File

@@ -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

View File

@@ -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,

View File

@@ -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<Application, "id">;
}
export type UnavailableGuildPayload = Pick<
CreateGuildPayload,
"id" | "unavailable"
>;