-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.js
40 lines (31 loc) · 2.12 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const { core } = require('./lib')
const { default: makeWASocket, useSingleFileAuthState, DisconnectReason, fetchLatestBaileysVersion } = require("@adiwajshing/baileys")
const { Boom } = require("@hapi/boom");
const { connFileName } = require("./config/configFile")
const { state, saveState } = useSingleFileAuthState(connFileName)
// Removido temporariamente
const MAIN_LOGGER = require("@adiwajshing/baileys/lib/Utils/logger").default
const logger = MAIN_LOGGER.child({})
logger.level = 'silent'
const startSock = async () => {
const { version } = await fetchLatestBaileysVersion()
const sock = makeWASocket({ logger, version, printQRInTerminal: true, auth: state })
sock.ev.on('messages.upsert', async m => await core(sock, m))
sock.ev.on('connection.update', (update) => {
const { connection, lastDisconnect } = update
if (connection) { console.log("Connection Status: ", connection); }
if (connection !== "close") return
let reason = new Boom(lastDisconnect?.error)?.output?.statusCode;
const DR = DisconnectReason
if (reason === DR.badSession) { console.log(`Sessão corrompida. Apague ${connFileName} e leia o código QR.`); sock.logout(); return }
if (reason === DR.connectionClosed) { console.log("Conexão encerrada. Reconectando..."); startSock(); return }
if (reason === DR.connectionLost) { console.log("Conexão perdida com o servidor. Tentando reconectar..."); startSock(); return }
if (reason === DR.connectionReplaced) { console.log("Sessão atual substituida pela nova aberta. Feche essa sessão primeiro."); sock.logout(); return }
if (reason === DR.loggedOut) { console.log(`Sessão encerrada pelo celular. Apague ${connFileName} e leia o código QR.`); sock.logout(); return }
if (reason === DR.restartRequired) { console.log("Reinio necessario. Reiniciando..."); startSock(); return }
if (reason === DR.timedOut) { console.log("A conexão estourou o tempo limite, Reconectando..."); startSock(); return }
sock.end(`Deu ruim: ${reason}|${lastDisconnect.error}`)
})
sock.ev.on('creds.update', saveState)
}
startSock()