Bot stays offline/unresponsive after reconnect

I have my bot deployed on a server, which restarts its internet router every day at 4:32am. To handle this, i implemented a setInterval, which runs every 30 seconds to check if the bot is still alive (function described below as checkAliveAndRestart()). This "works" and logs the client in again and the bot is sending heartbeats again afterwards. As this occurs in the middle of the night when im still asleep, i dont have a way to verify if the bot is responsive after this restart at 4:32am, but since it's sending out heartbeats, i have reason to believe it does (checkAliveAndRestart() also logs still alive). Logs for this are attatched in the first two screenshots. (fyi no one interacted with the bot from 1am to 8:18am, so user interaction couldn't have crashed the bot, client.on('debug', ...) also never logged an error except at 4:32, when it briefly lost internet) (actual problem described part 2/3 due to character limit) node v18.10.0, [email protected] part 1
3 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Ben
BenOP2y ago
Code mentioned above This still gets triggered as still alive!, even after bot no longer sends heartbeats
function checkAliveAndRestart() {
client.fetchGuildPreview(<guild-id>).catch((error) => {
// Set interval to keep checking every 5 seconds
checkAliveInterval = 5000;

// Logging
console.log('level=error msg="bot disconnected!" error="' + stringify(error) + '"');
console.log('level=info msg="attempting to reconnect..."');

// Attempt new login, log depending on success
client.login(process.env['TOKEN']).catch((err) => {
console.log('level=error msg="failed to reconnect!" error="' + stringify(err) + '"');
}).then(() => {
console.log('level=info msg="successfully reconnected!"');
});
}).then(() => {
// Logging
console.log('level=debug msg="still alive!" localTime="' + new Date().toLocaleString() + '"');
// If bot is alive, reset check time back to 30 seconds
checkAliveInterval = 30 * 1000;
});
}
function checkAliveAndRestart() {
client.fetchGuildPreview(<guild-id>).catch((error) => {
// Set interval to keep checking every 5 seconds
checkAliveInterval = 5000;

// Logging
console.log('level=error msg="bot disconnected!" error="' + stringify(error) + '"');
console.log('level=info msg="attempting to reconnect..."');

// Attempt new login, log depending on success
client.login(process.env['TOKEN']).catch((err) => {
console.log('level=error msg="failed to reconnect!" error="' + stringify(err) + '"');
}).then(() => {
console.log('level=info msg="successfully reconnected!"');
});
}).then(() => {
// Logging
console.log('level=debug msg="still alive!" localTime="' + new Date().toLocaleString() + '"');
// If bot is alive, reset check time back to 30 seconds
checkAliveInterval = 30 * 1000;
});
}
part 2 The actual problem (i think) occurs at 6:09am, when client.on('debug',...) logs [RECONNECT] Discord asked us to reconnect. Logs for this are shown in the 4th screenshot. This (according to my still alive! log) works, however the bot is not logging any heartbeats after this point and also stays offline/unresponsive. Oddly enough, the still alive! log can only be printed, if my "ping" (testing if fetching a hardcoded guild preview is successful) works. This means the bot still has API access internally, however is staying offline for reasons unknown to me. This only gets "fixed", after i manually restart the bot. Any help would be really appreciated. part 3
Ben
BenOP2y ago
Want results from more Discord servers?
Add your server