Stability

This commit is contained in:
Wizzard 2024-08-06 08:49:12 -04:00
parent 2372aa5300
commit 39a8822ed0
1 changed files with 33 additions and 7 deletions

36
main.js
View File

@ -9,6 +9,7 @@ const updateInterval = 1000;
const retryInterval = 30000; const retryInterval = 30000;
const restartInterval = 2 * 60 * 60 * 1000; const restartInterval = 2 * 60 * 60 * 1000;
const reconnectDelay = 10000; const reconnectDelay = 10000;
const extendedReconnectDelay = 90000;
let rp; let rp;
let startTime = Date.now(); let startTime = Date.now();
@ -38,23 +39,48 @@ function createClient() {
rp.on("disconnected", () => { rp.on("disconnected", () => {
console.log("Disconnected from Discord!"); console.log("Disconnected from Discord!");
rp = null; rp = null;
reconnect("connection closed");
});
rp.transport.on("error", (error) => {
console.error("Error with Discord connection:", error);
rp = null;
if (error.message.includes('RPC_CONNECTION_TIMEOUT')) {
reconnect("RPC_CONNECTION_TIMEOUT");
} else if (error.message.includes('connection closed')) {
reconnect("connection closed");
} else {
reconnect(); reconnect();
}
}); });
rp.login({ clientId: config.clientId }).catch((error) => { rp.login({ clientId: config.clientId }).catch((error) => {
console.error("Error connecting to Discord:", error); console.error("Error connecting to Discord:", error);
rp = null; rp = null;
if (error.message.includes('RPC_CONNECTION_TIMEOUT')) {
reconnect("RPC_CONNECTION_TIMEOUT");
} else if (error.message.includes('connection closed')) {
reconnect("connection closed");
} else {
reconnect(); reconnect();
}
}); });
} }
function reconnect() { function reconnect(errorType) {
if (reconnecting) return; if (reconnecting) return;
reconnecting = true; reconnecting = true;
let delay = reconnectDelay;
if (errorType === "RPC_CONNECTION_TIMEOUT" || errorType === "connection closed") {
delay = extendedReconnectDelay;
console.log(`Encountered ${errorType}, waiting for ${extendedReconnectDelay / 1000} seconds before reconnecting...`);
}
setTimeout(() => { setTimeout(() => {
reconnecting = false; reconnecting = false;
createClient(); createClient();
}, reconnectDelay); }, delay);
} }
async function updateStatus() { async function updateStatus() {
@ -171,14 +197,14 @@ function cleanupAndRestart() {
rp.destroy(); rp.destroy();
rp = null; rp = null;
console.log("RPC connection closed."); console.log("RPC connection closed.");
setTimeout(main, reconnectDelay); setTimeout(main, extendedReconnectDelay);
}).catch(error => { }).catch(error => {
console.error("Error clearing activity:", error); console.error("Error clearing activity:", error);
rp = null; rp = null;
setTimeout(main, reconnectDelay); setTimeout(main, extendedReconnectDelay);
}); });
} else { } else {
setTimeout(main, reconnectDelay); setTimeout(main, extendedReconnectDelay);
} }
} }