-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmain.js
463 lines (429 loc) · 19 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
"use strict";
const {
default: makeWASocket,
BufferJSON,
initInMemoryKeyStore,
DisconnectReason,
AnyMessageContent,
useSingleFileAuthState,
makeInMemoryStore,
delay,
downloadContentFromMessage,
jidDecode,
generateForwardMessageContent,
generateWAMessageFromContent,
proto,
prepareWAMessageMedia
} = require("@adiwajshing/baileys")
const figlet = require("figlet");
const fs = require("fs");
const moment = require('moment')
const chalk = require('chalk')
const logg = require('pino')
const clui = require('clui')
const PhoneNumber = require('awesome-phonenumber')
const { Spinner } = clui
const { imageToWebp, videoToWebp, writeExifImg, writeExifVid, TelegraPh } = require('./function/uploader')
const { serialize, fetchJson, getBuffer, makeid, reSize } = require("./function/myfunc");
const { color, mylog, infolog } = require("./function/console");
const time = moment(new Date()).format('HH:mm:ss DD/MM/YYYY');
let welcome = JSON.parse(fs.readFileSync('./database/bot/welcome.json'));
const { botName, sessionName, ownerNomer, ownerName, email, youtube, region, footer } = require("./options/setting");
let session = `./${sessionName}.json`
const { state, saveState } = useSingleFileAuthState(session)
function title() {
console.clear()
console.log(chalk.bold.green(figlet.textSync('Base Bot', {
font: 'Standard',
horizontalLayout: 'default',
verticalLayout: 'default',
width: 80,
whitespaceBreak: false
})))
console.log(chalk.yellow(`${chalk.red('[ Made By RonzzYT ]')}\n\n${chalk.italic.magenta('• Author')} : ${chalk.white('RonzzOfc')}\n${chalk.italic.magenta('• YouTube')} : ${chalk.white('Ronzz YT')}\n${chalk.italic.magenta('• Caption')} : ${chalk.white('Terus Berkarya Hingga Suatu Saat Menjadi Orang Kaya')}\n${chalk.italic.magenta('• Donate')} : ${chalk.white('https://saweria.co/RonzzYT')}\n`))
}
/**
* Uncache if there is file change;
* @param {string} module Module name or path;
* @param {function} cb <optional> ;
*/
function nocache(module, cb = () => { }) {
console.log(`Module ${module} sedang diperhatikan terhadap perubahan`)
fs.watchFile(require.resolve(module), async () => {
await uncache(require.resolve(module))
cb(module)
})
}
/**
* Uncache a module
* @param {string} module Module name or path;
*/
function uncache(module = '.') {
return new Promise((resolve, reject) => {
try {
delete require.cache[require.resolve(module)]
resolve()
} catch (e) {
reject(e)
}
})
}
const getPosiSaying = (from, _db) => {
let posi = null
Object.keys(_db).forEach((i) => {
if (_db[i].jid === from) {
posi = i
}
})
if (posi !== null) {
return posi
}
}
const status = new Spinner(chalk.cyan(` Booting WhatsApp Bot`))
const starting = new Spinner(chalk.cyan(` Preparing After Connect`))
const reconnect = new Spinner(chalk.redBright(` Reconnecting WhatsApp Bot`))
const store = makeInMemoryStore({ logger: logg().child({ level: 'fatal', stream: 'store' }) })
const connectToWhatsApp = async () => {
const ronzz = makeWASocket({
printQRInTerminal: true,
logger: logg({ level: 'fatal' }),
browser: ['Base Bot By Ronzz YT','Safari','1.0.0'],
auth: state
})
title()
store.bind(ronzz.ev)
/* Auto Update */
require('./options/setting')
require('./function/myfunc')
require('./index')
nocache('./options/setting', module => console.log(chalk.greenBright('[ WHATSAPP BOT ] ') + time + chalk.cyanBright(` "${module}" Telah diupdate!`)))
nocache('./function/myfunc', module => console.log(chalk.greenBright('[ WHATSAPP BOT ] ') + time + chalk.cyanBright(` "${module}" Telah diupdate!`)))
nocache('./index', module => console.log(chalk.greenBright('[ WHATSAPP BOT ] ') + time + chalk.cyanBright(` "${module}" Telah diupdate!`)))
ronzz.spam = []
ronzz.public = true
ronzz.ev.on('messages.upsert', async m => {
var msg = m.messages[0]
if (!m.messages) return;
//msg.message = (Object.keys(msg.message)[0] == "ephemeralMessage") ? msg.message.ephemeralMessage.message : msg.message
if (msg.key && msg.key.remoteJid == "status@broadcast") return
msg = serialize(ronzz, msg)
msg.isBaileys = msg.key.id.startsWith('BAE5') || msg.key.id.startsWith('3EB0')
require('./index')(ronzz, msg, m, store, welcome)
})
ronzz.ev.on('connection.update', (update) => {
const { connection, lastDisconnect } = update
if (connection === 'close') {
status.stop()
reconnect.stop()
starting.stop()
console.log(mylog('Server Ready'))
lastDisconnect.error.output.statusCode !== DisconnectReason.loggedOut
? connectToWhatsApp()
: console.log(mylog('Wa web terlogout...'))
}
})
ronzz.getName = (jid, withoutContact = false) => {
var id = ronzz.decodeJid(jid)
withoutContact = ronzz.withoutContact || withoutContact
let v
if (id.endsWith("@g.us")) return new Promise(async (resolve) => {
v = store.contacts[id] || {}
if (!(v.name || v.subject)) v = ronzz.groupMetadata(id) || {}
resolve(v.name || v.subject || PhoneNumber('+' + id.replace('@s.whatsapp.net', '')).getNumber('international'))
})
else v = id === '0@s.whatsapp.net' ? {
id,
name: 'WhatsApp'
} : id === ronzz.decodeJid(ronzz.user.id) ?
ronzz.user :
(store.contacts[id] || {})
return (withoutContact ? '' : v.name) || v.subject || v.verifiedName || PhoneNumber('+' + jid.replace('@s.whatsapp.net', '')).getNumber('international')
}
ronzz.ev.on('group-participants.update', async (update) =>{
const isWelcome = welcome
if(!isWelcome.includes(update.id)) return
if(isWelcome.includes(update.id)) {
const metadata = await ronzz.groupMetadata(update.id)
for (let participant of update.participants) {
try{
let dnew = new Date(new Date + 3600000)
let hari = dnew.toLocaleDateString('id', { weekday: 'long' })
const d = new Date
const tanggal = d.toLocaleDateString('id', {
day: 'numeric',
month: 'long',
year: 'numeric'
})
const jamwib = moment.tz('asia/jakarta').format('HH:mm:ss')
let metadata = await ronzz.groupMetadata(update.id)
let participants = update.participants
for (let num of participants) {
if (update.action == 'demote') {
var button = [{ buttonId: '!text_grup', buttonText: { displayText: 'Kasian😆'}, type: 1 }]
await ronzz.sendMessage(
update.id,
{
text: `*@${num.split("@")[0]} Demote From ${metadata.subject}*`,
buttons: button,
footer: footer,
mentions: [num] })
}
if (update.action == 'promote') {
var button = [{ buttonId: '!text_grup', buttonText: { displayText: 'Selamat🎉'}, type: 1 }]
await ronzz.sendMessage(
update.id,
{
text: `*@${num.split("@")[0]} Promote From ${metadata.subject}*`,
buttons: button,
footer: footer,
mentions: [num] })
}
if (update.action == 'add') {
try {
var ppuser = await ronzz.profilePictureUrl(num, 'image')
} catch {
var ppuser = 'https://telegra.ph/file/265c672094dfa87caea19.jpg'
}
const bio = (await ronzz.fetchStatus(num).catch(console.error) || {}).status || '-'
ronzz.sendMessage(
update.id,
{
image: { url: ppuser },
caption: `*Welcome To ${metadata.subject}*
📛 : _@${num.split("@")[0]}_
🔢 : _${num.split("@")[0]}_
💌 : _${bio ? bio : '-'}_
🏅 : _${metadata.participants.length ? metadata.participants.length : "Undefined"}_
📆 : _${hari}, ${tanggal}_
⏰ : _${jamwib} *WIB*_
📄 *Deskripsi :*
${metadata.desc ? metadata.desc : 'Tidak ada deskripsi'}`,
buttons: [{ buttonId: '!text_grup', buttonText: { displayText: 'Welcome👋'}, type: 1 }],
footer: footer,
mentions: [num] })
}
if (update.action == 'remove'){
try {
var ppuser = await ronzz.profilePictureUrl(num, 'image')
} catch {
var ppuser = 'https://telegra.ph/file/265c672094dfa87caea19.jpg'
}
const bio = (await ronzz.fetchStatus(num).catch(console.error) || {}).status || '-'
ronzz.sendMessage(
update.id,
{
image: { url: ppuser },
caption: `*Leave From Grup ${metadata.subject}*
📛 : _@${num.split("@")[0]}_
🔢 : _${num.split("@")[0]}_
💌 : _${bio ? bio : '-'}_
🏅 : _${metadata.participants.length ? metadata.participants.length : "Undefined"}_
📆 : _${hari}, ${tanggal}_
⏰ : _${jamwib} *WIB*_
*┗━━ ❑ GoodBye👋*`,
buttons: [{ buttonId: '!text_grup', buttonText: { displayText: 'GoodBye👋'}, type: 1 }],
footer: footer,
mentions: [num] })
}
}
} catch (err) {
console.log(err)
}
}
console.log(update)
}
})
ronzz.sendImage = async (jid, path, caption = '', quoted = '', options) => {
let buffer = Buffer.isBuffer(path) ? path : /^data:.*?\/.*?;base64,/i.test(path) ? Buffer.from(path.split`,`[1], 'base64') : /^https?:\/\//.test(path) ? await (await getBuffer(path)) : fs.existsSync(path) ? fs.readFileSync(path) : Buffer.alloc(0)
return await ronzz.sendMessage(jid, { image: buffer, caption: caption, ...options }, { quoted })
}
ronzz.sendButton = async (jid, text, buttons = [], footer, quoted = '', options = {}) => {
let buttonMessage = {
text,
footer,
buttons,
headerType: 2,
...options
}
return ronzz.sendMessage(jid, buttonMessage, { quoted, ...options })
}
ronzz.ev.on('group-update', async (anu) => {
updateGroup(ronzz, anu, MessageType)
})
ronzz.ev.on('creds.update', () => saveState)
ronzz.reply = (from, content, msg) => ronzz.sendMessage(from, { text: content }, { quoted: msg })
ronzz.ws.on('CB:call', async (json) => {
const callerId = json.content[0].attrs['call-creator']
if (json.content[0].tag == 'offer') {
ronzz.sendMessage(callerId, { text: `Kamu telah di blok oleh bot, karena kamu menelpon bot!!\n\nJika tidak sengaja silahkan hubungi owner agar dibuka blocknya!!\nNomor owner : https://wa.me/${ownerNomer}` })
setTimeout(() => {
ronzz.updateBlockStatus(callerId, 'block')
},1000)
}
})
ronzz.decodeJid = (jid) => {
if (!jid) return jid
if (/:\d+@/gi.test(jid)) {
let decode = jidDecode(jid) || {}
return decode.user && decode.server && decode.user + '@' + decode.server || jid
} else return jid
}
ronzz.ev.on('contacts.update', update => {
for (let contact of update) {
let id = ronzz.decodeJid(contact.id)
if (store && store.contacts) store.contacts[id] = { id, name: contact.notify }
}
})
ronzz.setStatus = (status) => {
ronzz.query({
tag: 'iq',
attrs: {
to: '@s.whatsapp.net',
type: 'set',
xmlns: 'status',
},
content: [{
tag: 'status',
attrs: {},
content: Buffer.from(status, 'utf-8')
}]
})
return status
}
ronzz.sendContact = async (jid, kon, quoted = '', opts = {}) => {
let list = []
for (let i of kon) {
list.push({
lisplayName: `${ownerNomer.includes(i) ? ownerName : await ronzz.getName(i + '@s.whatsapp.net')}`,
vcard: `BEGIN:VCARD\nVERSION:3.0\nN:${ownerNomer.includes(i) ? ownerName : await ronzz.getName(i + '@s.whatsapp.net')}\nFN:${ownerNomer.includes(i) ? ownerName : await ronzz.getName(i + '@s.whatsapp.net')}\nitem1.TEL;waid=${i}:${i}\nitem1.X-ABLabel:Ponsel\nitem2.EMAIL;type=INTERNET:${email}\nitem2.X-ABLabel:Email\nitem3.URL:${youtube}\nitem3.X-ABLabel:YouTube\nitem4.ADR:;;;;;${region}\nitem4.X-ABLabel:Region\nEND:VCARD`
})
}
list.push({
lisplayName: "Ronzz YT",
vcard: "BEGIN:VCARD\nVERSION:3.0\nN:Ronzz YT\nFN:Ronzz YT\nitem1.TEL;waid=16784037437:+1 (678) 403 7437\nitem1.X-ABLabel:Ponsel\nitem2.EMAIL;type=INTERNET:ronzzyt8598@gmail.com\nitem2.X-ABLabel:Email\nitem3.URL:https://youtube.com/c/RonzzYT\nitem3.X-ABLabel:YouTube\nitem4.ADR:;;;;;Indonesia\nitem4.X-ABLabel:Region\nEND:VCARD"
})
return ronzz.sendMessage(jid, { contacts: { displayName: `${list.length} Kontak`, contacts: list }, ...opts }, { quoted })
}
ronzz.sendTextMentions = async (jid, teks, mention, quoted = '') => {
return ronzz.sendMessage(jid, { text: teks, mentions: mention }, { quoted })
}
ronzz.copyNForward = async (jid, message, forceForward = false, options = {}) => {
let vtype
if (options.readViewOnce) {
message.message = message.message && message.message.ephemeralMessage && message.message.ephemeralMessage.message ? message.message.ephemeralMessage.message : (message.message || undefined)
vtype = Object.keys(message.message.viewOnceMessage.message)[0]
delete(message.message && message.message.ignore ? message.message.ignore : (message.message || undefined))
delete message.message.viewOnceMessage.message[vtype].viewOnce
message.message = {
...message.message.viewOnceMessage.message
}
}
let mtype = Object.keys(message.message)[0]
let content = await generateForwardMessageContent(message, forceForward)
let ctype = Object.keys(content)[0]
let context = {}
if (mtype != "conversation") context = message.message[mtype].contextInfo
content[ctype].contextInfo = {
...context,
...content[ctype].contextInfo
}
const waMessage = await generateWAMessageFromContent(jid, content, options ? {
...content[ctype],
...options,
...(options.contextInfo ? {
contextInfo: {
...content[ctype].contextInfo,
...options.contextInfo
}
} : {})
} : {})
await ronzz.relayMessage(jid, waMessage.message, { messageId: waMessage.key.id })
return waMessage
}
ronzz.sendMessageFromContent = async(jid, message, options = {}) => {
var option = { contextInfo: {}, ...options }
var prepare = await generateWAMessageFromContent(jid, message, option)
await ronzz.relayMessage(jid, prepare.message, { messageId: prepare.key.id })
return prepare
}
ronzz.downloadAndSaveMediaMessage = async(msg, type_file, path_file) => {
if (type_file === 'image') {
var stream = await downloadContentFromMessage(msg.message.imageMessage || msg.message.extendedTextMessage.contextInfo.quotedMessage.imageMessage, 'image')
let buffer = Buffer.from([])
for await(const chunk of stream) {
buffer = Buffer.concat([buffer, chunk])
}
fs.writeFileSync(path_file, buffer)
return path_file
} else if (type_file === 'video') {
var stream = await downloadContentFromMessage(msg.message.videoMessage || msg.message.extendedTextMessage.contextInfo.quotedMessage.videoMessage, 'video')
let buffer = Buffer.from([])
for await(const chunk of stream) {
buffer = Buffer.concat([buffer, chunk])
}
fs.writeFileSync(path_file, buffer)
return path_file
} else if (type_file === 'sticker') {
var stream = await downloadContentFromMessage(msg.message.stickerMessage || msg.message.extendedTextMessage.contextInfo.quotedMessage.stickerMessage, 'sticker')
let buffer = Buffer.from([])
for await(const chunk of stream) {
buffer = Buffer.concat([buffer, chunk])
}
fs.writeFileSync(path_file, buffer)
return path_file
} else if (type_file === 'audio') {
var stream = await downloadContentFromMessage(msg.message.audioMessage || msg.message.extendedTextMessage.contextInfo.quotedMessage.audioMessage, 'audio')
let buffer = Buffer.from([])
for await(const chunk of stream) {
buffer = Buffer.concat([buffer, chunk])
}
fs.writeFileSync(path_file, buffer)
return path_file
}
}
/**
* @param {*} jid
* @param {*} path
* @param {*} quoted
* @param {*} options
* @returns
*/
ronzz.sendImageAsSticker = async (jid, path, quoted, options = {}) => {
let buff = Buffer.isBuffer(path) ? path : /^data:.*?\/.*?;base64,/i.test(path) ? Buffer.from(path.split`,`[1], 'base64') : /^https?:\/\//.test(path) ? await (await getBuffer(path)) : fs.existsSync(path) ? fs.readFileSync(path) : Buffer.alloc(0)
let buffer
if (options && (options.packname || options.author)) {
buffer = await writeExifImg(buff, options)
} else {
buffer = await imageToWebp(buff)
}
await ronzz.sendMessage(jid, { sticker: { url: buffer }, ...options }, { quoted })
.then( response => {
fs.unlinkSync(buffer)
return response
})
}
/**
* @param {*} jid
* @param {*} path
* @param {*} quoted
* @param {*} options
* @returns
*/
ronzz.sendVideoAsSticker = async (jid, path, quoted, options = {}) => {
let buff = Buffer.isBuffer(path) ? path : /^data:.*?\/.*?;base64,/i.test(path) ? Buffer.from(path.split`,`[1], 'base64') : /^https?:\/\//.test(path) ? await (await getBuffer(path)) : fs.existsSync(path) ? fs.readFileSync(path) : Buffer.alloc(0)
let buffer
if (options && (options.packname || options.author)) {
buffer = await writeExifVid(buff, options)
} else {
buffer = await videoToWebp(buff)
}
await ronzz.sendMessage(jid, { sticker: { url: buffer }, ...options }, { quoted })
.then( response => {
fs.unlinkSync(buffer)
return response
})
}
return ronzz
}
connectToWhatsApp()
.catch(err => console.log(err))