diff --git a/src/handlers/guilds/GUILD_CREATE.ts b/src/handlers/guilds/GUILD_CREATE.ts index 968388b9d..f09e37dc1 100644 --- a/src/handlers/guilds/GUILD_CREATE.ts +++ b/src/handlers/guilds/GUILD_CREATE.ts @@ -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); diff --git a/src/handlers/guilds/GUILD_DELETE.ts b/src/handlers/guilds/GUILD_DELETE.ts index 2ffd989e8..ab92580b5 100644 --- a/src/handlers/guilds/GUILD_DELETE.ts +++ b/src/handlers/guilds/GUILD_DELETE.ts @@ -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()); diff --git a/src/handlers/misc/READY.ts b/src/handlers/misc/READY.ts index 095b8a992..bdac99abb 100644 --- a/src/handlers/misc/READY.ts +++ b/src/handlers/misc/READY.ts @@ -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; diff --git a/src/util/utils.ts b/src/util/utils.ts index ec4ece452..f06d38b7e 100644 --- a/src/util/utils.ts +++ b/src/util/utils.ts @@ -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, ) { - basicShards.forEach((shard) => { + ws.shards.forEach((shard) => { sendWS({ op: DiscordGatewayOpcodes.StatusUpdate, d: {