1
+ const path = require ( "path" ) ;
2
+ const fs = require ( "fs" ) . promises ;
3
+ const login = require ( "fca-unofficial" ) ;
4
+ const ProgressBar = require ( "progress" ) ;
5
+ const chalk = require ( "chalk" ) ;
6
+ const figlet = require ( "figlet" ) ;
7
+ const { handleError } = require ( './0x3/moduleInstaller.js' ) ;
8
+ require ( "./0x3/server.js" ) ;
9
+ const pat = require ( './cmds/pat.js' ) ;
10
+ const bes = require ( './cmds/bes.js' ) ;
11
+
12
+ const appstateFolderPath = path . join (
13
+ __dirname ,
14
+ "0x3" ,
15
+ "credentials" ,
16
+ "cookies"
17
+ ) ;
18
+
19
+ figlet . text (
20
+ "Hexabot" ,
21
+ {
22
+ font : "Standard" ,
23
+ horizontalLayout : "default" ,
24
+ verticalLayout : "default" ,
25
+ } ,
26
+ async ( err , data ) => {
27
+ if ( err ) {
28
+ console . log ( chalk . red ( "Error rendering HEXABOT ASCII art." ) ) ;
29
+ console . dir ( err ) ;
30
+ return ;
31
+ }
32
+
33
+ console . log ( chalk . cyan ( data ) ) ;
34
+ console . log ( chalk . gray ( "Developed by HEXCLAN | ©️ 2023" ) ) ;
35
+ console . log ( chalk . gray ( "This bot is not for sale" ) ) ;
36
+ console . log ( chalk . gray ( "Repository: https://github.com/0x3EF8/Hexabot" ) ) ;
37
+ console . log ( ) ;
38
+
39
+ try {
40
+ const files = await fs . readdir ( appstateFolderPath ) ;
41
+ const appStates = files . filter ( ( file ) => path . extname ( file ) === ".json" ) ;
42
+
43
+ const cmdFolder = path . join ( __dirname , "cmds" ) ;
44
+ const cmdFiles = await fs . readdir ( cmdFolder ) ;
45
+
46
+ const bar = new ProgressBar ( chalk . cyan ( ":bar" ) + " :percent :etas" , {
47
+ total : cmdFiles . length ,
48
+ width : 40 ,
49
+ complete : "█" ,
50
+ incomplete : " " ,
51
+ renderThrottle : 1 ,
52
+ } ) ;
53
+
54
+ const commandFiles = { } ;
55
+ const commandErrors = [ ] ;
56
+
57
+ for ( const file of cmdFiles ) {
58
+ const commandName = file . split ( "." ) [ 0 ] . toLowerCase ( ) ;
59
+
60
+ try {
61
+ commandFiles [ commandName ] = require ( path . join ( cmdFolder , file ) ) ;
62
+ } catch ( error ) {
63
+ commandErrors . push ( { fileName : file , error } ) ;
64
+ }
65
+
66
+ bar . tick ( ) ;
67
+ }
68
+
69
+ if ( bar . complete ) {
70
+ console . log ( chalk . green ( `\n✅ Commands successfully integrated: ${ cmdFiles . length - commandErrors . length } \n` ) ) ;
71
+
72
+ if ( commandErrors . length > 0 ) {
73
+ console . log ( chalk . red ( `⚠️ Alert: ${ commandErrors . length } command${ commandErrors . length === 1 ? '' : 's' } could not be integrated:` ) ) ;
74
+
75
+ for ( const { fileName, error } of commandErrors ) {
76
+ console . log ( chalk . red ( `Error detected in file: ${ fileName } ` ) ) ;
77
+ console . log ( chalk . red ( `Reason: ${ error } ` ) ) ;
78
+ if ( error . stack ) {
79
+ const stackLines = error . stack . split ( '\n' ) ;
80
+ const lineNumber = stackLines [ 1 ] . match ( / : ( \d + ) : \d + \) $ / ) [ 1 ] ;
81
+ console . log ( chalk . red ( `Line: ${ lineNumber } ` ) ) ;
82
+ }
83
+ console . log ( chalk . red ( `---------------------------------` ) ) ;
84
+ }
85
+ console . log ( ) ;
86
+ }
87
+ }
88
+ const userInformation = [ ] ;
89
+
90
+ function displayUserInformation ( ) {
91
+ console . log ( "--------------------------------------------------" ) ;
92
+ console . log ( chalk . cyan ( "User Authentication Report" ) ) ;
93
+ console . log ( "--------------------------------------------------" ) ;
94
+ for ( const { userName, appState } of userInformation ) {
95
+ console . log ( chalk . green ( `Verified User: ${ userName } ` ) ) ;
96
+ console . log ( `Authentication Record: ${ appState } ` ) ;
97
+ }
98
+ console . log ( "--------------------------------------------------" ) ;
99
+ }
100
+
101
+ let completedLogins = 0 ;
102
+ const loginPromises = [ ] ;
103
+
104
+ for ( const appState of appStates ) {
105
+ const appStateData = JSON . parse (
106
+ await fs . readFile ( path . join ( appstateFolderPath , appState ) , "utf8" )
107
+ ) ;
108
+
109
+ const loginPromise = new Promise ( ( resolve ) => {
110
+ login ( { appState : appStateData } , async ( err , api ) => {
111
+ if ( err ) {
112
+ handleError (
113
+ `❌ Login failed. \nAuthentication record: ${ appState } ` ,
114
+ err
115
+ ) ;
116
+ resolve ( null ) ;
117
+ return ;
118
+ }
119
+
120
+ api . setOptions ( {
121
+ listenEvents : true ,
122
+ selfListen : false ,
123
+ autoMarkRead : false ,
124
+ autoMarkDelivery : false ,
125
+ forceLogin : true ,
126
+ } ) ;
127
+
128
+ api . getUserInfo ( api . getCurrentUserID ( ) , ( err , ret ) => {
129
+ if ( err ) {
130
+ handleError (
131
+ `❌ Failed to retrieve user information. \nAuthentication record: ${ appState } ` ,
132
+ err ,
133
+ api
134
+ ) ;
135
+ return ;
136
+ }
137
+
138
+ if ( ret && ret [ api . getCurrentUserID ( ) ] ) {
139
+ const userName = ret [ api . getCurrentUserID ( ) ] . name ;
140
+ userInformation . push ( { userName, appState } ) ;
141
+ }
142
+
143
+ completedLogins ++ ;
144
+ if ( completedLogins === appStates . length ) {
145
+ displayUserInformation ( ) ;
146
+ }
147
+ } ) ;
148
+
149
+ resolve ( api ) ;
150
+ } ) ;
151
+ } ) ;
152
+
153
+ loginPromises . push ( loginPromise ) ;
154
+ }
155
+
156
+ const apis = await Promise . all ( loginPromises ) ;
157
+
158
+ for ( let i = 0 ; i < apis . length ; i ++ ) {
159
+ const api = apis [ i ] ;
160
+ if ( ! api ) {
161
+ const appStateToDelete = appStates [ i ] ;
162
+ console . log ( chalk . yellow ( `Initiating secure deletion of appstate file: ${ appStateToDelete } ` ) ) ;
163
+
164
+ setTimeout ( async ( ) => {
165
+ try {
166
+ await fs . unlink ( path . join ( appstateFolderPath , appStateToDelete ) ) ;
167
+ console . log ( chalk . green ( `✅ Appstate file successfully deleted: ${ appStateToDelete } ` ) ) ;
168
+ } catch ( deleteError ) {
169
+ console . error ( chalk . red ( `❌ Error during appstate file deletion: ${ appStateToDelete } ` , deleteError ) ) ;
170
+ }
171
+ } , 5000 ) ;
172
+ continue ;
173
+ }
174
+
175
+ api . listenMqtt ( async ( err , event ) => {
176
+ if ( err ) {
177
+ handleError ( "Error in MQTT listener:" , err , api ) ;
178
+ return ;
179
+ }
180
+
181
+ try {
182
+ if ( event . type === "message" || event . type === "message_reply" ) {
183
+ const input = event . body . toLowerCase ( ) . trim ( ) ;
184
+ const matchingCommand = Object . keys ( commandFiles ) . find (
185
+ ( commandName ) => {
186
+ const commandPattern = new RegExp (
187
+ `^${ commandName } (\\s+.*|$)`
188
+ ) ;
189
+ return commandPattern . test ( input ) ;
190
+ }
191
+ ) ;
192
+
193
+ const settingsFilePath = path . join (
194
+ __dirname ,
195
+ "." ,
196
+ "json" ,
197
+ "settings.json"
198
+ ) ;
199
+ const settings = JSON . parse (
200
+ await fs . readFile ( settingsFilePath , "utf8" )
201
+ ) ;
202
+
203
+ const userpanelFilePath = path . join (
204
+ __dirname ,
205
+ "." ,
206
+ "json" ,
207
+ "userpanel.json"
208
+ ) ;
209
+ const userpanel = JSON . parse (
210
+ await fs . readFile ( userpanelFilePath , "utf8" )
211
+ ) ;
212
+
213
+ if ( matchingCommand ) {
214
+ const cmd = commandFiles [ matchingCommand ] ;
215
+ if ( ! settings [ 0 ] . sys &&
216
+ ! userpanel . userpanel . VIPS . includes ( event . senderID ) ) {
217
+ api . sendMessage (
218
+ "⚠️ Alert: The system is currently undergoing maintenance.\nNote: Only authorized users are permitted to use commands during this period." ,
219
+ event . threadID
220
+ ) ;
221
+ return ;
222
+ }
223
+
224
+ if ( cmd ) {
225
+ if ( cmd . config && cmd . run ) {
226
+ cmd . run ( { api, event } ) ;
227
+ } else if ( typeof cmd === "function" ) {
228
+ cmd ( event , api ) ;
229
+ } else if ( cmd . onStart ) {
230
+ cmd . onStart ( event , api ) ;
231
+ }
232
+ }
233
+ } else {
234
+ const isPrivateThread = event . threadID == event . senderID ;
235
+ const isGroupChat = ! isPrivateThread ;
236
+ /*const containsQuestion = /(\b(what|how|did|where|hi|hello|if|do|hey)\b|\?|@el)/i.test(input);*/
237
+ const containsQuestion = / ( w h a t | h o w | w h y | w h e r e | d i d ) \b / i. test ( input ) ;
238
+ if ( isPrivateThread ) {
239
+ pat ( event , api ) ;
240
+ } else if ( isGroupChat && containsQuestion ) {
241
+ bes ( event , api ) ;
242
+ }
243
+ }
244
+ }
245
+ } catch ( error ) {
246
+ handleError (
247
+ "An error occurred in the listenMqtt function:" ,
248
+ error ,
249
+ api
250
+ ) ;
251
+ }
252
+ } ) ;
253
+ }
254
+
255
+ } catch ( error ) {
256
+ handleError ( "Error reading directory:" , error ) ;
257
+ }
258
+ }
259
+ ) ;
0 commit comments