diff --git a/packages/bot/src/bot.ts b/packages/bot/src/bot.ts index 02a3016fb..9b267e27d 100644 --- a/packages/bot/src/bot.ts +++ b/packages/bot/src/bot.ts @@ -67,14 +67,6 @@ export function createBot(options: CreateBotOptions): Bot { // Set up helpers below. helpers: {} as BotHelpers, async start() { - // @ts-expect-error should this work - if (typeof Deno !== 'undefined') { - // @ts-expect-error should this work - const katsura = await import('https://x.nest.land/katsura@1.3.9/src/discordenoFixes/gatewaySocket.ts') - - await katsura(bot.gateway) - } - if (!options.gateway?.connection) { bot.gateway.connection = await bot.rest.getSessionInfo() } diff --git a/packages/gateway/src/Shard.ts b/packages/gateway/src/Shard.ts index 3fb47e0bd..06361135c 100644 --- a/packages/gateway/src/Shard.ts +++ b/packages/gateway/src/Shard.ts @@ -12,11 +12,13 @@ import type { import { GatewayCloseEventCodes, GatewayIntents, GatewayOpcodes } from '@discordeno/types' import { Collection, LeakyBucket, camelize, delay, logger } from '@discordeno/utils' import { inflateSync } from 'node:zlib' -import WebSocket from 'ws' +import NodeWebSocket from 'ws' import type { RequestMemberRequest } from './manager.js' import type { BotStatusUpdate, ShardEvents, ShardGatewayConfig, ShardHeart, ShardSocketRequest, StatusUpdate, UpdateVoiceState } from './types.js' import { ShardSocketCloseCodes, ShardState } from './types.js' +declare let WebSocket: any + export class DiscordenoShard { /** The id of the shard */ id: number @@ -33,7 +35,7 @@ export class DiscordenoShard { /** Current session id of the shard if present. */ sessionId?: string /** This contains the WebSocket connection to Discord, if currently connected. */ - socket?: WebSocket + socket?: NodeWebSocket /** Current internal state of the this. */ state = ShardState.Offline /** The url provided by discord to use when resuming a connection for this this. */ @@ -111,7 +113,7 @@ export class DiscordenoShard { /** Close the socket connection to discord if present. */ close(code: number, reason: string): void { - if (this.socket?.readyState !== WebSocket.OPEN) return + if (this.socket?.readyState !== NodeWebSocket.OPEN) return this.socket?.close(code, reason) } @@ -129,13 +131,13 @@ export class DiscordenoShard { url.searchParams.set('v', this.gatewayConfig.version.toString()) url.searchParams.set('encoding', 'json') - const socket = new WebSocket(url.toString()) + const socket: NodeWebSocket = process?.versions !== undefined ? new NodeWebSocket(url.toString()) : new WebSocket(url.toString()) this.socket = socket // TODO: proper event handling - socket.onerror = (event) => console.log({ error: event, shardId: this.id }) - socket.onclose = async (event) => await this.handleClose(event) - socket.onmessage = async (message) => await this.handleMessage(message) + socket.onerror = (event: NodeWebSocket.ErrorEvent) => console.log({ error: event, shardId: this.id }) + socket.onclose = async (event: NodeWebSocket.CloseEvent) => await this.handleClose(event) + socket.onmessage = async (message: NodeWebSocket.MessageEvent) => await this.handleMessage(message) return await new Promise((resolve) => { socket.onopen = () => { @@ -204,7 +206,7 @@ export class DiscordenoShard { /** Check whether the connection to Discord is currently open. */ isOpen(): boolean { - return this.socket?.readyState === WebSocket.OPEN + return this.socket?.readyState === NodeWebSocket.OPEN } /** Attempt to resume the previous shards session with the gateway. */ @@ -282,7 +284,7 @@ export class DiscordenoShard { } /** Handle a gateway connection close. */ - async handleClose(close: WebSocket.CloseEvent): Promise { + async handleClose(close: NodeWebSocket.CloseEvent): Promise { // gateway.debug("GW CLOSED", { shardId, payload: event }); this.stopHeartbeating() @@ -485,7 +487,7 @@ export class DiscordenoShard { } /** Handle an incoming gateway message. */ - async handleMessage(message: WebSocket.MessageEvent): Promise { + async handleMessage(message: NodeWebSocket.MessageEvent): Promise { let preProcessMessage = message.data // If message compression is enabled,