APRK01 commited on
Commit
9ba9cec
Β·
1 Parent(s): 9dabc42

Fix: Add login retry logic for DNS stability

Browse files
Files changed (1) hide show
  1. 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
- // Debug logging for Render network issues
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
  client.on('debug', (info) => {
96
  if (info.includes('Gateway')) console.log(`[DEBUG] ${info}`);
97
  });
98
 
99
- const loginTimeout = setTimeout(() => {
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();