diff --git a/bot.ts b/bot.ts index 0850adba9..9cb2a99f5 100644 --- a/bot.ts +++ b/bot.ts @@ -235,17 +235,7 @@ export interface HelperUtils { } export async function stopBot(bot: Bot) { - // STOP WS - bot.gateway.shards.forEach((shard) => { - clearInterval(shard.heartbeat.intervalId); - bot.gateway.closeWS( - shard.ws, - 3061, - "Discordeno Testing Finished! Do Not RESUME!", - ); - }); - - await delay(5000); + await bot.gateway.stopGateway(bot.gateway); return bot; } diff --git a/gateway/gateway_manager.ts b/gateway/gateway_manager.ts index cd513b9a6..b3823976e 100644 --- a/gateway/gateway_manager.ts +++ b/gateway/gateway_manager.ts @@ -18,6 +18,7 @@ import { import { resume } from "./resume.ts"; import { sendShardMessage } from "./sendShardMessage.ts"; import { prepareBuckets, spawnShards } from "./spawnShards.ts"; +import { stopGateway } from "./stopGateway"; import { tellWorkerToIdentify } from "./tellWorkerToIdentify.ts"; import { DiscordenoShard } from "./shard.ts"; @@ -88,6 +89,7 @@ export function createGatewayManager( handleOnMessage: options.handleOnMessage ?? handleOnMessage, processGatewayQueue: options.processGatewayQueue ?? processGatewayQueue, closeWS: options.closeWS ?? closeWS, + stopGateway: options.stopGateway ?? stopGateway, sendShardMessage: options.sendShardMessage ?? sendShardMessage, resume: options.resume ?? resume, safeRequestsPerShard: options.safeRequestsPerShard ?? safeRequestsPerShard, @@ -209,6 +211,8 @@ export interface GatewayManager { processGatewayQueue: typeof processGatewayQueue; /** Closes shard WebSocket connection properly. */ closeWS: typeof closeWS; + /** Use this function to stop the gateway properly. */ + stopGateway: typeof stopGateway; /** Properly adds a message to the shards queue. */ sendShardMessage: typeof sendShardMessage; /** Properly resume an old shards session. */ diff --git a/gateway/stopGateway.ts b/gateway/stopGateway.ts new file mode 100644 index 000000000..a4b6b7a7b --- /dev/null +++ b/gateway/stopGateway.ts @@ -0,0 +1,16 @@ +/** Use this function to stop the gateway properly */ +export async function stopGateway(gateway: GatewayManager, code = 3061, reason = "Discordeno Testing Finished! Do Not RESUME!") { + + // STOP WS + gateway.shards.forEach((shard) => { + clearInterval(shard.heartbeat.intervalId); + gateway.closeWS( + shard.ws, + code, + + reason, + ); + }); + + await delay(5000); +}