Bom dia!
Comecei a estudar sobre Node.js a pouco tempo, e estou tentando criar um bot utilizando o venom-bot como forma de aprendizado, porém estou com alguns problemas referente a exportação de módulos.
Tenho um arquivo chamado ‘index.js’ onde tenho o código abaixo, nele tenho uma váriavel chamada ‘emAtendimento’ onde irei guardar o usuário que enviou a mensagem e em qual status ele se encontra do atendimento, porém para deixar o código mais organizado gostaria de separar algumas funções do arquivo ‘index.js’, para isso criei outro arquivo de teste chamado ‘teste.js’ onde criei duas funções, uma é executada caso o cliente já esteja no hash e a outra é executada caso ele ainda não esteja, e nessas funções eu insiro um novo registro nesse array, conforme os códigos abaixo:
index.js
const venom = require('venom-bot')
const testes = require('./teste')
let emAtendimento = new Map()
venom.create().then(function start(client) {
client.onMessage(async (message) => {
if(message.from.indexOf('@g.us') < 0){
if(emAtendimento.get(message.from) == null){
testes.teste1()
}else{
testes.teste2()
}
}
});
});
module.exports = {
emAtendimento,
}
testes.js
const index = require('./index')
async function teste1(){
console.log('teste 1')
index.emAtendimento.set('aaaaa', 'BBBBBB')
}
async function teste2(){
console.log('teste 2')
index.emAtendimento.set('aaaaa', 'BBBBBB')
}
module.exports = {
teste1, teste2,
}
Porém está dando alguns erros quando executo o ‘index.js’, segue os erros:
(node:4436) Warning: Accessing non-existent property ‘emAtendimento’ of module exports inside circular dependency
(Use node --trace-warnings ...
to show where the warning was created)
(node:4436) UnhandledPromiseRejectionWarning: TypeError: Cannot read property ‘set’ of undefined
at Object.teste1 (C:\Users\dresl\Desktop\Boteco Lagoinha\teste.js:5:25)
at C:\Users\dresl\Desktop\Boteco Lagoinha\index.js:10:24
at EventEmitter. (C:\Users\dresl\Desktop\Boteco Lagoinha\node_modules\venom-bot\dist\api\layers\listener.layer.js:280:25)
at EventEmitter.emit (events.js:315:20)
at C:\Users\dresl\Desktop\Boteco Lagoinha\node_modules\venom-bot\dist\api\layers\listener.layer.js:185:90
at Page._onBindingCalled (C:\Users\dresl\Desktop\Boteco Lagoinha\node_modules\puppeteer\lib\cjs\puppeteer\common\Page.js:717:62)
at C:\Users\dresl\Desktop\Boteco Lagoinha\node_modules\puppeteer\lib\cjs\puppeteer\common\Page.js:151:60
at C:\Users\dresl\Desktop\Boteco Lagoinha\node_modules\puppeteer\lib\cjs\vendor\mitt\src\index.js:51:62
at Array.map ()
at Object.emit (C:\Users\dresl\Desktop\Boteco Lagoinha\node_modules\puppeteer\lib\cjs\vendor\mitt\src\index.js:51:43)
(node:4436) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict
(see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:4436) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate
the Node.js process with a non-zero exit code.
O que estou fazendo de errado?
Obrigado!