1+ // main_global.js
2+ console . log ( "[CARREGANDO] Aguarde..." ) ;
3+
14import pkg from 'whatsapp-web.js' ;
25const { Client, LocalAuth, MessageMedia } = pkg ;
3-
46import qrcode from 'qrcode-terminal' ;
57import fs from 'fs' ;
8+ import { exec } from 'child_process' ;
69
7- const CHAT_ID_ALVO = process . argv [ 2 ] ;
8- if ( ! CHAT_ID_ALVO ) {
9- console . error ( "Use: node main.js <ID_DO_CHAT>" ) ;
10- process . exit ( 1 ) ;
11- }
12-
13- // Cada chat tem um clientId único e persistente
14- const CLIENT_ID = `chat_${ CHAT_ID_ALVO . replace ( / [ ^ a - z A - Z 0 - 9 _ - ] / g, "_" ) } ` ;
10+ const CLIENT_ID = "bot_permanente" ; // sessão única global
1511const BOT_PREFIX = "🤖 *ManyBot:* " ;
1612
13+ // lista fixa de chats que queremos interagir
14+ import { CHATS_PERMITIDOS } from "./env.js"
15+ // parecido com isso:
16+ /*
17+ export const CHATS_PERMITIDOS = [
18+ "123456789101234567@c.us", // pedrinho
19+ "987654321012345678@g.us" // escola
20+ ];
21+ */
22+
23+ let jogoAtivo = null ;
24+
25+ // criar client único
1726const client = new Client ( {
1827 authStrategy : new LocalAuth ( { clientId : CLIENT_ID } ) ,
19- puppeteer : {
20- headless : true ,
21- args : [ '--no-sandbox' , '--disable-setuid-sandbox' ] ,
22- timeout : 60000 ,
23- }
28+ puppeteer : { headless : true }
2429} ) ;
2530
2631client . on ( 'qr' , qr => {
27- console . log ( `[ ${ CHAT_ID_ALVO } ] QR Code gerado. Escaneie apenas uma vez:` ) ;
32+ console . log ( "[BOT ] QR Code gerado. Escaneie apenas uma vez:" ) ;
2833 qrcode . generate ( qr , { small : true } ) ;
2934} ) ;
3035
31- client . on ( 'ready' , ( ) => console . log ( `[${ CHAT_ID_ALVO } ] WhatsApp conectado.` ) ) ;
36+ client . on ( 'ready' , ( ) => {
37+ exec ( "clear" ) ;
38+ console . log ( "[BOT] WhatsApp conectado e sessão permanente" ) ;
39+ } ) ;
3240
3341client . on ( 'disconnected' , reason => {
34- console . warn ( `[${ CHAT_ID_ALVO } ] Desconectado: ${ reason } . Tentando reconectar...` ) ;
42+ console . warn ( `[BOT ] Desconectado: ${ reason } . Tentando reconectar...` ) ;
3543 setTimeout ( ( ) => client . initialize ( ) , 5000 ) ;
3644} ) ;
3745
3846client . on ( 'message_create' , async msg => {
3947 try {
4048 const chat = await msg . getChat ( ) ;
41- if ( chat . id . _serialized !== CHAT_ID_ALVO ) return ;
49+
50+ // filtra apenas chats permitidos
51+ if ( ! CHATS_PERMITIDOS . includes ( chat . id . _serialized ) ) return ;
4252
4353 console . log ( "==================================" ) ;
4454 console . log ( `CHAT NAME : ${ chat . name || chat . id . user || "Sem nome" } ` ) ;
@@ -55,6 +65,7 @@ client.on('message_create', async msg => {
5565 }
5666} ) ;
5767
68+
5869// ---------------- Funções de envio ----------------
5970
6071async function enviarVideo ( cliente , chatId , caminhoArquivo ) {
@@ -88,7 +99,6 @@ function iniciarJogo(chat) {
8899 jogoAtivo = numeroSecreto ;
89100
90101 console . log ( `[JOGO] ${ chat . name } : Número escolhido ${ numeroSecreto } ` ) ;
91- chat . sendMessage ( botMsg ( "Hora do jogo! Tentem adivinhar o número de 1 a 100!" ) ) ;
92102}
93103
94104// ---------------- Download ----------------
@@ -101,45 +111,64 @@ let processingQueue = false;
101111// Garantir que a pasta downloads exista
102112if ( ! fs . existsSync ( 'downloads' ) ) fs . mkdirSync ( 'downloads' ) ;
103113
104- import { exec } from "child_process" ;
114+ function runYtDlp ( cmd1 , cmd2 ) {
115+ return new Promise ( ( resolve , reject ) => {
116+ exec ( cmd1 , ( error , stdout , stderr ) => {
117+ if ( ! error ) return resolve ( { stdout, stderr } ) ;
118+
119+ exec ( cmd2 , ( error2 , stdout2 , stderr2 ) => {
120+ if ( error2 ) return reject ( error2 ) ;
121+ resolve ( { stdout : stdout2 , stderr : stderr2 } ) ;
122+ } ) ;
123+ } ) ;
124+ } ) ;
125+ }
105126
106127function get_video ( url , id ) {
107128 downloadsAtivos ++ ;
108129
109- return new Promise ( ( resolve , reject ) => {
110- const cmd = `yt-dlp -t mp4 --print after_move:filepath -o "downloads/${ id } .%(ext)s" "${ url } "` ;
130+ return new Promise ( async ( resolve , reject ) => {
131+ const cmd1 = `yt-dlp -t mp4 --print after_move:filepath -o "downloads/${ id } .%(ext)s" "${ url } "` ;
132+ const cmd2 = `.\yt-dlp.exe -t mp4 --print after_move:filepath -o "downloads/${ id } .%(ext)s" "${ url } "` ;
111133
112- exec ( cmd , ( error , stdout , stderr ) => {
134+ try {
135+ const { stdout, stderr } = await runYtDlp ( cmd1 , cmd2 ) ;
113136 downloadsAtivos -- ;
114137
115138 if ( stderr ) console . error ( stderr ) ;
116- if ( error ) return reject ( new Error ( `yt-dlp falhou: ${ error . message } ` ) ) ;
117139
118140 const filepath = stdout . trim ( ) ;
119141 if ( ! filepath ) return reject ( new Error ( "yt-dlp não retornou filepath" ) ) ;
120142
121143 resolve ( filepath ) ;
122- } ) ;
144+ } catch ( err ) {
145+ downloadsAtivos -- ;
146+ reject ( new Error ( `yt-dlp falhou: ${ err . message } ` ) ) ;
147+ }
123148 } ) ;
124149}
125150
126151function get_audio ( url , id ) {
127152 downloadsAtivos ++ ;
128153
129- return new Promise ( ( resolve , reject ) => {
130- const cmd = `yt-dlp -t mp3 --print after_move:filepath -o "downloads/${ id } .%(ext)s" "${ url } "` ;
154+ return new Promise ( async ( resolve , reject ) => {
155+ const cmd1 = `yt-dlp -t mp3 --print after_move:filepath -o "downloads/${ id } .%(ext)s" "${ url } "` ;
156+ const cmd2 = `.\yt-dlp.exe -t mp3 --print after_move:filepath -o "downloads/${ id } .%(ext)s" "${ url } "` ;
131157
132- exec ( cmd , ( error , stdout , stderr ) => {
158+ try {
159+ const { stdout, stderr } = await runYtDlp ( cmd1 , cmd2 ) ;
133160 downloadsAtivos -- ;
134161
135162 if ( stderr ) console . error ( stderr ) ;
136- if ( error ) return reject ( new Error ( `yt-dlp falhou: ${ error . message } ` ) ) ;
137163
138164 const filepath = stdout . trim ( ) ;
139165 if ( ! filepath ) return reject ( new Error ( "yt-dlp não retornou filepath" ) ) ;
140166
141167 resolve ( filepath ) ;
142- } ) ;
168+ } catch ( err ) {
169+ downloadsAtivos -- ;
170+ reject ( new Error ( `yt-dlp falhou: ${ err . message } ` ) ) ;
171+ }
143172 } ) ;
144173}
145174
@@ -188,15 +217,28 @@ async function processarComando(msg) {
188217 if ( tokens . length === 1 ) {
189218 chat . sendMessage ( botMsg (
190219 "- `!many ping` -> testa se estou funcionando\n" +
191- "- `!many jogo ` -> jogo de adivinhação\n" +
220+ "- `!many adivinhação <começar|parar> ` -> jogo de adivinhação\n" +
192221 "- `!many video <link>` -> baixo um vídeo da internet para você!\n" +
193222 "- `!many audio <link>` -> baixo um audio da internet para você!"
194223 ) ) ;
195224 return ;
196225 }
197226
198227 if ( tokens [ 1 ] === "ping" ) chat . sendMessage ( botMsg ( "pong 🏓" ) ) ;
199- if ( tokens [ 1 ] === "jogo" ) iniciarJogo ( chat ) ;
228+ if ( tokens [ 1 ] === "adivinhação" ) {
229+ if ( tokens [ 2 ] === undefined ) {
230+ chat . sendMessage ( botMsg ( "Acho que você se esqueceu de algo! 😅\n" +
231+ "🏁 `!many adivinhação começar` -> começa o jogo\n" +
232+ "🛑 `!many adivinhação parar` -> para o jogo atual"
233+ ) ) ;
234+ } else if ( tokens [ 2 ] === "começar" ) {
235+ iniciarJogo ( chat ) ;
236+ chat . sendMessage ( botMsg ( "Hora do jogo! 🏁 Tentem adivinhar o número de 1 a 100 que eu estou pensando!" ) ) ;
237+ } else if ( tokens [ 2 ] === "parar" ) {
238+ jogoAtivo = null
239+ chat . sendMessage ( botMsg ( "O jogo atual foi interrompido 🛑" ) ) ;
240+ }
241+ }
200242
201243 if ( tokens [ 1 ] === "video" && tokens [ 2 ] ) {
202244 chat . sendMessage ( botMsg ( "⏳ Baixando vídeo, aguarde..." ) ) ;
0 commit comments