diff --git a/src/handlers/channel.ts b/src/handlers/channel.ts index 1e8b56430..534d5703e 100644 --- a/src/handlers/channel.ts +++ b/src/handlers/channel.ts @@ -1,5 +1,5 @@ import type { MessageCreateOptions } from "../types/message.ts"; -import { +import type { GetMessagesAfter, GetMessagesBefore, GetMessagesAround, diff --git a/src/module/basicShard.ts b/src/module/basicShard.ts index 8935ab09f..2f10296b4 100644 --- a/src/module/basicShard.ts +++ b/src/module/basicShard.ts @@ -3,6 +3,8 @@ import type { IdentifyPayload } from "./client.ts"; import { connectWebSocket, isWebSocketCloseEvent, + isWebSocketPingEvent, + isWebSocketPongEvent, WebSocket, } from "https://deno.land/std@0.67.0/ws/mod.ts"; import type { DiscordHeartbeatPayload } from "../types/discord.ts"; @@ -11,12 +13,14 @@ import type { BotStatusRequest } from "../utils/utils.ts"; import { handleDiscordPayload } from "./shardingManager.ts"; import { logRed } from "../utils/logger.ts"; -import { delay } from "https://deno.land/std@0.67.0/async/delay.ts"; import { GatewayOpcode } from "../types/discord.ts"; import { eventHandlers, botGatewayData, } from "./client.ts"; +import { delay } from "https://deno.land/std@0.67.0/async/delay.ts"; +import { cache } from "../utils/cache.ts"; +import { inflate } from "https://deno.land/x/zlib.es@v1.0.0/mod.ts"; const basicShards = new Map(); const heartbeating = new Set(); @@ -66,7 +70,48 @@ export async function createBasicShard( await resume(basicShard, identifyPayload); } - for await (const message of basicShard.socket) { + for await (let message of basicShard.socket) { + if (isWebSocketCloseEvent(message)) { + eventHandlers.debug?.( + { type: "websocketClose", data: { shardID: basicShard.id, message } }, + ); + + // These error codes should just crash the projects + if ([4004, 4005, 4012, 4013, 4014].includes(message.code)) { + logRed(`Close :( ${JSON.stringify(message)}`); + eventHandlers.debug?.( + { + type: "websocketErrored", + data: { shardID: basicShard.id, message }, + }, + ); + + throw new Error( + "Shard.ts: Error occurred that is not resumeable or able to be reconnected.", + ); + } + // These error codes can not be resumed but need to reconnect from start + if ([4003, 4007, 4008, 4009].includes(message.code)) { + eventHandlers.debug?.( + { + type: "websocketReconnecting", + data: { shardID: basicShard.id, message }, + }, + ); + createBasicShard(botGatewayData, identifyPayload, false, shardID); + } else { + basicShard.needToResume = true; + resumeConnection(botGatewayData, identifyPayload, basicShard.id); + } + continue; + } else if (isWebSocketPingEvent(message) || isWebSocketPongEvent(message)) { + continue; + } + + if (message instanceof Uint8Array) { + message = new TextDecoder().decode(inflate(message as Uint8Array)); + } + if (typeof message === "string") { const data = JSON.parse(message); if (!data.t) eventHandlers.rawGateway?.(data); @@ -118,38 +163,6 @@ export async function createBasicShard( handleDiscordPayload(data, basicShard.id); break; } - } else if (isWebSocketCloseEvent(message)) { - eventHandlers.debug?.( - { type: "websocketClose", data: { shardID: basicShard.id, message } }, - ); - - // These error codes should just crash the projects - if ([4004, 4005, 4012, 4013, 4014].includes(message.code)) { - logRed(`Close :( ${JSON.stringify(message)}`); - eventHandlers.debug?.( - { - type: "websocketErrored", - data: { shardID: basicShard.id, message }, - }, - ); - - throw new Error( - "Shard.ts: Error occurred that is not resumeable or able to be reconnected.", - ); - } - // These error codes can not be resumed but need to reconnect from start - if ([4003, 4007, 4008, 4009].includes(message.code)) { - eventHandlers.debug?.( - { - type: "websocketReconnecting", - data: { shardID: basicShard.id, message }, - }, - ); - createBasicShard(botGatewayData, identifyPayload, false, shardID); - } else { - basicShard.needToResume = true; - resumeConnection(botGatewayData, identifyPayload, basicShard.id); - } } } } diff --git a/src/module/client.ts b/src/module/client.ts index 8a098bbaf..1c150d988 100644 --- a/src/module/client.ts +++ b/src/module/client.ts @@ -14,7 +14,7 @@ export let botGatewayData: DiscordBotGatewayData; export const identifyPayload: IdentifyPayload = { token: "", - compress: false, + compress: true, properties: { $os: "linux", $browser: "Discordeno",