@@ -18,61 +18,79 @@ import ready from "./events/ready";
1818import revoke from "./events/revoke" ;
1919import callEvent from "./events/call" ;
2020
21- const loadingBar = LoadingBar ( "Loading Client | {bar} | {value}%" ) ;
22- const client = new Client ( {
23- puppeteer : {
24- args : [ "--no-sandbox" , "--disable-setuid-sandbox" ] ,
25- executablePath :
26- process . env . PUPPETEER_EXEC_PATH || "/opt/google/chrome/google-chrome" ,
27- } ,
28- authStrategy : new LocalAuth ( ) ,
29- } ) ;
21+ let instance : Client | null = null ;
3022let isLoadingBarStarted = false ;
23+ const loadingBar = LoadingBar ( "Loading Client | {bar} | {value}%" ) ;
24+
25+ async function client ( ) : Promise < Client > {
26+ if ( instance ) return instance ; // prevent re-creating
27+
28+ const newClient = new Client ( {
29+ puppeteer : {
30+ args : [ "--no-sandbox" , "--disable-setuid-sandbox" ] ,
31+ executablePath :
32+ process . env . PUPPETEER_EXEC_PATH || "/opt/google/chrome/google-chrome" ,
33+ } ,
34+ authStrategy : new LocalAuth ( ) ,
35+ } ) ;
36+
37+ registerEvents ( newClient ) ;
3138
32- client . on ( "loading_screen" , ( percent : number , message : string ) => {
33- if ( ! isLoadingBarStarted ) {
34- loadingBar . start ( 100 , 0 , { message } ) ;
35- isLoadingBarStarted = true ;
36- }
39+ instance = newClient ;
40+ await newClient . initialize ( ) ;
41+ return newClient ;
42+ }
3743
38- if ( percent >= 99 ) loadingBar . stop ( ) ;
44+ function registerEvents ( client : Client ) {
45+ client . on ( "loading_screen" , ( percent : number , message : string ) => {
46+ if ( ! isLoadingBarStarted ) {
47+ loadingBar . start ( 100 , 0 , { message } ) ;
48+ isLoadingBarStarted = true ;
49+ }
50+ if ( percent >= 99 ) loadingBar . stop ( ) ;
51+ loadingBar . update ( percent , { message } ) ;
52+ } ) ;
3953
40- loadingBar . update ( percent , { message } ) ;
41- } ) ;
54+ client . on ( "authenticated" , ( ) =>
55+ log . info ( "Auth" , "Client authenticated successfully." ) ,
56+ ) ;
4257
43- client . on ( "authenticated" , ( ) =>
44- log . info ( "Auth" , "Client authenticated successfully." ) ,
45- ) ;
58+ client . on ( "qr" , ( qr : string ) => {
59+ log . info ( "QR" , "Scan this QR code with your WhatsApp app:" ) ;
60+ qrcode . generate ( qr , { small : true } ) ;
61+ } ) ;
4662
47- client . on ( "qr" , ( qr : string ) => {
48- // Generate and scan this code with your phone
49- log . info ( "QR" , "Scan this QR code with your WhatsApp app:" ) ;
50- qrcode . generate ( qr , { small : true } ) ;
51- } ) ;
63+ client . on ( "ready" , ( ) => ready ( ) ) ;
5264
53- client . on ( "ready " , ( ) => ready ( ) ) ;
65+ client . on ( "message_reaction " , ( react : Reaction ) => reaction ( client , react ) ) ;
5466
55- client . on ( "message_reaction " , ( react : Reaction ) => reaction ( client , react ) ) ;
67+ client . on ( "message_create " , ( msg : Message ) => messageEvent ( msg , "create" ) ) ;
5668
57- // client.on("message", (msg) => messageEvent(msg));
58- client . on ( "message_create" , ( msg : Message ) => messageEvent ( msg , "create" ) ) ;
69+ client . on (
70+ "message_edit" ,
71+ ( msg : Message , newBody : string , prevBody : string ) => {
72+ msg . body = newBody ;
73+ Promise . all ( [
74+ messageEdit ( msg , newBody , prevBody ) ,
75+ messageEvent ( msg , "edit" ) ,
76+ ] ) ;
77+ } ,
78+ ) ;
5979
60- client . on ( "message_edit" , ( msg : Message , newBody : string , prevBody : string ) => {
61- msg . body = newBody ;
62- Promise . all ( [ messageEdit ( msg , newBody , prevBody ) , messageEvent ( msg , "edit" ) ] ) ;
63- } ) ;
80+ client . on ( "message_revoke_everyone" , ( msg : Message , revoked_msg ?: Message ) =>
81+ revoke ( msg , revoked_msg ) ,
82+ ) ;
6483
65- client . on ( "message_revoke_everyone" , ( msg : Message , revoked_msg ?: Message ) =>
66- revoke ( msg , revoked_msg ) ,
67- ) ;
84+ client . on ( "call" , ( call : Call ) => callEvent ( call ) ) ;
6885
69- client . on ( "call" , ( call : Call ) => callEvent ( call ) ) ;
86+ client . on ( "group_join" , ( notif : GroupNotification ) => groupJoin ( notif ) ) ;
87+ client . on ( "group_leave" , ( notif : GroupNotification ) => groupLeave ( notif ) ) ;
7088
71- client . on ( "group_join" , ( notif : GroupNotification ) => groupJoin ( notif ) ) ;
72- client . on ( "group_leave" , ( notif : GroupNotification ) => groupLeave ( notif ) ) ;
73- client . on ( "auth_failure" , ( msg : string ) => {
74- loadingBar . stop ( ) ;
75- log . error ( "Auth" , "Authentication failed. Please try again." ) ;
76- } ) ;
89+ client . on ( "auth_failure" , ( ) => {
90+ loadingBar . stop ( ) ;
91+ log . error ( "Auth" , "Authentication failed. Please try again." ) ;
92+ } ) ;
93+ }
7794
7895export { client } ;
96+ export default client ;
0 commit comments