forked from tuhinpal/WhatsBot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
492 lines (415 loc) Β· 24.3 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
const express = require('express');
const app = express();
const { Client, MessageMedia } = require('whatsapp-web.js');
const config = require('./config')
const qr = require('./modules/qr');
const zee = require('./modules/zee5');
const saavn = require('./modules/jiosaavn');
const pmpermit = require('./modules/pmpermit');
const carbon = require('./modules/carbon');
const telegraph = require('./modules/telegraph');
const serveIndex = require('serve-index');
const youtube = require('./modules/youtube');
const weather = require('./modules/weather');
const { exec } = require('child_process');
const help = require('./modules/help');
const translator = require('./modules/translator');
const start = require('./modules/start');
const ud = require('./modules/ud');
const gitinfo = require('./modules/git');
const cron = require('node-cron');
const cricket = require('./modules/cricket');
const crypto = require('./modules/crypto');
const watch = require('./modules/watch');
const shorten = require('./modules/urlshortner');
const ocr = require('./modules/ocr');
const emailVerifier = require('./modules/emailverifier');
const client = new Client({ puppeteer: { headless: true, args: ['--no-sandbox'] }, session: config.session });
client.initialize();
client.on('auth_failure', msg => {
console.error("There is a problem in authentication, Kindly set the env var again and restart the app");
});
client.on('ready', () => {
console.log('Bot has been started');
});
client.on('message', async msg => {
if (msg.author == undefined && config.pmpermit_enabled == "true") { // Pm check for pmpermit module
var pmpermitcheck = await pmpermit.handler(msg.from.split("@")[0])
const chat = await msg.getChat();
if (pmpermitcheck == "permitted") {
// do nothing
} else if (pmpermitcheck.mute == true && chat.isMuted == false) { // mute
msg.reply(pmpermitcheck.msg)
const chat = await msg.getChat();
var unmuteDate = new Date();
unmuteDate.setSeconds(Number(unmuteDate.getSeconds()) + Number(config.pmpermit_mutetime));
await chat.mute(unmuteDate)
} else if (chat.isMuted == true) {
//do nothing
} else if (pmpermitcheck == "error") {
//do nothing
} else {
msg.reply(pmpermitcheck.msg)
}
} else {
if (msg.body.includes("!info")) {
var startdata = await start.get(await client.info.getBatteryStatus(), client.info.phone)
client.sendMessage(msg.to, new MessageMedia(startdata.mimetype, startdata.data, startdata.filename), { caption: startdata.msg })
}
}
});
const allricketschedules = {} // Will need later
client.on('message_create', async (msg) => {
if (msg.fromMe) {
if (msg.body == "!allow" && config.pmpermit_enabled == "true" && !msg.to.includes("-")) { // allow and unmute the chat (PMPermit module)
pmpermit.permitacton(msg.to.split("@")[0])
var chat = await msg.getChat();
await chat.unmute(true)
msg.reply("Allowed for PM")
} else if (msg.body == "!nopm" && config.pmpermit_enabled == "true" && !msg.to.includes("-")) { // not allowed for pm (PMPermit module)
pmpermit.nopermitacton(msg.to.split("@")[0])
msg.reply("Not Allowed for PM")
} else if (msg.body == "!block" && !msg.to.includes("-")) { // Block an user in pm
var chat = await msg.getChat()
var contact = await chat.getContact()
msg.reply("You have been Blocked")
contact.block()
} else if (msg.body == "!mute" && !msg.to.includes("-")) { // Mute an user in pm
var chat = await msg.getChat()
var unmuteDate = new Date()
unmuteDate.setSeconds(Number(unmuteDate.getSeconds()) + Number(config.pmpermit_mutetime));
await chat.mute(unmuteDate)
msg.reply(`You have been muted for ${config.pmpermit_mutetime / 60} Minutes`)
} else if (msg.body == "!unmute" && !msg.to.includes("-")) { // Unmute an user in pm
var chat = await msg.getChat();
await chat.unmute(true)
msg.reply(`You have been unmuted`)
} else if (msg.body.startsWith("!term ")) { // Terminal
msg.delete(true)
exec("cd public && " + msg.body.replace("!term ", ""), (error, stdout, stderr) => {
if (error) {
client.sendMessage(msg.to, "*whatsbot~:* ```" + error + "```")
} else if (stderr) {
client.sendMessage(msg.to, "*whatsbot~:* ```" + stderr + "```")
} else {
client.sendMessage(msg.to, "*whatsbot~:* ```" + stdout + "```")
}
})
} else if (msg.body.startsWith("!help")) { // help module
msg.delete(true)
var data = await help.mainF(msg.body)
client.sendMessage(msg.to, data)
} else if (msg.body == "!ping") { // Ping command
msg.reply("Pong !!!");
} else if (msg.body == "!start") { // Start command
msg.delete(true)
var startdata = await start.get(await client.info.getBatteryStatus(), client.info.phone)
client.sendMessage(msg.to, new MessageMedia(startdata.mimetype, startdata.data, startdata.filename), { caption: startdata.msg })
} else if (msg.body == '!delete' && msg.hasQuotedMsg) {
msg.delete(true)
var quotedMsg = await msg.getQuotedMessage();
if (quotedMsg.fromMe) {
quotedMsg.delete(true);
} else {
client.sendMessage(msg.to, "Sorry, I can't delete that message.");
}
} else if (msg.body.startsWith("!qr ")) { // QR Code Gen
msg.delete(true)
var data = await qr.qrgen(msg.body.replace("!qr ", ""));
client.sendMessage(msg.to, new MessageMedia(data.mimetype, data.data, data.filename), { caption: `QR code for π\n` + "```" + msg.body.replace("!qr ", "") + "```" });
} else if (msg.body.startsWith("!qr") && msg.hasQuotedMsg) { // QR Code Gen from reply text
msg.delete(true)
var quotedMsg = await msg.getQuotedMessage();
var data = await qr.qrgen(quotedMsg.body);
client.sendMessage(msg.to, new MessageMedia(data.mimetype, data.data, data.filename), { caption: `QR code for π\n` + "```" + quotedMsg.body + "```" });
} else if (msg.body.startsWith("!zee5 ")) { // Zee5 Module
msg.delete(true)
var data = await zee.mainF(msg.body.replace("!zee5 ", ""));
if (data == "error") {
client.sendMessage(msg.to, `πββοΈ *Error*\n\n` + "```Something Unexpected Happened to fetch this Zee5 Content, Maybe it's a wrong url.```")
} else {
client.sendMessage(msg.to, new MessageMedia(data.image.mimetype, data.image.data, data.image.filename), { caption: `π₯ *${data.title}* _(${data.genre})_\n\nπ ` + "```" + data.description + "```" + `\n\n*Stream Url* π\n${data.url}` });
}
} else if (msg.body.startsWith("!jiosaavn ")) { // Jiosaavn Module
msg.delete(true)
var data = await saavn.mainF(msg.body.replace("!jiosaavn ", ""));
if (data == "error") {
client.sendMessage(msg.to, `πββοΈ *Error*\n\n` + "```Something Unexpected Happened to fetch this Jiosaavn Link, Maybe it's a wrong url.```")
} else {
client.sendMessage(msg.to, new MessageMedia(data.image.mimetype, data.image.data, data.image.filename), { caption: `πΆ *${data.title}* _(${data.released_year})_\n\nπ *Artist :* ` + "```" + data.singers + "```\nπ *Album :* " + "```" + data.album + "```" + `\n\n*Download Url* π\n${data.url}` });
}
} else if (msg.body.startsWith("!jiosaavn") && msg.hasQuotedMsg) { // Jiosaavn Module message reply
msg.delete(true)
var quotedMsg = await msg.getQuotedMessage();
var data = await saavn.mainF(quotedMsg.body);
if (data == "error") {
client.sendMessage(msg.to, `πββοΈ *Error*\n\n` + "```Something Unexpected Happened to fetch this Jiosaavn Link, Maybe it's a wrong url.```")
} else {
client.sendMessage(msg.to, new MessageMedia(data.image.mimetype, data.image.data, data.image.filename), { caption: `πΆ *${data.title}* _(${data.released_year})_\n\nπ *Artist :* ` + "```" + data.singers + "```\nπ *Album :* " + "```" + data.album + "```" + `\n\n*Download Url* π\n${data.url}` });
}
} else if (msg.body.startsWith("!carbon ")) { // Carbon Module
msg.delete(true)
var data = await carbon.mainF(msg.body.replace("!carbon ", ""));
if (data == "error") {
client.sendMessage(msg.to, `πββοΈ *Error*\n\n` + "```Something Unexpected Happened to create the Carbon.```")
} else {
client.sendMessage(msg.to, new MessageMedia(data.mimetype, data.data, data.filename), { caption: `Carbon for π\n` + "```" + msg.body.replace("!carbon ", "") + "```" });
}
} else if (msg.body.startsWith("!carbon") && msg.hasQuotedMsg) { // Carbon Module message reply
msg.delete(true)
var quotedMsg = await msg.getQuotedMessage();
var data = await carbon.mainF(quotedMsg.body);
if (data == "error") {
client.sendMessage(msg.to, `πββοΈ *Error*\n\n` + "```Something Unexpected Happened to create the Carbon.```")
} else {
client.sendMessage(msg.to, new MessageMedia(data.mimetype, data.data, data.filename), { caption: `Carbon for π\n` + "```" + quotedMsg.body + "```" });
}
} else if (msg.body.startsWith("!directlink") && msg.hasQuotedMsg) { // Telegraph Module
msg.delete(true)
var quotedMsg = await msg.getQuotedMessage();
var attachmentData = await quotedMsg.downloadMedia();
var data = await telegraph.mainF(attachmentData);
if (data == "error") {
quotedMsg.reply(`Error occured while create direct link.`)
} else {
quotedMsg.reply(`π *Direct Link π*\n\n` + "```" + data + "```")
}
} else if (msg.body.startsWith("!yt ")) { // Youtube Module
msg.delete(true)
var data = await youtube.mainF(msg.body.replace("!yt ", ""));
if (data == "error") {
client.sendMessage(msg.to, `πββοΈ *Error*\n\n` + "```Something Unexpected Happened to fetch the YouTube video```")
} else {
client.sendMessage(msg.to, new MessageMedia(data.image.mimetype, data.image.data, data.image.filename), { caption: `*${data.title}*\n\nViews: ` + "```" + data.views + "```\nLikes: " + "```" + data.likes + "```\nComments: " + "```" + data.comments + "```\n\n*Download Link* π\n" + "```" + data.download_link + "```" });
}
} else if (msg.body.startsWith("!yt") && msg.hasQuotedMsg) { // Youtube Module Reply
msg.delete(true)
var quotedMsg = await msg.getQuotedMessage();
var data = await youtube.mainF(quotedMsg.body);
if (data == "error") {
client.sendMessage(msg.to, `πββοΈ *Error*\n\n` + "```Something Unexpected Happened to fetch the YouTube video```")
} else {
client.sendMessage(msg.to, new MessageMedia(data.image.mimetype, data.image.data, data.image.filename), { caption: `*${data.title}*\n\nViews: ` + "```" + data.views + "```\nLikes: " + "```" + data.likes + "```\nComments: " + "```" + data.comments + "```\n\n*Download Link* π\n" + "```" + data.download_link + "```" });
}
} else if (msg.body.startsWith("!weather ")) { // Weather Module
msg.delete(true)
var data = await weather.mainF(msg.body.replace("!weather ", ""));
if (data == "error") {
client.sendMessage(msg.to, `πββοΈ *Error*\n\n` + "```Something Unexpected Happened to fetch Weather```")
} else {
client.sendMessage(msg.to, `*Today's Weather at ${data.place}*\n` + "```" + data.current_observation.text + " (" + data.current_observation.temperature + "Β°C)```\n\n*Type:* " + "```" + data.today_forcast.text + "```\n*Max temperature:* " + "```" + data.today_forcast.high + "Β°C```\n*Min temperature:* " + "```" + data.today_forcast.low + "Β°C```");
}
} else if (msg.body.startsWith("!tr") && msg.hasQuotedMsg) { // Translator Module reply
msg.delete(true)
var quotedMsg = await msg.getQuotedMessage()
var data = await translator.argu(quotedMsg.body, msg.body)
if (data == "error") {
client.sendMessage(msg.to, `πββοΈ *Error*\n\n` + "```Something Unexpected Happened while translate```")
} else {
client.sendMessage(msg.to, `*Original (${data.ori_lang}) :* ` + "```" + data.original + "```\n\n" + `*Translation (${data.trans_lang}) :* ` + "```" + data.translated + "```")
}
} else if (msg.body.startsWith("!tr")) { // Translator Module
msg.delete(true)
var data = await translator.single(msg.body)
if (data == "error") {
client.sendMessage(msg.to, `πββοΈ *Error*\n\n` + "```Something Unexpected Happened while translate```")
} else {
client.sendMessage(msg.to, `*Original (${data.ori_lang}) :* ` + "```" + data.original + "```\n\n" + `*Translation (${data.trans_lang}) :* ` + "```" + data.translated + "```")
}
} else if (msg.body.startsWith("!ud ")) { // Urban Dictionary Module
msg.delete(true)
var data = await ud.mainF(msg.body.replace("!ud ", ""))
if (data == "error") {
client.sendMessage(msg.to, `πββοΈ *Error*\n\n` + "```Something Unexpected Happened while Lookup on Urban Dictionary```")
} else {
client.sendMessage(msg.to, "*Term:* ```" + data.term + "```\n\n" + "*Definition:* ```" + data.def + "```\n\n" + "*Example:* ```" + data.example + "```")
}
} else if (msg.body.startsWith("!sticker") && msg.hasQuotedMsg) { // Sticker Module
msg.delete(true)
var quotedMsg = await msg.getQuotedMessage();
if (quotedMsg.hasMedia) {
var attachmentData = await quotedMsg.downloadMedia();
client.sendMessage(msg.to, new MessageMedia(attachmentData.mimetype, attachmentData.data, attachmentData.filename), { sendMediaAsSticker: true });
} else {
client.sendMessage(msg.to, `πββοΈ *Error*\n\n` + "```No image found to make a Sticker```")
}
} else if (msg.body == "!awake") {
client.sendPresenceAvailable()
msg.reply("```" + "I will be online from now." + "```")
} else if (msg.body.startsWith('!git ')) { // Gitinfo Module with link
msg.delete(true)
var data = await gitinfo.detail(msg.body.replace('!git ', ''))
if (data.status) {
if (data.data.status) {
await client.sendMessage(msg.to, new MessageMedia(data.data.mimetype, data.data.data, data.data.filename))
}
client.sendMessage(msg.to, data.msg)
} else {
client.sendMessage(msg.to, `πββοΈ *Error*\n\n` + "```" + data.msg + "```")
}
} else if (msg.body.startsWith('!cricket ')) { // Cricket Module Start
msg.delete(true)
var packed = {
url: msg.body.split(' ')[1],
interval: Number(msg.body.split(' ')[2]?.replace('m', '').replace('M', '').replace('.', '')) || 1,
stoptime: Number(msg.body.split(' ')[3]?.replace('m', '').replace('M', '')) || 10,
}
var task = cron.schedule(`*/${packed.interval} * * * *`, async () => {
var fetchscore = await cricket(packed.url)
if (fetchscore.status) {
client.sendMessage(msg.to, fetchscore.msg)
}
})
if (allricketschedules[msg.to] !== undefined) {
let critask = allricketschedules[msg.to];
critask.stop();
client.sendMessage(msg.to, `Previous cricket updates of this chat has been stopped !`)
}
client.sendMessage(msg.to, `β± *Update setted*\n\n_It will now give you cricket update in every ${packed.interval}M and it will stop after ${packed.stoptime}M._`)
setTimeout(() => {
task.stop()
}, packed.stoptime * 60 * 1000);
allricketschedules[msg.to] = task
} else if (msg.body.startsWith('!cricketstop')) { // Cricket Module stop
msg.delete(true)
let critask = allricketschedules[msg.to];
critask.stop();
client.sendMessage(msg.to, `All running cricket updates of this chat has been stopped !`)
}
else if (msg.body.startsWith("!spam ")) { // Spamming Op in the chat
msg.delete(true)
var i, count
if (msg.hasQuotedMsg) {
var quotedMsg = await msg.getQuotedMessage()
count = msg.body.replace("!spam ", "")
if (isNaN(count)) {
client.sendMessage(msg.to, `πββοΈ *Error*\n\n` + "```Invalid count```")
return 0
}
if (count > 0)
count = parseInt(count)
else {
client.sendMessage(msg.to, `πββοΈ *Error*\n\n` + "```Count can't be zero.```")
return 0
}
if (quotedMsg.hasMedia) {
var media = await quotedMsg.downloadMedia();
sticker = false
if (quotedMsg.type == "sticker")
sticker = true
for (i = 0; i < count; i++)
client.sendMessage(msg.to, new MessageMedia(media.mimetype, media.data, media.filename), { sendMediaAsSticker: sticker });
} else {
for (i = 0; i < count; i++)
client.sendMessage(msg.to, quotedMsg.body)
}
} else {
raw_text = msg.body.replace("!spam ", "")
if (raw_text.includes("|")) {
res = raw_text.split("|")
count = res[0]
text = res[1]
} else {
client.sendMessage(msg.to, "```Please read !help spam.```")
return 0
}
if (isNaN(count)) {
client.sendMessage(msg.to, `πββοΈ *Error*\n\n` + "```Invalid count```")
return 0
}
if (count > 0)
count = parseInt(count)
else {
client.sendMessage(msg.to, `πββοΈ *Error*\n\n` + "```Count can't be zero.```")
return 0
}
for (i = 0; i < count; i++)
client.sendMessage(msg.to, text)
}
}
else if (msg.body.startsWith("!crypto ")) {
msg.delete(true)
var data = await crypto.getPrice(msg.body.replace("!crypto ", ""));
if (data == "error") {
client.sendMessage(msg.to, `πββοΈ *Error*\n\n` + "```Something unexpected happened while fetching Cryptocurrency Price```")
}
if (data == "unsupported") {
client.sendMessage(msg.to, `πββοΈ *Error*\n\n` + "```Support for this CryptoCurrency is not yet added```")
}
else {
var date = new Date().toLocaleString('en-US', { timeZone: 'Asia/Kolkata' })
client.sendMessage(msg.to, `Price of *${data.name}* as of ${date} is *βΉ ${data.price}*`);
}
}
else if (msg.body.startsWith("!watch ")) { // Watch Module
msg.delete(true)
var data = await watch.getDetails(msg.body.replace("!watch ", ""));
if (data == "error") {
client.sendMessage(msg.to, `πββοΈ *Error*\n\n` + "```Something Unexpected Happened while fetching Movie/TV Show Details.```")
}
else if (data == "No Results") {
client.sendMessage(msg.to, `πββοΈ *No Results Found!*\n\n` + "```Please check the name of Movie/TV Show you have entered.```")
}
else {
client.sendMessage(msg.to, new MessageMedia(data.mimetype, data.thumbdata, data.filename), { caption: data.caption });
}
}
else if (msg.body.startsWith("!shorten ")) { // URL Shortener Module
msg.delete(true)
var data = await shorten.getShortURL(msg.body.replace("!shorten ", ""));
if (data == "error") {
client.sendMessage(msg.to, `πββοΈ *Error*\n\n` + "```Please make sure the entered URL is in correct format.```")
}
else {
client.sendMessage(msg.to, `Short URL for ${data.input} is π\n${data.short}`)
}
}
else if (msg.body.startsWith("!shorten") && msg.hasQuotedMsg) { // URL Shortener Module Reply
msg.delete(true)
var quotedMsg = await msg.getQuotedMessage();
var data = await shorten.getShortURL(quotedMsg.body);
if (data == "error") {
client.sendMessage(msg.to, `πββοΈ *Error*\n\n` + "```Please make sure the entered URL is in correct format.```")
}
else {
client.sendMessage(msg.to, `Short URL for ${data.input} is π\n${data.short}`)
}
} else if (msg.body.startsWith("!ocr") && msg.hasQuotedMsg) { // OCR Module
msg.delete(true)
var quotedMsg = await msg.getQuotedMessage();
var attachmentData = await quotedMsg.downloadMedia();
var data = await ocr.readImage(attachmentData);
if (data == "error") {
quotedMsg.reply(`Error occured while reading the image. Please make sure the image is clear.`)
} else {
quotedMsg.reply(`*Extracted Text from the Image* π\n\n${data.parsedText}`)
}
} else if (msg.body.startsWith("!emailverifier") && msg.hasQuotedMsg) { // Email Verifier Module Reply
msg.delete(true)
var quotedMsg = await msg.getQuotedMessage();
var getdata = await emailVerifier(quotedMsg.body)
quotedMsg.reply(getdata)
} else if (msg.body.startsWith("!emailverifier ")) { // Email Verifier Module
msg.delete(true)
var getdata = await emailVerifier(msg.body.replace('!emailverifier ', ''))
client.sendMessage(msg.to, getdata);
}
}
});
client.on('message_revoke_everyone', async (after, before) => {
if (before) {
if (before.fromMe !== true && before.hasMedia !== true && before.author == undefined && config.enable_delete_alert == "true") {
client.sendMessage(before.from, "_You deleted this message_ ππ\n\n" + before.body)
}
}
});
client.on('disconnected', (reason) => {
console.log('Client was logged out', reason);
});
app.get('/', (req, res) => {
res.send('<h1>This server is powered by Whatsbot<br><a href="https://github.com/TheWhatsBot/WhatsBot">https://github.com/TheWhatsBot/WhatsBot</a></h1>')
})
app.use('/public', express.static('public'), serveIndex('public', { 'icons': true })) // public directory will be publicly available
app.listen(process.env.PORT || 8080, () => {
console.log(`Server listening at Port: ${process.env.PORT || 8080}`)
})