fix(provider): baileys fix restart

This commit is contained in:
Leifer Mendez
2023-01-04 22:12:39 +01:00
parent b89aebb106
commit ae83774365

View File

@@ -37,7 +37,7 @@ class BaileysProvider extends ProviderClass {
saveCredsGlobal = null saveCredsGlobal = null
constructor() { constructor() {
super() super()
this.initBailey().then(() => this.initBusEvents()) this.initBailey().then()
} }
/** /**
@@ -63,18 +63,28 @@ class BaileysProvider extends ProviderClass {
const statusCode = lastDisconnect?.error?.output?.statusCode const statusCode = lastDisconnect?.error?.output?.statusCode
if (statusCode === DisconnectReason.loggedOut) { /** Conexion cerrada por diferentes motivos */
rimraf(PATH_BASE, (err) => { if (connection === 'close') {
if (err) return if (statusCode !== DisconnectReason.loggedOut) {
}) this.initBailey()
}
this.initBailey() if (statusCode === DisconnectReason.loggedOut) {
rimraf(PATH_BASE, (err) => {
if (err) return
})
this.initBailey()
}
} }
if (statusCode && statusCode !== DisconnectReason.loggedOut) { /** Conexion abierta correctamente */
this.initBailey() if (connection === 'open') {
this.emit('ready', true)
this.initBusEvents(sock)
} }
/** QR Code */
if (qr) { if (qr) {
this.emit('require_action', { this.emit('require_action', {
instructions: [ instructions: [
@@ -85,11 +95,11 @@ class BaileysProvider extends ProviderClass {
}) })
await baileyGenerateImage(qr) await baileyGenerateImage(qr)
} }
if (connection === 'open') this.emit('ready', true)
}) })
this.vendor = sock sock.ev.on('creds.update', async () => {
await saveCreds()
})
} catch (e) { } catch (e) {
logger.log(e) logger.log(e)
this.emit('auth_failure', [ this.emit('auth_failure', [
@@ -108,12 +118,6 @@ class BaileysProvider extends ProviderClass {
* @returns * @returns
*/ */
busEvents = () => [ busEvents = () => [
{
event: 'creds.update',
func: async () => {
await this.saveCredsGlobal()
},
},
{ {
event: 'messages.upsert', event: 'messages.upsert',
func: ({ messages, type }) => { func: ({ messages, type }) => {
@@ -144,7 +148,8 @@ class BaileysProvider extends ProviderClass {
}, },
] ]
initBusEvents = () => { initBusEvents = (_sock) => {
this.vendor = _sock
const listEvents = this.busEvents() const listEvents = this.busEvents()
for (const { event, func } of listEvents) { for (const { event, func } of listEvents) {