fix: remove basicShards and allowNextShard (#755)

This commit is contained in:
Skillz4Killz
2021-04-05 01:37:25 -04:00
committed by GitHub
parent 2e397325f5
commit 47ed6f5652
4 changed files with 13 additions and 10 deletions

View File

@@ -3,6 +3,7 @@ import { cache, cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordGuild } from "../../types/guilds/guild.ts";
import { ws } from "../../ws/ws.ts";
export async function handleGuildCreate(
data: DiscordGatewayPayload,
@@ -13,12 +14,12 @@ export async function handleGuildCreate(
if (await cacheHandlers.has("guilds", payload.id)) return;
const guildStruct = await structures.createGuildStruct(
data.d,
payload,
shardId,
);
await cacheHandlers.set("guilds", guildStruct.id, guildStruct);
const shard = basicShards.get(shardId);
const shard = ws.shards.get(shardId);
if (shard?.unavailableGuildIds.has(payload.id)) {
await cacheHandlers.delete("unavailableGuilds", payload.id);

View File

@@ -2,6 +2,7 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordUnavailableGuild } from "../../types/guilds/unavailable_guild.ts";
import { ws } from "../../ws/ws.ts";
export async function handleGuildDelete(
data: DiscordGatewayPayload,
@@ -15,7 +16,7 @@ export async function handleGuildDelete(
await cacheHandlers.delete("guilds", payload.id);
if (payload.unavailable) {
const shard = basicShards.get(shardId);
const shard = ws.shards.get(shardId);
if (shard) shard.unavailableGuildIds.add(payload.id);
await cacheHandlers.set("unavailableGuilds", payload.id, Date.now());

View File

@@ -8,9 +8,9 @@ import { cache, cacheHandlers } from "../../cache.ts";
import { initialMemberLoadQueue } from "../../structures/guild.ts";
import { structures } from "../../structures/mod.ts";
import { delay } from "../../util/utils.ts";
import { allowNextShard, basicShards } from "../../ws/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordReady } from "../../types/gateway/ready.ts";
import { ws } from "../../ws/ws.ts";
export async function handleReady(
data: DiscordGatewayPayload,
@@ -28,7 +28,7 @@ export async function handleReady(
// Save when the READY event was received to prevent infinite load loops
const now = Date.now();
const shard = basicShards.get(shardId);
const shard = ws.shards.get(shardId);
if (!shard) return;
// Set ready to false just to go sure
@@ -41,13 +41,13 @@ export async function handleReady(
// Wait 5 seconds to spawn next shard
await delay(5000);
allowNextShard();
ws.createNextShard = true;
}
// Don't pass the shard itself because unavailableGuilds won't be updated by the GUILD_CREATE event
/** This function checks if the shard is fully loaded */
function checkReady(payload: DiscordReady, shardId: number, now: number) {
const shard = basicShards.get(shardId);
const shard = ws.shards.get(shardId);
if (!shard) return;
// Check if all guilds were loaded
@@ -67,7 +67,7 @@ function checkReady(payload: DiscordReady, shardId: number, now: number) {
}
async function loaded(shardId: number) {
const shard = basicShards.get(shardId);
const shard = ws.shards.get(shardId);
if (!shard) return;
shard.ready = true;
@@ -75,7 +75,7 @@ async function loaded(shardId: number) {
// If it is the last shard we can go full ready
if (shardId === lastShardId - 1) {
// Still some shards are loading so wait another 2 seconds for them
if (basicShards.some((shard) => !shard.ready)) {
if (ws.shards.some((shard) => !shard.ready)) {
setTimeout(() => loaded(shardId), 2000);
} else {
cache.isReady = true;

View File

@@ -3,6 +3,7 @@ import { DiscordGatewayOpcodes } from "../types/codes/gateway_opcodes.ts";
import { Errors } from "../types/misc/errors.ts";
import { DiscordImageFormat } from "../types/misc/image_format.ts";
import { DiscordImageSize } from "../types/misc/image_size.ts";
import { ws } from "../ws/ws.ts";
import { SLASH_COMMANDS_NAME_REGEX } from "./constants.ts";
export const sleep = (timeout: number) => {
@@ -12,7 +13,7 @@ export const sleep = (timeout: number) => {
export function editBotStatus(
data: Pick<GatewayStatusUpdatePayload, "activities" | "status">,
) {
basicShards.forEach((shard) => {
ws.shards.forEach((shard) => {
sendWS({
op: DiscordGatewayOpcodes.StatusUpdate,
d: {