add sendShardMessage

This commit is contained in:
ITOH
2021-04-16 19:14:48 +02:00
parent 864945f578
commit 6315ba75fc
3 changed files with 23 additions and 5 deletions
+2 -2
View File
@@ -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<StatusUpdate, "afk" | "since">) {
@@ -10,7 +11,7 @@ export function editBotStatus(data: Omit<StatusUpdate, "afk" | "since">) {
`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<StatusUpdate, "afk" | "since">) {
...data,
},
});
ws.processQueue(shard.id);
});
}
+3 -3
View File
@@ -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) => {
+18
View File
@@ -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);
}