some ws fixes

This commit is contained in:
Skillz4Killz
2021-04-08 14:34:37 +00:00
committed by GitHub
parent 3a180219d6
commit d016fc3764
3 changed files with 18 additions and 8 deletions
+16 -7
View File
@@ -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;
}
}