From fb8035c4eff3a42e531a8ebb0583636f6667debe Mon Sep 17 00:00:00 2001 From: ITOH <72305210+itohatweb@users.noreply.github.com> Date: Thu, 6 May 2021 15:28:42 +0200 Subject: [PATCH] change: move closeWS & sendShardMessage to ws object --- src/helpers/members/fetch_members.ts | 3 +-- src/helpers/misc/edit_bot_status.ts | 3 +-- src/ws/handle_on_message.ts | 3 +-- src/ws/heartbeat.ts | 3 +-- src/ws/identify.ts | 6 ++---- src/ws/resume.ts | 6 ++---- src/ws/ws.ts | 12 +++++++++--- tests/ws/ws_close.ts | 3 +-- 8 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/helpers/members/fetch_members.ts b/src/helpers/members/fetch_members.ts index 5f02762dd..6aa05964f 100644 --- a/src/helpers/members/fetch_members.ts +++ b/src/helpers/members/fetch_members.ts @@ -5,7 +5,6 @@ import { Errors } from "../../types/discordeno/errors.ts"; import { DiscordGatewayIntents } from "../../types/gateway/gateway_intents.ts"; import type { RequestGuildMembers } from "../../types/members/request_guild_members.ts"; import { Collection } from "../../util/collection.ts"; -import { sendShardMessage } from "../../ws/send_shard_message.ts"; import { ws } from "../../ws/ws.ts"; /** @@ -37,7 +36,7 @@ export function fetchMembers( const nonce = `${guildId}-${Date.now()}`; cache.fetchAllMembersProcessingRequests.set(nonce, resolve); - sendShardMessage(shardId, { + ws.sendShardMessage(shardId, { op: DiscordGatewayOpcodes.RequestGuildMembers, d: { guild_id: guildId, diff --git a/src/helpers/misc/edit_bot_status.ts b/src/helpers/misc/edit_bot_status.ts index bfed6b0bc..288f9df6b 100644 --- a/src/helpers/misc/edit_bot_status.ts +++ b/src/helpers/misc/edit_bot_status.ts @@ -1,7 +1,6 @@ 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) { @@ -11,7 +10,7 @@ export function editBotStatus(data: Omit) { `Running forEach loop in editBotStatus function.`, ); - sendShardMessage(shard, { + ws.sendShardMessage(shard, { op: DiscordGatewayOpcodes.StatusUpdate, d: { since: null, diff --git a/src/ws/handle_on_message.ts b/src/ws/handle_on_message.ts index 33335c8da..4a3a3e0b2 100644 --- a/src/ws/handle_on_message.ts +++ b/src/ws/handle_on_message.ts @@ -8,7 +8,6 @@ import { camelize, delay } from "../util/utils.ts"; import { decompressWith } from "./deps.ts"; import { identify } from "./identify.ts"; import { resume } from "./resume.ts"; -import { sendShardMessage } from "./send_shard_message.ts"; import { ws } from "./ws.ts"; /** Handler for handling every message event from websocket. */ @@ -39,7 +38,7 @@ export async function handleOnMessage(message: any, shardId: number) { shard.heartbeat.lastSentAt = Date.now(); // Discord randomly sends this requiring an immediate heartbeat back - sendShardMessage(shard, { + ws.sendShardMessage(shard, { op: DiscordGatewayOpcodes.Heartbeat, d: shard?.previousSequenceNumber, }, true); diff --git a/src/ws/heartbeat.ts b/src/ws/heartbeat.ts index 133b95982..a04028f74 100644 --- a/src/ws/heartbeat.ts +++ b/src/ws/heartbeat.ts @@ -1,6 +1,5 @@ import { DiscordGatewayOpcodes } from "../types/codes/gateway_opcodes.ts"; import { delay } from "../util/utils.ts"; -import { closeWS } from "./close_ws.ts"; import { identify } from "./identify.ts"; import { ws } from "./ws.ts"; @@ -45,7 +44,7 @@ export async function heartbeat(shardId: number, interval: number) { } if (!currentShard.heartbeat.acknowledged) { - closeWS(currentShard.ws, 3066, "Did not receive an ACK in time."); + ws.closeWS(currentShard.ws, 3066, "Did not receive an ACK in time."); return identify(shardId, ws.maxShards); } diff --git a/src/ws/identify.ts b/src/ws/identify.ts index bcd8d2fa4..c645df9d1 100644 --- a/src/ws/identify.ts +++ b/src/ws/identify.ts @@ -1,6 +1,4 @@ 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) { @@ -9,7 +7,7 @@ export async function identify(shardId: number, maxShards: number) { // Need to clear the old heartbeat interval const oldShard = ws.shards.get(shardId); if (oldShard) { - closeWS(oldShard.ws, 3065, "Reidentifying closure of old shard"); + ws.closeWS(oldShard.ws, 3065, "Reidentifying closure of old shard"); clearInterval(oldShard.heartbeat.intervalId); } @@ -41,7 +39,7 @@ export async function identify(shardId: number, maxShards: number) { }); socket.onopen = () => { - sendShardMessage(shardId, { + ws.sendShardMessage(shardId, { op: DiscordGatewayOpcodes.Identify, d: { ...ws.identifyPayload, shard: [shardId, maxShards] }, }, true); diff --git a/src/ws/resume.ts b/src/ws/resume.ts index 4be26a0ac..d1efa4e7b 100644 --- a/src/ws/resume.ts +++ b/src/ws/resume.ts @@ -1,6 +1,4 @@ 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 resume(shardId: number) { @@ -12,7 +10,7 @@ export async function resume(shardId: number) { if (oldShard) { // HOW TO CLOSE OLD SHARD SOCKET!!! - closeWS(oldShard.ws, 3064, "Resuming the shard, closing old shard."); + ws.closeWS(oldShard.ws, 3064, "Resuming the shard, closing old shard."); // STOP OLD HEARTBEAT clearInterval(oldShard.heartbeat.intervalId); } @@ -48,7 +46,7 @@ export async function resume(shardId: number) { // Resume on open socket.onopen = () => { - sendShardMessage(shardId, { + ws.sendShardMessage(shardId, { op: DiscordGatewayOpcodes.Resume, d: { token: ws.identifyPayload.token, diff --git a/src/ws/ws.ts b/src/ws/ws.ts index 2ff84d92b..76519d39f 100644 --- a/src/ws/ws.ts +++ b/src/ws/ws.ts @@ -1,6 +1,7 @@ import { DiscordGatewayOpcodes } from "../types/codes/gateway_opcodes.ts"; import { Collection } from "../util/collection.ts"; import { cleanupLoadingShards } from "./cleanup_loading_shards.ts"; +import { closeWS } from "./close_ws.ts"; import { createShard } from "./create_shard.ts"; import { log } from "./events.ts"; import { handleDiscordPayload } from "./handle_discord_payload.ts"; @@ -9,6 +10,7 @@ import { heartbeat } from "./heartbeat.ts"; import { identify } from "./identify.ts"; import { processQueue } from "./process_queue.ts"; import { resharder } from "./resharder.ts"; +import { sendShardMessage } from "./send_shard_message.ts"; import { spawnShards } from "./spawn_shards.ts"; import { startGateway } from "./start_gateway.ts"; import { tellClusterToIdentify } from "./tell_cluster_to_identify.ts"; @@ -92,7 +94,7 @@ export const ws = { spawnShards, /** Create the websocket and adds the proper handlers to the websocket. */ createShard, - /** Begins identification of the shard to discord */ + /** Begins identification of the shard to discord. */ identify, /** Begins heartbeating of the shard to keep it alive */ heartbeat, @@ -106,10 +108,14 @@ export const ws = { resharder, /** Cleanups loading shards that were unable to load. */ cleanupLoadingShards, - /** Handles the message events from websocket */ + /** Handles the message events from websocket. */ handleOnMessage, - /** Handles processing queue of requests send to this shard */ + /** Handles processing queue of requests send to this shard. */ processQueue, + /** Closes shard WebSocket connection properly. */ + closeWS, + /** Properly adds a message to the shards queue */ + sendShardMessage, }; export interface DiscordenoShard { diff --git a/tests/ws/ws_close.ts b/tests/ws/ws_close.ts index 1a1ce6111..fdc7d2493 100644 --- a/tests/ws/ws_close.ts +++ b/tests/ws/ws_close.ts @@ -1,5 +1,4 @@ import { delay } from "../../src/util/utils.ts"; -import { closeWS } from "../../src/ws/close_ws.ts"; import { ws } from "../../src/ws/ws.ts"; import { defaultTestOptions } from "./start_bot.ts"; @@ -9,7 +8,7 @@ Deno.test({ async fn() { ws.shards.forEach((shard) => { clearInterval(shard.heartbeat.intervalId); - closeWS(shard.ws, 3061, "Discordeno Testing Finished! Do Not RESUME!"); + ws.closeWS(shard.ws, 3061, "Discordeno Testing Finished! Do Not RESUME!"); }); await delay(3000);