From ee4be53a456d29da25148149ece10471464377b8 Mon Sep 17 00:00:00 2001 From: ITOH <72305210+itohatweb@users.noreply.github.com> Date: Thu, 8 Apr 2021 18:51:50 +0200 Subject: [PATCH] f --- src/bot.ts | 4 +--- src/handlers/misc/READY.ts | 13 ++++--------- src/ws/events.ts | 3 ++- src/ws/handle_discord_payload.ts | 3 ++- src/ws/identify.ts | 1 + src/ws/resume.ts | 1 + src/ws/ws.ts | 2 ++ 7 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/bot.ts b/src/bot.ts index 19956503f..264791627 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -13,7 +13,6 @@ export let eventHandlers: EventHandlers = {}; export let botGatewayData: DiscordGetGatewayBot; export let proxyWSURL = `wss://gateway.discord.gg`; -export let lastShardId = 0; export const identifyPayload = { token: "", @@ -58,8 +57,7 @@ export async function startBot(config: BotConfig) { : next), 0, ); - lastShardId = botGatewayData.shards; - identifyPayload.shard = [0, lastShardId]; + identifyPayload.shard = [0, botGatewayData.shards]; ws.spawnShards(); } diff --git a/src/handlers/misc/READY.ts b/src/handlers/misc/READY.ts index bdac99abb..7098ad079 100644 --- a/src/handlers/misc/READY.ts +++ b/src/handlers/misc/READY.ts @@ -1,15 +1,10 @@ -import { - eventHandlers, - lastShardId, - setApplicationId, - setBotId, -} from "../../bot.ts"; +import { eventHandlers, setApplicationId, setBotId } from "../../bot.ts"; 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 { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; import { DiscordReady } from "../../types/gateway/ready.ts"; +import { delay, snakeKeysToCamelCase } from "../../util/utils.ts"; import { ws } from "../../ws/ws.ts"; export async function handleReady( @@ -73,7 +68,7 @@ async function loaded(shardId: number) { shard.ready = true; // If it is the last shard we can go full ready - if (shardId === lastShardId - 1) { + if (shardId === ws.lastShardId - 1) { // Still some shards are loading so wait another 2 seconds for them if (ws.shards.some((shard) => !shard.ready)) { setTimeout(() => loaded(shardId), 2000); @@ -86,7 +81,7 @@ async function loaded(shardId: number) { await Promise.allSettled( members.map(async (member) => { const memberStruct = await structures.createMemberStruct( - member, + snakeKeysToCamelCase(member), guildId, ); diff --git a/src/ws/events.ts b/src/ws/events.ts index 727a6276d..b4c7fdcfb 100644 --- a/src/ws/events.ts +++ b/src/ws/events.ts @@ -1,3 +1,4 @@ +import { DiscordGatewayPayload } from "../types/gateway/gateway_payload.ts"; import { DiscordenoShard } from "./ws.ts"; /** The handler for logging different actions happening inside the ws. User can override and put custom handling per event. */ @@ -35,7 +36,7 @@ export function log( ): unknown; export function log( type: "INVALID_SESSION", - data: { shardId: number; payload: DiscordPayload }, + data: { shardId: number; payload: DiscordGatewayPayload }, ): unknown; export function log(type: "RAW", data: Record): unknown; export function log(type: "RECONNECT", data: { shardId: number }): unknown; diff --git a/src/ws/handle_discord_payload.ts b/src/ws/handle_discord_payload.ts index 83c4b2088..a1ba8ddb8 100644 --- a/src/ws/handle_discord_payload.ts +++ b/src/ws/handle_discord_payload.ts @@ -1,8 +1,9 @@ +import { DiscordGatewayPayload } from "../types/gateway/gateway_payload.ts"; import { ws } from "./ws.ts"; /** Handler for processing all dispatch payloads that should be sent/forwarded to another server/vps/process. */ export async function handleDiscordPayload( - data: DiscordPayload, + data: DiscordGatewayPayload, shardId: number, ) { await fetch(ws.url, { diff --git a/src/ws/identify.ts b/src/ws/identify.ts index b3e32f599..368c60c44 100644 --- a/src/ws/identify.ts +++ b/src/ws/identify.ts @@ -15,6 +15,7 @@ export async function identify(shardId: number, maxShards: number) { sessionId: "", previousSequenceNumber: 0, resuming: false, + ready: false, unavailableGuildIds: new Set(), heartbeat: { lastSentAt: 0, diff --git a/src/ws/resume.ts b/src/ws/resume.ts index 46211b595..18f2819fa 100644 --- a/src/ws/resume.ts +++ b/src/ws/resume.ts @@ -28,6 +28,7 @@ export async function resume(shardId: number) { sessionId, previousSequenceNumber, resuming: false, + ready: false, unavailableGuildIds: new Set(), heartbeat: { lastSentAt: 0, diff --git a/src/ws/ws.ts b/src/ws/ws.ts index 5e4b62911..167c90f4c 100644 --- a/src/ws/ws.ts +++ b/src/ws/ws.ts @@ -116,6 +116,8 @@ export interface DiscordenoShard { previousSequenceNumber: number | null; /** Whether the shard is currently resuming. */ resuming: boolean; + /** Whether the shard has received the ready event */ + ready: boolean; /** The list of guild ids that are currently unavailable due to an outage. */ unavailableGuildIds: Set; heartbeat: {