Skip to content

Commit

Permalink
fix(provider): ⚡ baileys fix restart
Browse files Browse the repository at this point in the history
  • Loading branch information
leifermendez committed Jan 4, 2023
1 parent b89aebb commit ae83774
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions packages/provider/src/baileys/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class BaileysProvider extends ProviderClass {
saveCredsGlobal = null
constructor() {
super()
this.initBailey().then(() => this.initBusEvents())
this.initBailey().then()
}

/**
Expand All @@ -63,18 +63,28 @@ class BaileysProvider extends ProviderClass {

const statusCode = lastDisconnect?.error?.output?.statusCode

if (statusCode === DisconnectReason.loggedOut) {
rimraf(PATH_BASE, (err) => {
if (err) return
})
/** Conexion cerrada por diferentes motivos */
if (connection === 'close') {
if (statusCode !== DisconnectReason.loggedOut) {
this.initBailey()
}

if (statusCode === DisconnectReason.loggedOut) {
rimraf(PATH_BASE, (err) => {
if (err) return
})

this.initBailey()
this.initBailey()
}
}

if (statusCode && statusCode !== DisconnectReason.loggedOut) {
this.initBailey()
/** Conexion abierta correctamente */
if (connection === 'open') {
this.emit('ready', true)
this.initBusEvents(sock)
}

/** QR Code */
if (qr) {
this.emit('require_action', {
instructions: [
Expand All @@ -85,11 +95,11 @@ class BaileysProvider extends ProviderClass {
})
await baileyGenerateImage(qr)
}

if (connection === 'open') this.emit('ready', true)
})

this.vendor = sock
sock.ev.on('creds.update', async () => {
await saveCreds()
})
} catch (e) {
logger.log(e)
this.emit('auth_failure', [
Expand All @@ -108,12 +118,6 @@ class BaileysProvider extends ProviderClass {
* @returns
*/
busEvents = () => [
{
event: 'creds.update',
func: async () => {
await this.saveCredsGlobal()
},
},
{
event: 'messages.upsert',
func: ({ messages, type }) => {
Expand Down Expand Up @@ -144,7 +148,8 @@ class BaileysProvider extends ProviderClass {
},
]

initBusEvents = () => {
initBusEvents = (_sock) => {
this.vendor = _sock
const listEvents = this.busEvents()

for (const { event, func } of listEvents) {
Expand Down

0 comments on commit ae83774

Please sign in to comment.