diff --git a/src/bot.ts b/src/bot.ts index 181001c21..87eb6c8bb 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -30,6 +30,7 @@ export const identifyPayload = { export async function startBot(config: BotConfig) { if (config.eventHandlers) eventHandlers = config.eventHandlers; authorization = `Bot ${config.token}`; + ws.identifyPayload.token = `Bot ${config.token}`; // Initial API connection to get info about bots connection botGatewayData = await getGatewayBot(); diff --git a/src/ws/handle_on_message.ts b/src/ws/handle_on_message.ts index 32f0c4397..1a202fd75 100644 --- a/src/ws/handle_on_message.ts +++ b/src/ws/handle_on_message.ts @@ -1,3 +1,5 @@ +import { eventHandlers } from "../bot.ts"; +import { handlers } from "../handlers/mod.ts"; import { DiscordGatewayOpcodes } from "../types/codes/gateway_opcodes.ts"; import { DiscordReady } from "../types/gateway/ready.ts"; import { decompressWith } from "./deps.ts"; @@ -7,16 +9,14 @@ import { ws } from "./ws.ts"; /** Handler for handling every message event from websocket. */ // deno-lint-ignore no-explicit-any -export function handleOnMessage(message: any, shardId: number) { +export async function handleOnMessage(message: any, shardId: number) { if (message instanceof ArrayBuffer) { message = new Uint8Array(message); } if (message instanceof Uint8Array) { - message = decompressWith( - message, - 0, - (slice: Uint8Array) => ws.utf8decoder.decode(slice), + message = decompressWith(message, 0, (slice: Uint8Array) => + ws.utf8decoder.decode(slice) ); } @@ -29,7 +29,7 @@ export function handleOnMessage(message: any, shardId: number) { case DiscordGatewayOpcodes.Hello: ws.heartbeat( shardId, - (messageData.d as DiscordHeartbeat).heartbeat_interval, + (messageData.d as DiscordHeartbeat).heartbeat_interval ); break; case DiscordGatewayOpcodes.HeartbeatACK: @@ -90,7 +90,16 @@ export function handleOnMessage(message: any, shardId: number) { } } - ws.handleDiscordPayload(messageData, shardId); + if (ws.url) ws.handleDiscordPayload(messageData, shardId); + else { + eventHandlers.raw?.(messageData); + await eventHandlers.dispatchRequirements?.(messageData, shardId); + + if (messageData.op !== DiscordGatewayOpcodes.Dispatch) return; + + return handlers[messageData.t]?.(messageData, shardId); + } + break; } } diff --git a/src/ws/identify.ts b/src/ws/identify.ts index b3e32f599..46c5123e2 100644 --- a/src/ws/identify.ts +++ b/src/ws/identify.ts @@ -31,7 +31,7 @@ export async function identify(shardId: number, maxShards: number) { JSON.stringify({ op: DiscordGatewayOpcodes.Identify, d: { ...ws.identifyPayload, shard: [shardId, maxShards] }, - }), + }) ); };