diff --git a/src/helpers/misc/edit_bot_status.ts b/src/helpers/misc/edit_bot_status.ts index 35e09c6bc..bfed6b0bc 100644 --- a/src/helpers/misc/edit_bot_status.ts +++ b/src/helpers/misc/edit_bot_status.ts @@ -1,6 +1,7 @@ import { eventHandlers } from "../../bot.ts"; import { DiscordGatewayOpcodes } from "../../types/codes/gateway_opcodes.ts"; import type { StatusUpdate } from "../../types/gateway/status_update.ts"; +import { sendShardMessage } from "../../ws/send_shard_message.ts"; import { ws } from "../../ws/ws.ts"; export function editBotStatus(data: Omit) { @@ -10,7 +11,7 @@ export function editBotStatus(data: Omit) { `Running forEach loop in editBotStatus function.`, ); - shard.queue.push({ + sendShardMessage(shard, { op: DiscordGatewayOpcodes.StatusUpdate, d: { since: null, @@ -18,6 +19,5 @@ export function editBotStatus(data: Omit) { ...data, }, }); - ws.processQueue(shard.id); }); } diff --git a/src/ws/identify.ts b/src/ws/identify.ts index 0671e8d9a..bcd8d2fa4 100644 --- a/src/ws/identify.ts +++ b/src/ws/identify.ts @@ -1,5 +1,6 @@ import { DiscordGatewayOpcodes } from "../types/codes/gateway_opcodes.ts"; import { closeWS } from "./close_ws.ts"; +import { sendShardMessage } from "./send_shard_message.ts"; import { ws } from "./ws.ts"; export async function identify(shardId: number, maxShards: number) { @@ -40,11 +41,10 @@ export async function identify(shardId: number, maxShards: number) { }); socket.onopen = () => { - ws.shards.get(shardId)?.queue.unshift({ + sendShardMessage(shardId, { op: DiscordGatewayOpcodes.Identify, d: { ...ws.identifyPayload, shard: [shardId, maxShards] }, - }); - ws.processQueue(shardId); + }, true); }; return new Promise((resolve, reject) => { diff --git a/src/ws/send_shard_message.ts b/src/ws/send_shard_message.ts new file mode 100644 index 000000000..1603c088f --- /dev/null +++ b/src/ws/send_shard_message.ts @@ -0,0 +1,18 @@ +import { DiscordenoShard, WebSocketRequest, ws } from "./ws.ts"; + +export function sendShardMessage( + shard: number | DiscordenoShard, + message: WebSocketRequest, + highPriority = false, +) { + if (typeof shard === "number") shard = ws.shards.get(shard)!; + if (!shard) return; + + if (!highPriority) { + shard.queue.push(message); + } else { + shard.queue.unshift(message); + } + + ws.processQueue(shard.id); +}