Update modules/BotCore.js
Browse files- modules/BotCore.js +61 -0
modules/BotCore.js
CHANGED
|
@@ -95,6 +95,60 @@ class BotCore {
|
|
| 95 |
});
|
| 96 |
}
|
| 97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
/**
|
| 99 |
* Conecta ao WhatsApp
|
| 100 |
*/
|
|
@@ -102,6 +156,9 @@ class BotCore {
|
|
| 102 |
try {
|
| 103 |
this.logger.info('🔗 Conectando ao WhatsApp...');
|
| 104 |
|
|
|
|
|
|
|
|
|
|
| 105 |
const { state, saveCreds } = await useMultiFileAuthState(this.config.AUTH_FOLDER);
|
| 106 |
const { version } = await fetchLatestBaileysVersion();
|
| 107 |
|
|
@@ -497,6 +554,10 @@ class BotCore {
|
|
| 497 |
* Obtém QR Code atual
|
| 498 |
*/
|
| 499 |
getQRCode() {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 500 |
return this.currentQR;
|
| 501 |
}
|
| 502 |
|
|
|
|
| 95 |
});
|
| 96 |
}
|
| 97 |
|
| 98 |
+
/**
|
| 99 |
+
* Verifica e limpa credenciais antigas se necessário
|
| 100 |
+
*/
|
| 101 |
+
async _checkAndCleanOldAuth() {
|
| 102 |
+
const fs = require('fs');
|
| 103 |
+
const path = require('path');
|
| 104 |
+
|
| 105 |
+
const authPath = this.config.AUTH_FOLDER;
|
| 106 |
+
const credsPath = path.join(authPath, 'creds.json');
|
| 107 |
+
|
| 108 |
+
try {
|
| 109 |
+
if (fs.existsSync(credsPath)) {
|
| 110 |
+
const stats = fs.statSync(credsPath);
|
| 111 |
+
const ageHours = (Date.now() - stats.mtime.getTime()) / (1000 * 60 * 60);
|
| 112 |
+
|
| 113 |
+
// Se as credenciais têm mais de 24 horas, força novo login
|
| 114 |
+
if (ageHours > 24) {
|
| 115 |
+
this.logger.warn(`🧹 Credenciais antigas detectadas (${ageHours.toFixed(1)}h). Forçando novo login...`);
|
| 116 |
+
fs.rmSync(authPath, { recursive: true, force: true });
|
| 117 |
+
this.isConnected = false;
|
| 118 |
+
this.currentQR = null;
|
| 119 |
+
return;
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
// Verifica se o arquivo de credenciais é válido
|
| 123 |
+
const credsContent = fs.readFileSync(credsPath, 'utf8');
|
| 124 |
+
const creds = JSON.parse(credsContent);
|
| 125 |
+
|
| 126 |
+
if (!creds || !creds.me) {
|
| 127 |
+
this.logger.warn('📄 Credenciais inválidas detectadas. Forçando novo login...');
|
| 128 |
+
fs.rmSync(authPath, { recursive: true, force: true });
|
| 129 |
+
this.isConnected = false;
|
| 130 |
+
this.currentQR = null;
|
| 131 |
+
return;
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
this.logger.info('✅ Credenciais válidas encontradas');
|
| 135 |
+
} else {
|
| 136 |
+
this.logger.info('📱 Nenhuma credencial salva. Aguardando QR code...');
|
| 137 |
+
this.isConnected = false;
|
| 138 |
+
}
|
| 139 |
+
} catch (error) {
|
| 140 |
+
this.logger.warn('⚠️ Erro ao verificar credenciais:', error.message);
|
| 141 |
+
// Em caso de erro, limpa tudo e força novo login
|
| 142 |
+
try {
|
| 143 |
+
if (fs.existsSync(authPath)) {
|
| 144 |
+
fs.rmSync(authPath, { recursive: true, force: true });
|
| 145 |
+
}
|
| 146 |
+
} catch (e) {}
|
| 147 |
+
this.isConnected = false;
|
| 148 |
+
this.currentQR = null;
|
| 149 |
+
}
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
/**
|
| 153 |
* Conecta ao WhatsApp
|
| 154 |
*/
|
|
|
|
| 156 |
try {
|
| 157 |
this.logger.info('🔗 Conectando ao WhatsApp...');
|
| 158 |
|
| 159 |
+
// Verifica se devemos limpar credenciais antigas
|
| 160 |
+
await this._checkAndCleanOldAuth();
|
| 161 |
+
|
| 162 |
const { state, saveCreds } = await useMultiFileAuthState(this.config.AUTH_FOLDER);
|
| 163 |
const { version } = await fetchLatestBaileysVersion();
|
| 164 |
|
|
|
|
| 554 |
* Obtém QR Code atual
|
| 555 |
*/
|
| 556 |
getQRCode() {
|
| 557 |
+
// Se não está conectado E não tem QR, significa que precisa de login
|
| 558 |
+
if (!this.isConnected && !this.currentQR) {
|
| 559 |
+
this.logger.warn('⚠️ Bot não conectado e sem QR code. Verifique se as credenciais expiraram.');
|
| 560 |
+
}
|
| 561 |
return this.currentQR;
|
| 562 |
}
|
| 563 |
|