diff --git a/lib/gateway/Shard.js b/lib/gateway/Shard.js index d3ff34056..a7a47d52e 100644 --- a/lib/gateway/Shard.js +++ b/lib/gateway/Shard.js @@ -126,11 +126,16 @@ class Shard extends EventEmitter { * @prop {String} message The debug message * @prop {Number} id The ID of the shard */ - this.emit("debug", `Queueing reconnect in ${this.reconnectInterval}ms | Attempt ${this.connectAttempts}`, this.id); - setTimeout(() => { + if(this.sessionID) { + this.emit("debug", `Immediately reconnecting for potential resume | Attempt ${this.connectAttempts}`, this.id); this.client.shards.connect(this); - }, this.reconnectInterval); - this.reconnectInterval = Math.min(Math.round(this.reconnectInterval * (Math.random() * 2 + 1)), 30000); + } else { + this.emit("debug", `Queueing reconnect in ${this.reconnectInterval}ms | Attempt ${this.connectAttempts}`, this.id); + setTimeout(() => { + this.client.shards.connect(this); + }, this.reconnectInterval); + this.reconnectInterval = Math.min(Math.round(this.reconnectInterval * (Math.random() * 2 + 1)), 30000); + } } else if(!options.reconnect) { this.hardReset(); } @@ -1627,8 +1632,10 @@ class Shard extends EventEmitter { err = new Error("Gateway received invalid message"); } else if(event.code === 4003) { err = new Error("Not authenticated"); + this.sessionID = null; } else if(event.code === 4004) { err = new Error("Authentication failed"); + this.sessionID = null; reconnect = false; this.emit("error", new Error(`Invalid token: ${this.client.token}`)); } else if(event.code === 4005) { @@ -1643,9 +1650,11 @@ class Shard extends EventEmitter { err = new Error("Gateway connection was ratelimited"); } else if(event.code === 4010) { err = new Error("Invalid shard key"); + this.sessionID = null; reconnect = false; } else if(event.code === 4011) { err = new Error("Shard has too many guilds (>2500)"); + this.sessionID = null; reconnect = false; } else if(event.code === 1006) { err = new Error("Connection reset by peer"); diff --git a/lib/gateway/ShardManager.js b/lib/gateway/ShardManager.js index 4fcce7ea0..31171197c 100644 --- a/lib/gateway/ShardManager.js +++ b/lib/gateway/ShardManager.js @@ -19,7 +19,7 @@ class ShardManager extends Collection { } connect(shard) { - if(this.lastConnect <= Date.now() - 5000 && !this.find((shard) => shard.connecting)) { + if(shard.sessionID || (this.lastConnect <= Date.now() - 5000 && !this.find((shard) => shard.connecting))) { shard.connect(); this.lastConnect = Date.now() + 7500; } else {