Skip to content

Commit

Permalink
move connection to specific location
Browse files Browse the repository at this point in the history
  • Loading branch information
guiguicdd committed Mar 11, 2022
1 parent 666551e commit de39537
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
20 changes: 20 additions & 0 deletions connection/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const { default: makeWASocket, useSingleFileAuthState, DisconnectReason, fetchLatestBaileysVersion } = require("@adiwajshing/baileys")
const { state, saveState } = useSingleFileAuthState('./connection/wabasemdConnection.json')

exports.createConnection = new Promise((resolve, reject) => {
try {
const startSock = async () => {
const { version } = await fetchLatestBaileysVersion()
const sock = makeWASocket({ version, printQRInTerminal: true, auth: state })
sock.ev.on('connection.update', (update) => {
const { connection, lastDisconnect } = update
if (connection === 'close')
lastDisconnect.error?.output?.statusCode !== DisconnectReason.loggedOut
? startSock() : console.log('+ connection closed')
})
sock.ev.on('creds.update', saveState)
if (sock.user) resolve(sock)
}
startSock()
} catch (e) { reject(e) }
})
19 changes: 5 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
const { core } = require('./lib')
const { default: makeWASocket, useSingleFileAuthState, DisconnectReason, fetchLatestBaileysVersion } = require("@adiwajshing/baileys")
const { state, saveState } = useSingleFileAuthState('./wabasemdConnection.json')
const { createConnection } = require('./connection')
const { core } = require('./lib');

const startSock = async () => {
const { version } = await fetchLatestBaileysVersion()
const sock = makeWASocket({ version, printQRInTerminal: true, auth: state })
createConnection.then(sock => {
sock.ev.on('messages.upsert', async m => await core(sock, m))
sock.ev.on('connection.update', (update) => {
const { connection, lastDisconnect } = update
if (connection === 'close') lastDisconnect.error?.output?.statusCode !== DisconnectReason.loggedOut
? startSock() : console.log('+ connection closed')
})
sock.ev.on('creds.update', saveState)
}
})

startSock()
createConnection.catch(erro => console.log(erro, 'Conn'))
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wabase-md",
"version": "2.0.000",
"version": "2.1.000",
"description": "Robo desenvolvido em JS para o whatsapp-md. Com suporte ao Whatsapp Legacy via Baileys",
"main": "index.js",
"directories": {
Expand Down

0 comments on commit de39537

Please sign in to comment.