From a6ba23553be1cf74869894b8337fc80d777be35b Mon Sep 17 00:00:00 2001 From: ayntee Date: Thu, 25 Feb 2021 22:12:22 +0400 Subject: [PATCH] feat(ws/shard): add closeWS() function (#543) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(ws/shard): add closeWS() function * foolproof reason lol * feat(ws/shard): handle 3069 close code * be less verbose 😂 --- src/ws/shard.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/ws/shard.ts b/src/ws/shard.ts index 9f41b45bc..800348e40 100644 --- a/src/ws/shard.ts +++ b/src/ws/shard.ts @@ -163,6 +163,8 @@ export function createShard( data: { shardID: basicShard.id, code, reason, wasClean }, }); createShard(data, identifyPayload, false, shardID); + } else if (code === 3069 && reason === "[discordeno] requested closure") { + return; } else { basicShard.needToResume = true; await resumeConnection(botGatewayData, identifyPayload, shardID); @@ -407,5 +409,16 @@ export function sendWS(payload: DiscordPayload, shardID = 0) { const serialized = JSON.stringify(payload); shard.ws.send(serialized); + + return true; +} + +/** Closes the WebSocket connection or connection attempt */ +export function closeWS(shardID = 0) { + const shard = basicShards.get(shardID); + if (!shard) return false; + + shard.ws.close(3069, "[discordeno] requested closure"); + return true; }