-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
821 lines (769 loc) · 31 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
import './config.js';
import {
createRequire
} from "module";
import path, {
join
} from 'path';
import {
fileURLToPath,
pathToFileURL
} from 'url';
import {
platform
} from 'process';
global.__filename = function filename(pathURL = import.meta.url, rmPrefix = platform !== 'win32') {
return rmPrefix ? /file:\/\/\//.test(pathURL) ? fileURLToPath(pathURL) : pathURL : pathToFileURL(pathURL).toString()
}
global.__dirname = function dirname(pathURL) {
return path.dirname(global.__filename(pathURL, true))
}
global.__require = function require(dir = import.meta.url) {
return createRequire(dir)
}
import * as ws from 'ws';
import {
readdirSync,
statSync,
unlinkSync,
existsSync,
mkdirSync,
readFileSync,
rmSync,
watch
} from 'fs';
import yargs from 'yargs';
import {
spawn
} from 'child_process';
import lodash from 'lodash';
import chalk from 'chalk';
import syntaxerror from 'syntax-error';
import {
tmpdir
} from 'os';
import chokidar from 'chokidar';
import glob from 'glob';
import {
format,
promisify
} from 'util';
import {
Boom
} from "@hapi/boom";
import Pino from 'pino';
import {
makeWaSocket,
protoType,
serialize
} from './lib/simple.js';
import {
Low,
JSONFile
} from 'lowdb';
import {
mongoDB,
mongoDBV2
} from './lib/mongoDB.js';
const {
DisconnectReason,
useMultiFileAuthState,
MessageRetryMap,
fetchLatestBaileysVersion,
makeCacheableSignalKeyStore,
makeInMemoryStore,
proto,
jidNormalizedUser,
PHONENUMBER_MCC,
Browsers
} = await (await import('@whiskeysockets/baileys')).default;
import readline from "readline"
import {
parsePhoneNumber
} from "libphonenumber-js"
import single2multi from './lib/single2multi.js';
import storeSystem from './lib/store-multi.js';
import Helper from './lib/helper.js';
import emojiRegex from 'emoji-regex';
const pairingCode = process.argv.includes("--pairing-code")
const useMobile = process.argv.includes("--mobile")
const useQr = process.argv.includes("--qr")
const singleToMulti = process.argv.includes("--singleauth")
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
const question = (text) => new Promise((resolve) => rl.question(text, resolve))
import NodeCache from "node-cache"
const msgRetryCounterCache = new NodeCache()
const {
CONNECTING
} = ws
const {
chain
} = lodash
const PORT = process.env.PORT || process.env.SERVER_PORT || 3000
protoType()
serialize()
global.API = (name, path = '/', query = {}, apikeyqueryname) => (name in global.APIs ? global.APIs[name] : name) + path + (query || apikeyqueryname ? '?' + new URLSearchParams(Object.entries({
...query,
...(apikeyqueryname ? {
[apikeyqueryname]: global.APIKeys[name in global.APIs ? global.APIs[name] : name]
} : {})
})) : '')
global.timestamp = {
start: new Date
}
const __dirname = global.__dirname(import.meta.url)
global.opts = new Object(yargs(process.argv.slice(2)).exitProcess(false).parse())
const symbolRegex = /^[^\w\s\d]/u;
const emojiAndSymbolRegex = new RegExp(`(${symbolRegex.source}|${emojiRegex().source})`, 'u');
global.prefix = emojiAndSymbolRegex;
global.db = new Low(/https?:\/\//.test(opts['db'] || '') ? new cloudDBAdapter(opts['db']) : new JSONFile(`${opts._[0] ? opts._[0] + '_' : ''}database.json`))
global.DATABASE = global.db
global.loadDatabase = async function loadDatabase() {
if (global.db.READ) return new Promise((resolve) => setInterval(async function() {
if (!global.db.READ) {
clearInterval(this)
resolve(global.db.data == null ? global.loadDatabase() : global.db.data)
}
}, 1 * 1000))
if (global.db.data !== null) return
global.db.READ = true
await global.db.read().catch(console.error)
global.db.READ = null
global.db.data = {
users: {},
chats: {},
stats: {},
msgs: {},
sticker: {},
settings: {},
...(global.db.data || {})
}
global.db.chain = chain(global.db.data)
}
loadDatabase()
global.authFile = `MufarSession`;
const msgRetryCounterMap = (MessageRetryMap) => {};
const {
version
} = await fetchLatestBaileysVersion();
if (!pairingCode && !useMobile && !useQr && !singleToMulti) {
const title = "OPTIONS";
const message = "--pairing-code, --mobile, --qr, --singleauth";
const boxWidth = 40;
const horizontalLine = chalk.redBright("─".repeat(boxWidth));
const formatText = (text, bgColor, textColor) => chalk[bgColor](chalk[textColor](text.padStart(boxWidth / 2 + text.length / 2).padEnd(boxWidth)));
console.log(`╭${horizontalLine}╮
|${formatText(title, 'bgRed', 'white')}|
├${horizontalLine}┤
|${formatText(message, 'bgWhite', 'red')}|
╰${horizontalLine}╯`);
}
var authFolder = storeSystem.fixFileName(`${Helper.opts._[0] || ''}MufarSession`)
var authFile = `${Helper.opts._[0] || 'session'}.data.json`
var [
isCredsExist,
isAuthSingleFileExist,
authState
] = await Promise.all([
Helper.checkFileExists(authFolder + '/creds.json'),
Helper.checkFileExists(authFile),
storeSystem.useMultiFileAuthState(authFolder)
])
var store = storeSystem.makeInMemoryStore()
// Convert single auth to multi auth
if (Helper.opts['singleauth'] || Helper.opts['singleauthstate']) {
if (!isCredsExist && isAuthSingleFileExist) {
console.debug(chalk.blue('- singleauth -'), chalk.yellow('creds.json not found'), chalk.green('compiling singleauth to multiauth...'));
await single2multi(authFile, authFolder, authState);
console.debug(chalk.blue('- singleauth -'), chalk.green('compiled successfully'));
authState = await storeSystem.useMultiFileAuthState(authFolder);
} else if (!isAuthSingleFileExist) console.error(chalk.blue('- singleauth -'), chalk.red('singleauth file not found'));
}
var storeFile = `${Helper.opts._[0] || 'data'}.store.json`
store.readFromFile(storeFile)
const connectionOptions = {
...(!pairingCode && !useMobile && !useQr && {
printQRInTerminal: false,
mobile: !useMobile
}),
...(pairingCode && {
printQRInTerminal: !pairingCode
}),
...(useMobile && {
mobile: !useMobile
}),
...(useQr && {
printQRInTerminal: true
}),
patchMessageBeforeSending: (message) => {
const requiresPatch = !!(message.buttonsMessage || message.templateMessage || message.listMessage);
if (requiresPatch) {
message = {
viewOnceMessage: {
message: {
messageContextInfo: {
deviceListMetadataVersion: 2,
deviceListMetadata: {}
},
...message
}
}
};
}
return message;
},
msgRetryCounterMap,
logger: Pino({
level: 'fatal'
}),
auth: {
creds: authState.state.creds,
keys: makeCacheableSignalKeyStore(authState.state.keys, Pino().child({
level: 'fatal',
stream: 'store'
})),
},
browser: ['Chrome (Linux)', '', ''],
version,
getMessage: async (key) => {
let jid = jidNormalizedUser(key.remoteJid)
let msg = await store.loadMessage(jid, key.id)
return msg?.message || ""
},
msgRetryCounterCache,
connectTimeoutMs: 60000,
defaultQueryTimeoutMs: 0,
keepAliveIntervalMs: 10000,
emitOwnEvents: true,
fireInitQueries: true,
generateHighQualityLinkPreview: true,
syncFullHistory: true,
markOnlineOnConnect: true
};
global.conn = makeWaSocket(connectionOptions);
store.bind(conn.ev)
conn.isInit = false
if (pairingCode && !conn.authState.creds.registered) {
if (useMobile) conn.logger.error('\nCannot use pairing code with mobile api')
console.log(chalk.cyan('╭──────────────────────────────────────···'));
console.log(`📨 ${chalk.redBright('Please type your WhatsApp number')}:`);
console.log(chalk.cyan('├──────────────────────────────────────···'));
let phoneNumber = await question(` ${chalk.cyan('- Number')}: `);
console.log(chalk.cyan('╰──────────────────────────────────────···'));
phoneNumber = phoneNumber.replace(/[^0-9]/g, '')
if (!Object.keys(PHONENUMBER_MCC).some(v => phoneNumber.startsWith(v))) {
console.log(chalk.cyan('╭─────────────────────────────────────────────────···'));
console.log(`💬 ${chalk.redBright("Start with your country's WhatsApp code, Example 62xxx")}:`);
console.log(chalk.cyan('╰─────────────────────────────────────────────────···'));
console.log(chalk.cyan('╭──────────────────────────────────────···'));
console.log(`📨 ${chalk.redBright('Please type your WhatsApp number')}:`);
console.log(chalk.cyan('├──────────────────────────────────────···'));
phoneNumber = await question(` ${chalk.cyan('- Number')}: `);
console.log(chalk.cyan('╰──────────────────────────────────────···'));
phoneNumber = phoneNumber.replace(/[^0-9]/g, '')
}
let code = await conn.requestPairingCode(phoneNumber)
code = code?.match(/.{1,4}/g)?.join("-") || code
console.log(chalk.cyan('╭──────────────────────────────────────···'));
console.log(` 💻 ${chalk.redBright('Your Pairing Code')}:`);
console.log(chalk.cyan('├──────────────────────────────────────···'));
console.log(` ${chalk.cyan('- Code')}: ${code}`);
console.log(chalk.cyan('╰──────────────────────────────────────···'));
rl.close()
}
if (useMobile && !conn.authState.creds.registered) {
const {
registration
} = conn.authState.creds || {
registration: {}
}
if (!registration.phoneNumber) {
console.log(chalk.cyan('╭──────────────────────────────────────···'));
console.log(`📨 ${chalk.redBright('Please type your WhatsApp number')}:`);
console.log(chalk.cyan('├──────────────────────────────────────···'));
let phoneNumber = await question(` ${chalk.cyan('- Number')}: `);
console.log(chalk.cyan('╰──────────────────────────────────────···'));
phoneNumber = phoneNumber.replace(/[^0-9]/g, '')
if (!Object.keys(PHONENUMBER_MCC).some(v => phoneNumber.startsWith(v))) {
console.log(chalk.cyan('╭─────────────────────────────────────────────────···'));
console.log(`💬 ${chalk.redBright("Start with your country's WhatsApp code, Example 62xxx")}:`);
console.log(chalk.cyan('╰─────────────────────────────────────────────────···'));
console.log(chalk.cyan('╭──────────────────────────────────────···'));
console.log(`📨 ${chalk.redBright('Please type your WhatsApp number')}:`);
console.log(chalk.cyan('├──────────────────────────────────────···'));
phoneNumber = await question(` ${chalk.cyan('- Number')}: `);
console.log(chalk.cyan('╰──────────────────────────────────────···'));
phoneNumber = phoneNumber.replace(/[^0-9]/g, '')
}
registration.phoneNumber = "+" + phoneNumber
}
const phoneNumber = parsePhoneNumber(registration.phoneNumber)
if (!phoneNumber.isValid()) conn.logger.error('\nInvalid phone number: ' + registration.phoneNumber)
registration.phoneNumber = phoneNumber.format("E.164")
registration.phoneNumberCountryCode = phoneNumber.countryCallingCode
registration.phoneNumberNationalNumber = phoneNumber.nationalNumber
const mcc = PHONENUMBER_MCC[phoneNumber.countryCallingCode]
registration.phoneNumberMobileCountryCode = mcc
async function enterCode() {
try {
console.log(chalk.cyan('╭──────────────────────────────────────···'));
console.log(`📨 ${chalk.redBright('Please Enter Your OTP Code')}:`);
console.log(chalk.cyan('├──────────────────────────────────────···'));
const code = await question(` ${chalk.cyan('- Code')}: `);
console.log(chalk.cyan('╰──────────────────────────────────────···'));
const response = await conn.register(code.replace(/[^0-9]/g, '').trim().toLowerCase())
console.log(chalk.cyan('╭─────────────────────────────────────────────────···'));
console.log(`💬 ${chalk.redBright("Successfully registered your phone number.")}`);
console.log(chalk.cyan('╰─────────────────────────────────────────────────···'));
console.log(response)
rl.close()
} catch (error) {
conn.logger.error('\nFailed to register your phone number. Please try again.\n', error)
await askOTP()
}
}
async function askOTP() {
console.log(chalk.cyan('╭──────────────────────────────────────···'));
console.log(`📨 ${chalk.redBright('What method do you want to use? "sms" or "voice"')}`);
console.log(chalk.cyan('├──────────────────────────────────────···'));
let code = await question(` ${chalk.cyan('- Method')}: `);
console.log(chalk.cyan('╰──────────────────────────────────────···'));
code = code.replace(/["']/g, '').trim().toLowerCase()
if (code !== 'sms' && code !== 'voice') return await askOTP()
registration.method = code
try {
await conn.requestRegistrationCode(registration)
await enterCode()
} catch (error) {
conn.logger.error('\nFailed to request registration code. Please try again.\n', error)
await askOTP()
}
}
await askOTP()
}
conn.logger.info('\n🚩 W A I T I N G\n');
if (!opts['test']) {
if (global.db) {
setInterval(async () => {
if (global.db.data) await global.db.write();
if (opts['autocleartmp'] && (global.support || {}).find)(tmp = [os.tmpdir(), 'tmp', 'jadibot'], tmp.forEach((filename) => cp.spawn('find', [filename, '-amin', '3', '-type', 'f', '-delete'])));
}, 30 * 1000);
}
}
if (opts['server'])(await import('./server.js')).default(global.conn, PORT);
async function clearTmp() {
const tmp = [tmpdir(), join(__dirname, "./tmp")];
const filename = [];
tmp.forEach((dirname) =>
readdirSync(dirname).forEach((file) => filename.push(join(dirname, file)))
);
return filename.map((file) => {
const stats = statSync(file);
if (
filename.length >= 1 &&
stats.isFile() &&
Date.now() - stats.mtimeMs >= 1000 * 60 * 3
) {
return unlinkSync(file); // 3 minutes
console.log(chalk.cyanBright("Successfully clear tmp"));
}
return false;
});
}
function clearSessions(folder = "MufarSession") {
let filename = [];
readdirSync(folder).forEach((file) => filename.push(join(folder, file)));
return filename.map((file) => {
let stats = statSync(file);
if (stats.isFile() && Date.now() - stats.mtimeMs >= 1000 * 60 * 120) {
// 1 hours
console.log("Deleted sessions", file);
return unlinkSync(file);
}
return false;
});
}
function purgeSession() {
let prekey = [];
const directorio = readdirSync('./MufarSession');
const filesFolderPreKeys = directorio.filter((file) => {
return file.startsWith('pre-key-');
});
prekey = [...prekey, ...filesFolderPreKeys];
filesFolderPreKeys.forEach((files) => {
unlinkSync(`./MufarSession/${files}`);
});
}
function purgeSessionSB() {
const folderPath = './jadibot';
if (!existsSync(folderPath)) {
mkdirSync(folderPath);
conn.logger.info('\nFolder jadibot berhasil dibuat.');
}
const listaDirectorios = readdirSync('./jadibot/');
let SBprekey = [];
listaDirectorios.forEach((filesInDir) => {
const directorio = readdirSync(`./jadibot/${filesInDir}`);
const DSBPreKeys = directorio.filter((fileInDir) => {
return fileInDir.startsWith('pre-key-');
});
SBprekey = [...SBprekey, ...DSBPreKeys];
DSBPreKeys.forEach((fileInDir) => {
unlinkSync(`./jadibot/${filesInDir}/${fileInDir}`);
});
});
}
function purgeOldFiles() {
const folderPath = './jadibot';
if (!existsSync(folderPath)) {
mkdirSync(folderPath);
conn.logger.info('\nFolder jadibot berhasil dibuat.');
}
const directories = ['./MufarSession/', './jadibot/'];
const oneHourAgo = Date.now() - (60 * 60 * 1000);
directories.forEach((dir) => {
readdirSync(dir, (err, files) => {
if (err) throw err;
files.forEach((file) => {
const filePath = path.join(dir, file);
stat(filePath, (err, stats) => {
if (err) throw err;
if (stats.isFile() && stats.mtimeMs < oneHourAgo && file !== 'creds.json') {
unlinkSync(filePath, (err) => {
if (err) throw err;
conn.logger.info(`\nBerkas ${file} berhasil dihapus`);
});
} else {
conn.logger.warn(`\nBerkas ${file} tidak dihapus`);
}
});
});
});
});
}
async function connectionUpdate(update) {
const {
connection,
lastDisconnect,
isNewLogin,
qr,
isOnline,
receivedPendingNotifications
} = update;
global.stopped = connection;
if (isNewLogin) conn.isInit = true;
const code = lastDisconnect?.error?.output?.statusCode || lastDisconnect?.error?.output?.payload?.statusCode;
if (code && code !== DisconnectReason.loggedOut && conn?.ws.socket == null) {
conn.logger.info(await global.reloadHandler(true).catch(console.error));
}
if (global.db.data == null) loadDatabase();
if (!pairingCode && !useMobile && useQr && qr != 0 && qr != undefined) {
conn.logger.info(chalk.yellow('\n🚩ㅤPindai kode QR ini, kode QR akan kedaluwarsa dalam 60 detik.'));
}
if (connection === "connecting") {
console.log(
chalk.redBright("⚡ Mengaktifkan Bot, Mohon tunggu sebentar...")
);
}
if (connection === "open") {
const {
jid,
name
} = conn.user;
const currentTime = new Date();
const pingStart = new Date();
conn.logger.info(chalk.yellow('\n🚩 R E A D Y'));
}
if (isOnline == true) {
conn.logger.info(chalk.green("Status Aktif"));
}
if (isOnline == false) {
conn.logger.error(chalk.red("Status Mati"));
}
if (receivedPendingNotifications) {
conn.logger.warn(chalk.yellow("Menunggu Pesan Baru"));
}
if (connection == 'close') {
conn.logger.error(chalk.yellow(`\n🚩 Koneksi ditutup, harap hapus folder ${global.authFile} dan pindai ulang kode QR`));
}
}
process.on("unhandledRejection", (reason, p) => {
console.log(" [AntiCrash] :: Unhandled Rejection/Catch");
console.log(reason, p);
});
process.on("uncaughtException", (err, origin) => {
console.log(" [AntiCrash] :: Uncaught Exception/Catch");
console.log(err, origin);
});
process.on("uncaughtExceptionMonitor", (err, origin) => {
console.log(" [AntiCrash] :: Uncaught Exception/Catch (MONITOR)");
console.log(err, origin);
});
process.on("multipleResolves", () => {
null;
});
let isInit = true;
let handler = await import('./handler.js');
global.reloadHandler = async function(restatConn) {
try {
const Handler = await import(`./handler.js?update=${Date.now()}`).catch(console.error);
if (Object.keys(Handler || {}).length) handler = Handler;
} catch (error) {
console.error;
}
if (restatConn) {
const oldChats = global.conn.chats;
try {
global.conn.ws.close();
} catch {}
conn.ev.removeAllListeners();
global.conn = makeWaSocket(connectionOptions, {
chats: oldChats
});
isInit = true;
}
if (!isInit) {
conn.ev.off('messages.upsert', conn.handler);
conn.ev.off('messages.update', conn.pollUpdate);
conn.ev.off('group-participants.update', conn.participantsUpdate);
// conn.ev.off('groups.update', conn.groupsUpdate);
conn.ev.off('message.delete', conn.onDelete);
conn.ev.off("presence.update", conn.presenceUpdate);
conn.ev.off('connection.update', conn.connectionUpdate);
conn.ev.off('creds.update', conn.credsUpdate);
}
const emoji = {
welcome: '👋',
bye: '👋',
promote: '👤👑',
demote: '👤🙅♂️',
desc: '📝',
subject: '📌',
icon: '🖼️',
revoke: '🔗',
announceOn: '🔒',
announceOff: '🔓',
restrictOn: '🚫',
restrictOff: '✅',
};
conn.welcome = `${emoji.welcome} Hallo @user\n\n *W E L C O M E*\n⫹⫺ Di grup @subject\n\n⫹⫺ Baca *DESKRIPSI*\n@desc`;
conn.bye = ` *G O O D B Y E*\n${emoji.bye} Sampai jumpa @user`;
conn.spromote = `*${emoji.promote} @user* sekarang menjadi admin!`;
conn.sdemote = `*${emoji.demote} @user* tidak lagi menjadi admin!`;
conn.sDesc = `${emoji.desc} Deskripsi telah diubah menjadi:\n@desc`;
conn.sSubject = `${emoji.subject} Judul grup telah diubah menjadi:\n@subject`;
conn.sIcon = `${emoji.icon} Icon grup telah diubah!`;
conn.sRevoke = `${emoji.revoke} Link grup telah diubah ke:\n@revoke`;
conn.sAnnounceOn = `${emoji.announceOn} Grup telah ditutup!\nSekarang hanya admin yang dapat mengirim pesan.`;
conn.sAnnounceOff = `${emoji.announceOff} Grup telah dibuka!\nSekarang semua peserta dapat mengirim pesan.`;
conn.sRestrictOn = `${emoji.restrictOn} Edit Info Grup diubah ke hanya admin!`;
conn.sRestrictOff = `${emoji.restrictOff} Edit Info Grup diubah ke semua peserta!`;
conn.handler = handler.handler.bind(global.conn);
conn.pollUpdate = handler.pollUpdate.bind(global.conn);
conn.participantsUpdate = handler.participantsUpdate.bind(global.conn);
//conn.groupsUpdate = handler.groupsUpdate.bind(global.conn);
conn.onDelete = handler.deleteUpdate.bind(global.conn);
conn.presenceUpdate = handler.presenceUpdate.bind(global.conn);
conn.connectionUpdate = connectionUpdate.bind(global.conn);
conn.credsUpdate = authState.saveCreds.bind(global.conn, true);
const currentDateTime = new Date();
const messageDateTime = new Date(conn.ev);
if (currentDateTime >= messageDateTime) {
const chats = Object.entries(conn.chats).filter(([jid, chat]) => !jid.endsWith('@g.us') && chat.isChats).map((v) => v[0]);
} else {
const chats = Object.entries(conn.chats).filter(([jid, chat]) => !jid.endsWith('@g.us') && chat.isChats).map((v) => v[0]);
}
conn.ev.on('messages.upsert', conn.handler);
conn.ev.on("messages.update", conn.pollUpdate);
conn.ev.on('group-participants.update', conn.participantsUpdate);
//conn.ev.on('groups.update', conn.groupsUpdate);
conn.ev.on('message.delete', conn.onDelete);
conn.ev.on("presence.update", conn.presenceUpdate);
conn.ev.on('connection.update', conn.connectionUpdate);
conn.ev.on('creds.update', conn.credsUpdate);
isInit = false;
return true;
};
global.plugins = {};
const pluginFilter = (filename) => /\.js$/.test(filename);
async function filesInit() {
const CommandsFiles = glob.sync("./plugins/**/*.js");
for (let file of CommandsFiles) {
try {
const module = await import(file);
global.plugins[file] = module.default || module;
} catch (e) {
conn.logger.error(e);
delete global.plugins[file];
}
}
}
filesInit()
.then(async (_) => {
console.log(Object.keys(global.plugins));
return await conn.sendMessage(nomorown + "@s.whatsapp.net", {
text: "ʙᴏᴛ sᴜᴄᴄᴇssғᴜʟʟʏ ʀᴇʟᴏᴀᴅ ᴄᴏᴍᴍᴀɴᴅ"
}, {
quoted: null
})
.catch(() => {
return;
});
})
.catch(console.error);
function FileEv(type, file) {
const filename = async (file) => file.replace(/^.*[\\\/]/, "");
console.log(file);
switch (type) {
case "delete":
return delete global.plugins[file];
break;
case "change":
try {
(async () => {
const module = await import(
`${global.__filename(file)}?update=${Date.now()}`
);
global.plugins[file] = module.default || module;
})();
} catch (e) {
conn.logger.error(
`error require plugin '${filename(file)}\n${format(e)}'`
);
} finally {
global.plugins = Object.fromEntries(
Object.entries(global.plugins).sort(([a], [b]) => a.localeCompare(b))
);
}
break;
case "add":
try {
(async () => {
const module = await import(
`${global.__filename(file)}?update=${Date.now()}`
);
global.plugins[file] = module.default || module;
})();
} catch (e) {
conn.logger.error(
`error require plugin '${filename(file)}\n${format(e)}'`
);
} finally {
global.plugins = Object.fromEntries(
Object.entries(global.plugins).sort(([a], [b]) => a.localeCompare(b))
);
}
break;
}
}
function watchFiles() {
let watcher = chokidar.watch("plugins/**/*.js", {
ignored: /(^|[\/\\])\../,
persistent: true,
ignoreInitial: true,
alwaysState: true,
});
const pluginFilter = (filename) => /\.js$/.test(filename);
watcher
.on("add", (path) => {
conn.logger.info(`new plugin - '${path}'`);
return FileEv("add", `./${path}`);
})
.on("change", (path) => {
conn.logger.info(`updated plugin - '${path}'`);
return FileEv("change", `./${path}`);
})
.on("unlink", (path) => {
conn.logger.warn(`deleted plugin - '${path}'`);
return FileEv("delete", `./${path}`);
});
}
watchFiles();
await global.reloadHandler();
async function _quickTest() {
const test = await Promise.all([
spawn('ffmpeg'),
spawn('ffprobe'),
spawn('ffmpeg', ['-hide_banner', '-loglevel', 'error', '-filter_complex', 'color', '-frames:v', '1', '-f', 'webp', '-']),
spawn('convert'),
spawn('magick'),
spawn('gm'),
spawn('find', ['--version']),
].map((p) => {
return Promise.race([
new Promise((resolve) => {
p.on('close', (code) => {
resolve(code !== 127);
});
}),
new Promise((resolve) => {
p.on('error', (_) => resolve(false));
})
]);
}));
const [ffmpeg, ffprobe, ffmpegWebp, convert, magick, gm, find] = test;
const s = global.support = {
ffmpeg,
ffprobe,
ffmpegWebp,
convert,
magick,
gm,
find
};
Object.freeze(global.support);
}
const actions = [{
func: clearTmp,
message: '\nPenyegaran Tempat Penyimpanan Berhasil ✅'
},
{
func: purgeSession,
message: '\nSesi-Sesi Tersimpan Sudah Dihapus ✅'
},
{
func: purgeSessionSB,
message: '\nSesi-Sesi Sub-Bot Telah Dihapus ✅'
},
{
func: purgeOldFiles,
message: '\nBerkas Lama Telah Dihapus ✅'
}
];
for (const action of actions) {
setInterval(async () => {
if (stopped === 'close' || !conn || !conn.user) return;
await action.func();
console.log(chalk.cyanBright(
`\n╭───────────────────────────────────···\n│\n` +
`│ ${action.message}\n│\n` +
`╰───────────────────────────────────···\n`
));
}, 60 * 60 * 1000);
}
function clockString(ms) {
if (isNaN(ms)) return '-- Hari -- Jam -- Menit -- Detik';
const units = [{
label: 'Hari',
value: Math.floor(ms / 86400000)
},
{
label: 'Jam',
value: Math.floor(ms / 3600000) % 24
},
{
label: 'Menit',
value: Math.floor(ms / 60000) % 60
},
{
label: 'Detik',
value: Math.floor(ms / 1000) % 60
}
];
return units.map(unit => `${unit.value.toString().padStart(2, '0')} ${unit.label}`).join(' ');
}
_quickTest().catch(console.error);