-
Notifications
You must be signed in to change notification settings - Fork 47
/
index.js
749 lines (674 loc) · 23.9 KB
/
index.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
/* --------------------------------- SERVER --------------------------------- */
const express = require("express");
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.static(__dirname));
const port = process.env.PORT || 8080;
app.get("/", (req, res) => {
// res.send("Bot is running");
console.log("Get request to /");
res.sendFile(__dirname + "/index.html");
});
/* -------------------------- delete auth from url -------------------------- */
const authHiddenPath = process.env.authHiddenPath; //to have a hidden path for auth db deletion
const { dropAuth } = require("./db/dropauthDB");
app.get("/" + authHiddenPath, async (req, res) => {
console.log("Get request to /" + authHiddenPath);
let response = await dropAuth();
if (response) res.send("Auth DB deleted!");
else res.send("There is some error!");
});
app.listen(port, () => {
// console.clear();
console.log("\nWeb-server running!\n");
});
/* ------------------------------ add packages ------------------------------ */
const {
default: makeWASocket,
DisconnectReason,
useMultiFileAuthState,
fetchLatestBaileysVersion,
makeCacheableSignalKeyStore,
isJidBroadcast,
} = require("@adiwajshing/baileys");
const pino = require("pino");
const fs = require("fs");
const stringSimilarity = require("string-similarity");
const NodeCache = require("node-cache");
const cache = new NodeCache();
const msgRetryCounterMap = {};
// start a connection
// console.log('state : ', state.creds);
/* ----------------------------- add local files ---------------------------- */
const { setCountMember } = require("./db/countMemberDB");
const { setCountVideo } = require("./db/countVideoDB");
const { getDisableCommandData } = require("./db/disableCommandDB");
const { postStudyInfo } = require("./functions/postStudyInfo");
const { postTechNews } = require("./functions/postTechNews");
const { checkTodayBday } = require("./functions/checkTodayBday");
const { storeAuth, fetchAuth } = require("./db/authDB");
const { getGroupAdmins } = require("./functions/getGroupAdmins");
const { addCommands } = require("./functions/addCommands");
const { LoggerBot, LoggerTg } = require("./functions/loggerBot");
const { forwardSticker } = require("./functions/forwardSticker");
const { memberAddCheck } = require("./functions/memberAddCheck");
const { addDefaultMilestones } = require("./functions/addDefaultMilestone");
const { addUnknownCmd } = require("./db/addUnknownCmdDB");
const { countRemainder } = require("./functions/countRemainder");
// const { setGroupParticipant } = require("./db/groupParticipantDB");
require("dotenv").config();
const myNumber = process.env.myNumber;
const myNumberWithJid = myNumber + "@s.whatsapp.net";
const pvx = process.env.pvx;
const isStickerForward = process.env.isStickerForward;
const prefix = "!";
const stats = {
started: "",
totalMessages: 0,
textMessage: 0,
stickerMessage: 0,
imageMessage: 0,
videoMessage: 0,
documentMessage: 0,
commandExecuted: 0,
newsPosted: 0,
stickerForwarded: 0,
stickerNotForwarded: 0,
memberJoined: 0,
memberLeft: 0,
};
stats.started = new Date().toLocaleString("en-GB", {
timeZone: "Asia/kolkata",
});
let startCount = 1;
let dateCheckerInterval;
const { pvxgroups } = require("./constants/constants");
let milestones = {};
try {
fs.rmSync("./auth_info_multi.json", { recursive: true, force: true });
// fs.unlinkSync("./auth_info_multi.json");
} catch (err) {
console.log("Local auth file already deleted");
}
const startBot = async () => {
console.log(`[STARTING BOT]: ${startCount}`);
LoggerTg(`[STARTING BOT]: ${startCount}`);
try {
const { state, saveCreds } = await useMultiFileAuthState(
"./auth_info_multi.json"
);
const {
commandsPublic,
commandsMembers,
commandsAdmins,
commandsOwners,
allCommandsName,
} = await addCommands();
clearInterval(dateCheckerInterval);
const { version, isLatest } = await fetchLatestBaileysVersion();
console.log(`using WA v${version.join(".")}, isLatest: ${isLatest}`);
let silentLogs = pino({ level: "silent" }); //to hide the chat logs
// let debugLogs = pino({ level: "debug" });
//Fetch login auth
const { cred, auth_row_count } = await fetchAuth(state);
if (auth_row_count != 0) {
state.creds = cred.creds;
}
const bot = makeWASocket({
version,
logger: silentLogs,
printQRInTerminal: true,
auth: {
creds: state.creds,
keys: makeCacheableSignalKeyStore(state.keys, silentLogs),
},
msgRetryCounterMap,
generateHighQualityLinkPreview: true,
shouldIgnoreJid: (jid) => isJidBroadcast(jid),
});
if (pvx === "true") {
let usedDate = new Date()
.toLocaleString("en-GB", { timeZone: "Asia/kolkata" })
.split(",")[0];
dateCheckerInterval = setInterval(async () => {
console.log("SET INTERVAL.");
let todayDate = new Date().toLocaleDateString("en-GB", {
timeZone: "Asia/kolkata",
});
let hour = Number(
new Date()
.toLocaleTimeString("en-GB", {
timeZone: "Asia/kolkata",
})
.split(":")[0]
);
//8 to 24 ON
if (hour >= 8) {
await postTechNews(bot.sendMessage, pvxgroups.pvxtech);
await postStudyInfo(bot.sendMessage, pvxgroups.pvxstudy);
++stats.newsPosted;
}
// if (hour % 12 == 0) kickZeroMano(bot, pvxgroups.pvxmano);
if (usedDate !== todayDate) {
usedDate = todayDate;
checkTodayBday(bot, todayDate, pvxgroups.pvxcommunity);
}
}, 1000 * 60 * 20); //20 min
}
let botNumberJid = bot.user ? bot.user.id : ""; //'1506xxxxx54:[email protected]'
botNumberJid =
botNumberJid.slice(0, botNumberJid.search(":")) +
botNumberJid.slice(botNumberJid.search("@"));
bot.ev.on("groups.upsert", async (msg) => {
//new group added
try {
console.log("[groups.upsert]");
const from = msg[0].id;
cache.del(from + ":groupMetadata");
await bot.sendMessage(from, {
text: `*─「 🔥 <{PVX}> BOT 🔥 」─* \n\nSEND ${prefix}help FOR BOT COMMANDS`,
});
bot.sendMessage(myNumberWithJid, {
text: `Bot is added to group.`,
});
} catch (err) {
await LoggerBot(bot, "groups.upsert", err, msg);
}
});
bot.ev.on("groups.update", async (msg) => {
//subject change, etc
try {
console.log("[groups.update]");
const from = msg[0].id;
cache.del(from + ":groupMetadata");
} catch (err) {
await LoggerBot(bot, "groups.update", err, msg);
}
});
//---------------------------------------group-participants.update-----------------------------------------//
bot.ev.on("group-participants.update", async (msg) => {
console.log("[group-participants.update]");
try {
let from = msg.id;
let numJid = msg.participants[0];
let num_split = `${numJid.split("@s.whatsapp.net")[0]}`;
if (numJid === botNumberJid && msg.action === "remove") {
//bot is removed
bot.sendMessage(myNumberWithJid, {
text: `Bot is removed from group.`,
});
return;
}
cache.del(from + ":groupMetadata");
const groupMetadata = await bot.groupMetadata(from);
let groupSubject = groupMetadata.subject;
if (msg.action === "add") {
// if (groupSubject.toUpperCase().includes("<{PVX}>"))
// await setGroupParticipant(numJid, from, "ADD");
await memberAddCheck(
bot,
from,
num_split,
numJid,
groupSubject,
pvxgroups
);
const text = `${groupSubject}\n[ADD] ${num_split}`;
await bot.sendMessage(myNumberWithJid, { text });
console.log(text);
++stats.memberJoined;
} else if (msg.action === "remove") {
// if (groupSubject.toUpperCase().includes("<{PVX}>"))
// await setGroupParticipant(numJid, from, "REMOVE");
const text = `${groupSubject}\n[REMOVE] ${num_split}`;
await bot.sendMessage(myNumberWithJid, { text });
console.log(text);
++stats.memberLeft;
}
} catch (err) {
await LoggerBot(bot, "group-participants.update", err, msg);
}
});
bot.ev.on("messages.upsert", async (m) => {
// console.log("m", JSON.stringify(m, undefined, 2));
// console.log(m.messages);
try {
//type: append (whatsapp web), notify (app)
if (m.type === "append") return;
const msg = JSON.parse(JSON.stringify(m)).messages[0];
if (msg.key && msg.key.remoteJid == "status@broadcast") return;
if (!msg.message) return; //when demote, add, remove, etc happen then msg.message is not there
//type to extract body text
const type = msg.message.conversation
? "textMessage"
: msg.message.reactionMessage
? "reactionMessage"
: msg.message.imageMessage
? "imageMessage"
: msg.message.videoMessage
? "videoMessage"
: msg.message.stickerMessage
? "stickerMessage"
: msg.message.documentMessage
? "documentMessage"
: msg.message.audioMessage
? "audioMessage"
: msg.message.ephemeralMessage
? "ephemeralMessage"
: msg.message.extendedTextMessage
? "extendedTextMessage"
: msg.message.viewOnceMessageV2
? "viewOnceMessageV2"
: "other";
//ephemeralMessage are from disappearing chat
const acceptedType = [
"textMessage",
"imageMessage",
"videoMessage",
"stickerMessage",
"documentMessage",
"extendedTextMessage",
];
if (!acceptedType.includes(type)) {
return;
}
++stats.totalMessages;
if (type === "extendedTextMessage") ++stats["textMessage"];
else ++stats[type];
//body will have the text message
let body =
type === "textMessage"
? msg.message.conversation
: type === "reactionMessage" && msg.message.reactionMessage.text
? msg.message.reactionMessage.text
: type == "imageMessage" && msg.message.imageMessage.caption
? msg.message.imageMessage.caption
: type == "videoMessage" && msg.message.videoMessage.caption
? msg.message.videoMessage.caption
: type == "documentMessage" && msg.message.documentMessage.captionf
? msg.message.documentMessage.caption
: type == "extendedTextMessage" &&
msg.message.extendedTextMessage.text
? msg.message.extendedTextMessage.text
: "";
body = body.replace(/\n|\r/g, ""); //remove all \n and \r
const from = msg.key.remoteJid;
const isGroup = from.endsWith("@g.us");
let groupMetadata = "";
if (isGroup) {
groupMetadata = cache.get(from + ":groupMetadata");
if (!groupMetadata) {
groupMetadata = await bot.groupMetadata(from);
const success = cache.set(
from + ":groupMetadata",
groupMetadata,
60 * 60
);
}
}
const groupName = isGroup ? groupMetadata.subject : "";
let sender = isGroup ? msg.key.participant : from;
if (msg.key.fromMe) sender = botNumberJid;
if (sender.includes(":"))
//remove : from number
sender =
sender.slice(0, sender.search(":")) +
sender.slice(sender.search("@"));
const senderNumber = sender.split("@")[0];
const senderName = msg.pushName;
//Count message
if (
isGroup &&
groupName.toUpperCase().includes("<{PVX}>") &&
from !== pvxgroups.pvxstickeronly1 &&
from != pvxgroups.pvxstickeronly2 &&
from != pvxgroups.pvxdeals &&
from !== pvxgroups.pvxtesting
) {
if (from === pvxgroups.pvxsticker && type === "stickerMessage") {
console.log("skipping count of sticker message in PVX sticker.");
} else {
const res = await setCountMember(sender, from, senderName);
// console.log(JSON.stringify(res));
await countRemainder(
bot.sendMessage,
res,
from,
senderNumber,
sender
);
}
}
//count video
if (from == pvxgroups.pvxmano && isGroup && type === "videoMessage") {
setCountVideo(sender, from);
}
//Forward all stickers
if (
type === "stickerMessage" &&
isStickerForward === "true" &&
isGroup &&
groupName.toUpperCase().startsWith("<{PVX}>") &&
from !== pvxgroups.pvxstickeronly1 &&
from != pvxgroups.pvxstickeronly2 &&
from !== pvxgroups.pvxmano
) {
const res = await forwardSticker(
bot.sendMessage,
msg.message.stickerMessage,
pvxgroups.pvxstickeronly1,
pvxgroups.pvxstickeronly2
);
if (res) ++stats.stickerForwarded;
else ++stats.stickerNotForwarded;
return;
}
let isCmd = body.startsWith(prefix);
const isMedia = type === "imageMessage" || type === "videoMessage"; //image or video
//auto sticker maker in pvx sticker group [empty caption]
if (from === pvxgroups.pvxsticker && body === "" && isMedia) {
if (
msg.message.videoMessage &&
msg.message.videoMessage.fileLength &&
msg.message.videoMessage.fileLength > 2 * 1000 * 1000
) {
return;
}
isCmd = true;
body = "!s";
}
// if (
// body.endsWith("?") &&
// sender !== botNumberJid &&
// msg.message.extendedTextMessage &&
// msg.message.extendedTextMessage.contextInfo &&
// msg.message.extendedTextMessage.contextInfo.participant ===
// botNumberJid &&
// !isCmd &&
// isGroup
// ) {
// isCmd = true;
// body = "!chatgpt " + body;
// }
// if (from === pvxstatus && !isCmd && body.includes("you")) {
// isCmd = true;
// body = "!ytv " + body;
// }
// if (from === pvxstatus && !isCmd && body.includes("insta")) {
// isCmd = true;
// body = "!insta " + body;
// }
if (!isCmd) {
const messageLog =
"[MESSAGE] " +
(body ? body.substr(0, 30) : type) +
" [FROM] " +
senderNumber +
" [IN] " +
(groupName || from);
console.log(messageLog);
return;
}
if (body[1] == " ") body = body[0] + body.slice(2); //remove space when space btw prefix and commandName like "! help"
const args = body.slice(1).trim().split(/ +/);
const command = args.shift().toLowerCase();
// Display every command info
console.log(
"[COMMAND]",
command,
"[FROM]",
senderNumber,
"[IN]",
groupName || from
);
if (
["score", "scorecard", "scoreboard", "sc", "sb"].includes(command)
) {
//for latest group desc
groupMetadata = await bot.groupMetadata(from);
}
const groupDesc =
isGroup && groupMetadata.desc ? groupMetadata.desc.toString() : "";
const groupMembers = isGroup ? groupMetadata.participants : "";
const groupAdmins = isGroup ? getGroupAdmins(groupMembers) : "";
const isBotGroupAdmins = groupAdmins.includes(botNumberJid) || false;
const isGroupAdmins = groupAdmins.includes(sender) || false;
const content = JSON.stringify(msg.message);
const isTaggedImage =
type === "extendedTextMessage" && content.includes("imageMessage");
const isTaggedVideo =
type === "extendedTextMessage" && content.includes("videoMessage");
const isTaggedSticker =
type === "extendedTextMessage" && content.includes("stickerMessage");
const isTaggedDocument =
type === "extendedTextMessage" && content.includes("documentMessage");
const reply = async (text) => {
await bot.sendMessage(from, { text }, { quoted: msg });
};
//CHECK IF COMMAND IF DISABLED FOR CURRENT GROUP OR NOT, not applicable for group admin
let resDisabled = [];
if (isGroup && !isGroupAdmins) {
resDisabled = cache.get(from + ":resDisabled");
if (!resDisabled) {
resDisabled = await getDisableCommandData(from);
const success = cache.set(
from + ":resDisabled",
resDisabled,
60 * 60
);
}
}
if (resDisabled.includes(command)) {
await reply("❌ Command disabled for this group!");
return;
}
if (command === "enable" || command === "disable") {
cache.del(from + ":resDisabled");
}
// send every command info to my whatsapp, won't work when i send something for bot
if (myNumber && myNumberWithJid !== sender) {
++stats.commandExecuted;
await bot.sendMessage(myNumberWithJid, {
text: `${stats.commandExecuted}) [${prefix}${command}] [${groupName}]`,
});
}
switch (command) {
case "stats":
let statsMessage = "📛 PVX BOT STATS 📛\n";
Object.keys(stats).forEach((key) => {
statsMessage += `\n${key}: ${stats[key]}`;
});
await reply(statsMessage);
return;
case "check":
return;
case "test":
if (myNumberWithJid !== sender) {
await reply(`❌ Command only for owner for bot testing purpose!`);
return;
}
if (args.length === 0) {
await reply(`❌ empty query!`);
return;
}
try {
let resultTest = eval(args[0]);
if (typeof resultTest === "object")
await reply(JSON.stringify(resultTest));
else await reply(resultTest.toString());
} catch (err) {
await reply(err.stack);
}
return;
}
let msgInfoObj = {
from,
prefix,
sender,
senderName,
groupName,
groupDesc,
isBotGroupAdmins,
isGroupAdmins,
isMedia,
type,
isTaggedImage,
isTaggedDocument,
isTaggedVideo,
isTaggedSticker,
myNumber,
botNumberJid,
command,
args,
groupMembers,
groupAdmins,
reply,
milestones,
allCommandsName,
};
try {
/* ----------------------------- public commands ---------------------------- */
if (commandsPublic[command]) {
await commandsPublic[command](bot, msg, msgInfoObj);
return;
}
/* ------------------------- group members commands ------------------------- */
if (commandsMembers[command]) {
if (isGroup) {
await commandsMembers[command](bot, msg, msgInfoObj);
return;
}
reply(
"❌ Group command only!\n\nJoin group to use commands:\nhttps://chat.whatsapp.com/CZeWkEFdoF28bTJPAY63ux"
);
return;
}
/* -------------------------- group admins commands ------------------------- */
if (commandsAdmins[command]) {
if (!isGroup) {
reply(
"❌ Group command only!\n\nJoin group to use commands:\nhttps://chat.whatsapp.com/CZeWkEFdoF28bTJPAY63ux"
);
return;
}
if (isGroupAdmins) {
await commandsAdmins[command](bot, msg, msgInfoObj);
return;
}
reply("❌ Admin command!");
return;
}
/* ----------------------------- owner commands ----------------------------- */
if (commandsOwners[command]) {
if (myNumberWithJid === sender) {
await commandsOwners[command](bot, msg, msgInfoObj);
return;
}
reply("❌ Owner command only!");
return;
}
} catch (err) {
await reply(err.stack);
await LoggerBot(bot, `COMMAND-ERROR in ${groupName}`, err, msg);
return;
}
/* ----------------------------- unknown command ---------------------------- */
let message = `Send ${prefix}help for <{PVX}> BOT commands list`;
const matches = stringSimilarity.findBestMatch(
command,
allCommandsName
);
if (matches.bestMatch.rating > 0.5)
message =
`Did you mean ${prefix}${matches.bestMatch.target}\n\n` + message;
reply(message);
if (command) {
await addUnknownCmd(command);
}
} catch (err) {
await LoggerBot(bot, "messages.upsert", err, m);
}
});
bot.ev.on("connection.update", async (update) => {
try {
LoggerTg(`connection.update: ${JSON.stringify(update)}`);
const { connection, lastDisconnect } = update;
if (connection === "open") {
console.log("Connected");
await bot.sendMessage(myNumberWithJid, {
text: `[BOT STARTED] - ${startCount}`,
});
milestones = await addDefaultMilestones(
bot.groupFetchAllParticipating,
pvxgroups
);
// bot.sendMessage(
// pvxcommunity,
// {
// text: `Yes`,
// },
// {
// quoted: {
// key: {
// remoteJid: pvxcommunity,
// fromMe: false,
// id: "710B5CF29EE7471fakeid",
// participant: "[email protected]",
// },
// messageTimestamp: 1671784177,
// pushName: "xyz",
// message: { conversation: "text" },
// },
// }
// );
} else if (connection === "close") {
// reconnect if not logged out
if (
(lastDisconnect.error &&
lastDisconnect.error.output &&
lastDisconnect.error.output.statusCode) !==
DisconnectReason.loggedOut
) {
await LoggerBot(
false,
"CONNECTION-CLOSED",
lastDisconnect.error,
update
);
++startCount;
setTimeout(() => {
startBot();
}, 1000 * 15);
} else {
LoggerTg(
`[CONNECTION-CLOSED]: You are logged out\nRestarting in 5 sec to scan new QR code!`
);
await dropAuth();
console.log(
"[CONNECTION-CLOSED]: You are logged out\nRestarting in 5 sec to scan new QR code!"
);
setTimeout(() => {
startBot();
}, 1000 * 5);
}
}
console.log("connection update", update);
} catch (err) {
await LoggerBot(false, "connection.update", err, update);
}
});
// listen for when the auth credentials is updated
bot.ev.on("creds.update", async () => {
try {
await saveCreds();
await storeAuth(state);
} catch (err) {
await LoggerBot(false, "creds.update", err, undefined);
}
});
return bot;
} catch (err) {
await LoggerBot(false, "BOT-ERROR", err, "");
}
};
startBot();