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
+1
View File
@@ -30,6 +30,7 @@ export const identifyPayload = {
export async function startBot(config: BotConfig) { export async function startBot(config: BotConfig) {
if (config.eventHandlers) eventHandlers = config.eventHandlers; if (config.eventHandlers) eventHandlers = config.eventHandlers;
authorization = `Bot ${config.token}`; authorization = `Bot ${config.token}`;
ws.identifyPayload.token = `Bot ${config.token}`;
// Initial API connection to get info about bots connection // Initial API connection to get info about bots connection
botGatewayData = await getGatewayBot(); botGatewayData = await getGatewayBot();
+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 { DiscordGatewayOpcodes } from "../types/codes/gateway_opcodes.ts";
import { DiscordReady } from "../types/gateway/ready.ts"; import { DiscordReady } from "../types/gateway/ready.ts";
import { decompressWith } from "./deps.ts"; import { decompressWith } from "./deps.ts";
@@ -7,16 +9,14 @@ import { ws } from "./ws.ts";
/** Handler for handling every message event from websocket. */ /** Handler for handling every message event from websocket. */
// deno-lint-ignore no-explicit-any // 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) { if (message instanceof ArrayBuffer) {
message = new Uint8Array(message); message = new Uint8Array(message);
} }
if (message instanceof Uint8Array) { if (message instanceof Uint8Array) {
message = decompressWith( message = decompressWith(message, 0, (slice: Uint8Array) =>
message, ws.utf8decoder.decode(slice)
0,
(slice: Uint8Array) => ws.utf8decoder.decode(slice),
); );
} }
@@ -29,7 +29,7 @@ export function handleOnMessage(message: any, shardId: number) {
case DiscordGatewayOpcodes.Hello: case DiscordGatewayOpcodes.Hello:
ws.heartbeat( ws.heartbeat(
shardId, shardId,
(messageData.d as DiscordHeartbeat).heartbeat_interval, (messageData.d as DiscordHeartbeat).heartbeat_interval
); );
break; break;
case DiscordGatewayOpcodes.HeartbeatACK: 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; break;
} }
} }
+1 -1
View File
@@ -31,7 +31,7 @@ export async function identify(shardId: number, maxShards: number) {
JSON.stringify({ JSON.stringify({
op: DiscordGatewayOpcodes.Identify, op: DiscordGatewayOpcodes.Identify,
d: { ...ws.identifyPayload, shard: [shardId, maxShards] }, d: { ...ws.identifyPayload, shard: [shardId, maxShards] },
}), })
); );
}; };