hopefully fix discord client going into a constant loop of disconnecting due to no ack and then reconnecting in certain situations

This commit is contained in:
hippoz 2022-02-25 20:00:17 +02:00
parent fb721481e7
commit 7f59b666a2
Signed by: hippoz
GPG key ID: 7C52899193467641

View file

@ -22,6 +22,8 @@ const skipReconnectFor = [
4004, 4010, 4011, 4012, 4013, 4014 4004, 4010, 4011, 4012, 4013, 4014
]; ];
const CLOSE_CONNECTION_ON_NO_ACK = false;
class DiscordClient extends EventEmitter { class DiscordClient extends EventEmitter {
constructor(token, { intents, gatewayUrl="wss://gateway.discord.gg/?v=9&encoding=json&compress=zlib-stream", apiBase="https://discord.com/api/v9" } = {}) { constructor(token, { intents, gatewayUrl="wss://gateway.discord.gg/?v=9&encoding=json&compress=zlib-stream", apiBase="https://discord.com/api/v9" } = {}) {
super(); super();
@ -48,7 +50,7 @@ class DiscordClient extends EventEmitter {
} }
this._heartbeatInterval = setInterval(() => { this._heartbeatInterval = setInterval(() => {
if (!this.gotServerHeartbeatACK) { if (CLOSE_CONNECTION_ON_NO_ACK && !this.gotServerHeartbeatACK) {
logError("Closing due to no heartbeat ACK..."); logError("Closing due to no heartbeat ACK...");
this.ws.close(1000, "No heartbeat ACK."); this.ws.close(1000, "No heartbeat ACK.");
return; return;
@ -224,6 +226,7 @@ class DiscordClient extends EventEmitter {
if (this.ws) { if (this.ws) {
log("a websocket connection already exists, killing..."); log("a websocket connection already exists, killing...");
this.ws.removeAllListeners(); this.ws.removeAllListeners();
this._setHeartbeat(-1);
this.ws.close(); this.ws.close();
this.ws = null; this.ws = null;
} }