APRK01 commited on
Commit Β·
9ba9cec
1
Parent(s): 9dabc42
Fix: Add login retry logic for DNS stability
Browse files- src/index.js +32 -15
src/index.js
CHANGED
|
@@ -89,23 +89,40 @@ http.createServer((req, res) => {
|
|
| 89 |
// ββ Login βββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 90 |
const token = process.env.BOT_TOKEN;
|
| 91 |
console.log(` π Token: ${token ? token.slice(0, 10) + '...' + token.slice(-5) : 'MISSING'}`);
|
| 92 |
-
console.log(' β³ Connecting to Discord...');
|
| 93 |
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
client.on('debug', (info) => {
|
| 96 |
if (info.includes('Gateway')) console.log(`[DEBUG] ${info}`);
|
| 97 |
});
|
| 98 |
|
| 99 |
-
|
| 100 |
-
console.error('β LOGIN TIMED OUT after 60s β Discord gateway unreachable');
|
| 101 |
-
console.log('π‘ TIP: Render IPs are sometimes flagged by Discord. Try restarting the service or checking the region.');
|
| 102 |
-
}, 60000);
|
| 103 |
-
|
| 104 |
-
client.login(token).then(() => {
|
| 105 |
-
clearTimeout(loginTimeout);
|
| 106 |
-
console.log(' β
Login promise resolved');
|
| 107 |
-
}).catch(err => {
|
| 108 |
-
clearTimeout(loginTimeout);
|
| 109 |
-
console.error('β LOGIN FAILED:', err.message);
|
| 110 |
-
process.exit(1);
|
| 111 |
-
});
|
|
|
|
| 89 |
// ββ Login βββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 90 |
const token = process.env.BOT_TOKEN;
|
| 91 |
console.log(` π Token: ${token ? token.slice(0, 10) + '...' + token.slice(-5) : 'MISSING'}`);
|
|
|
|
| 92 |
|
| 93 |
+
async function startBot(retryCount = 0) {
|
| 94 |
+
if (retryCount > 10) {
|
| 95 |
+
console.error('β MAX RETRIES REACHED. Bot failed to connect.');
|
| 96 |
+
process.exit(1);
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
console.log(` β³ Connecting to Discord... (Attempt ${retryCount + 1})`);
|
| 100 |
+
|
| 101 |
+
const loginTimeout = setTimeout(() => {
|
| 102 |
+
console.error('β LOGIN TIMED OUT after 60s β Discord gateway unreachable');
|
| 103 |
+
}, 60000);
|
| 104 |
+
|
| 105 |
+
try {
|
| 106 |
+
await client.login(token);
|
| 107 |
+
clearTimeout(loginTimeout);
|
| 108 |
+
console.log(' β
Login promise resolved');
|
| 109 |
+
} catch (err) {
|
| 110 |
+
clearTimeout(loginTimeout);
|
| 111 |
+
console.error(`β LOGIN FAILED: ${err.message}`);
|
| 112 |
+
|
| 113 |
+
if (err.message.includes('ENOTFOUND') || err.message.includes('EAI_AGAIN')) {
|
| 114 |
+
console.log(' π‘ DNS/Network error detected. Retrying in 10s...');
|
| 115 |
+
setTimeout(() => startBot(retryCount + 1), 10000);
|
| 116 |
+
} else {
|
| 117 |
+
console.error(' β οΈ Non-network error. Exiting.');
|
| 118 |
+
process.exit(1);
|
| 119 |
+
}
|
| 120 |
+
}
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
// Debug logging for network issues
|
| 124 |
client.on('debug', (info) => {
|
| 125 |
if (info.includes('Gateway')) console.log(`[DEBUG] ${info}`);
|
| 126 |
});
|
| 127 |
|
| 128 |
+
startBot();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|