From e4e977f44779bc602e91a998e9338d7c401eff15 Mon Sep 17 00:00:00 2001 From: Vlad Frangu Date: Sun, 16 Feb 2020 20:41:37 +0200 Subject: [PATCH] src: update client options and shards value if fetching shard count (#3787) * src: Update client options and shards value if fetching shard count * src: Fix bug and remove more dead code --- src/client/websocket/WebSocketManager.js | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/src/client/websocket/WebSocketManager.js b/src/client/websocket/WebSocketManager.js index 02139f484..0403b52c7 100644 --- a/src/client/websocket/WebSocketManager.js +++ b/src/client/websocket/WebSocketManager.js @@ -18,7 +18,7 @@ const BeforeReadyWhitelist = [ WSEvents.GUILD_MEMBER_REMOVE, ]; -const UNRECOVERABLE_CLOSE_CODES = Object.keys(WSCodes).slice(1); +const UNRECOVERABLE_CLOSE_CODES = Object.keys(WSCodes).slice(1).map(Number); const UNRESUMABLE_CLOSE_CODES = [1000, 4006, 4007]; /** @@ -153,24 +153,17 @@ class WebSocketManager extends EventEmitter { this.gateway = `${gatewayURL}/`; - const { shards } = this.client.options; + let { shards } = this.client.options; if (shards === 'auto') { this.debug(`Using the recommended shard count provided by Discord: ${recommendedShards}`); this.totalShards = this.client.options.shardCount = recommendedShards; - if (!this.client.options.shards.length) { - this.client.options.shards = Array.from({ length: recommendedShards }, (_, i) => i); - } + shards = this.client.options.shards = Array.from({ length: recommendedShards }, (_, i) => i); } - if (Array.isArray(shards)) { - this.totalShards = shards.length; - this.debug(`Spawning shards: ${shards.join(', ')}`); - this.shardQueue = new Set(shards.map(id => new WebSocketShard(this, id))); - } else { - this.debug(`Spawning ${this.totalShards} shards`); - this.shardQueue = new Set(Array.from({ length: this.totalShards }, (_, id) => new WebSocketShard(this, id))); - } + this.totalShards = shards.length; + this.debug(`Spawning shards: ${shards.join(', ')}`); + this.shardQueue = new Set(shards.map(id => new WebSocketShard(this, id))); await this._handleSessionLimit(remaining, reset_after); @@ -340,7 +333,7 @@ class WebSocketManager extends EventEmitter { this.debug(`Manager was destroyed. Called by:\n${new Error('MANAGER_DESTROYED').stack}`); this.destroyed = true; this.shardQueue.clear(); - for (const shard of this.shards.values()) shard.destroy({ closeCode: 1000, reset: true, emit: false }); + for (const shard of this.shards.values()) shard.destroy({ closeCode: 1000, reset: true, emit: false, log: false }); } /**