forked from zeusdohype/zeus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
456 lines (430 loc) · 16 KB
/
Copy pathmain.js
File metadata and controls
456 lines (430 loc) · 16 KB
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
let { WAConnection: _WAConnection, WA_MESSAGE_STUB_TYPES } = require('@adiwajshing/baileys')
let { generate } = require('qrcode-terminal')
let qrcode = require('qrcode')
let simple = require('./lib/simple')
let logs = require('./lib/logs')
let yargs = require('yargs/yargs')
let syntaxerror = require('syntax-error')
let fetch = require('node-fetch')
let chalk = require('chalk')
let fs = require('fs')
let path = require('path')
let util = require('util')
let { spawnSync } = require('child_process')
let WAConnection = simple.WAConnection(_WAConnection)
global.owner = ['6283856085455@s.whatsapp.net'] // Put your number here
global.mods = ['6283856085455@s.whatsapp.net'] // Want some help?
global.prems = ['6283856085455@s.whatsapp.net'] // Premium user has unlimited limit
global.APIs = { // API Prefix
// name: 'https://website'
nrtm: 'https://nurutomo.herokuapp.com',
xteam: 'https://api.xteam.xyz'
}
global.APIKeys = { // APIKey Here
// 'https://website': 'apikey'
'https://api.xteam.xyz': 'test'
}
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
}
global.LOGGER = logs()
const PORT = process.env.PORT || 3000
let opts = yargs(process.argv.slice(2)).exitProcess(false).parse()
global.opts = Object.freeze({...opts})
global.prefix = new RegExp('^[' + (opts['prefix'] || 'xzXZ\\/i!#$%\\-+£¢€¥^°=¶∆×÷π√✓©®:;?&.') + ']')
global.DATABASE = new (require('./lib/database'))(`${opts._[0] ? opts._[0] + '_' : ''}database.json`, null, 2)
if (!global.DATABASE.data.users) global.DATABASE.data = {
users: {},
groups: {},
chats: {},
stats: {},
}
if (!global.DATABASE.data.groups) global.DATABASE.data.groups = {}
if (!global.DATABASE.data.chats) global.DATABASE.data.chats = {}
if (!global.DATABASE.data.stats) global.DATABASE.data.stats = {}
if (opts['server']) {
let express = require('express')
global.app = express()
app.all('*', async (req, res) => {
await global.conn.connect().catch(console.log)
res.end(await qrcode.toBuffer(global.qr))
})
app.listen(PORT, () => console.log('App listened on port', PORT))
}
global.conn = new WAConnection()
let authFile = `${opts._[0] || 'session'}.data.json`
if (fs.existsSync(authFile)) conn.loadAuthInfo(authFile)
if (opts['big-qr'] || opts['server']) conn.on('qr', qr => generate(qr, { small: false }))
if (opts['server']) conn.on('qr', qr => { global.qr = qr })
conn.on('credentials-updated', () => fs.writeFileSync(authFile, JSON.stringify(conn.base64EncodedAuthInfo())))
let lastJSON = JSON.stringify(global.DATABASE.data)
setInterval(async () => {
conn.logger.info('Saving database . . .')
if (JSON.stringify(global.DATABASE.data) == lastJSON) conn.logger.info('Database is up to date')
else {
global.DATABASE.save()
conn.logger.info('Done saving database!')
lastJSON = JSON.stringify(global.DATABASE.data)
}
}, 60 * 1000) // Save every minute
const isNumber = x => typeof x === 'number' && !isNaN(x)
conn.handler = async function (m) {
try {
simple.smsg(this, m)
m.exp = 0
m.limit = false
try {
let user
if (user = global.DATABASE._data.users[m.sender]) {
if (!isNumber(user.exp)) user.exp = 0
if (!isNumber(user.limit)) user.limit = 10
if (!isNumber(user.lastclaim)) user.lastclaim = 0
} else global.DATABASE._data.users[m.sender] = {
exp: 0,
limit: 10,
lastclaim: 0,
}
let chat
if (chat = global.DATABASE._data.chats[m.chat]) {
if (!'isBanned' in chat) chat.isBanned = false
if (!'welcome' in chat) chat.welcome = false
if (!'sWelcome' in chat) chat.sWelcome = ''
if (!'sBye' in chat) chat.sBye = ''
if (!'delete' in chat) chat.delete = true
} else global.DATABASE._data.chats[m.chat] = {
isBanned: false,
welcome: false,
sWelcome: '',
sBye: '',
delete: true
}
} catch (e) {
console.log(e, global.DATABASE.data)
}
if (!m.fromMe && opts['self']) return
if (!m.text) return
if (m.isBaileys) return
m.exp += 1
let usedPrefix
for (let name in global.plugins) {
let plugin = global.plugins[name]
if (!plugin) continue
if (plugin.tags && plugin.tags.includes('admin')) continue
let _prefix = plugin.customPrefix ? plugin.customPrefix : conn.prefix ? conn.prefix : global.prefix
if ((usedPrefix = (_prefix.exec(m.text) || '')[0])) {
let noPrefix = m.text.replace(usedPrefix, '')
let [command, ...args] = noPrefix.trim().split` `.filter(v=>v)
args = args || []
let _args = noPrefix.trim().split` `.slice(1)
let text = _args.join` `
command = (command || '').toLowerCase()
let isROwner = [global.conn.user.jid, ...global.owner].map(v => v.replace(/[^0-9]/g, '') + '@s.whatsapp.net').includes(m.sender)
let isOwner = isROwner || m.fromMe
let isAccept = plugin.command instanceof RegExp ? // RegExp Mode?
plugin.command.test(command) :
Array.isArray(plugin.command) ? // Array?
plugin.command.some(cmd => cmd instanceof RegExp ? // RegExp in Array?
cmd.test(command) :
cmd === command
) :
typeof plugin.command === 'string' ? // String?
plugin.command === command :
false
if (!isAccept) continue
m.plugin = name
let isMods = isOwner || global.mods.map(v => v.replace(/[^0-9]/g, '') + '@s.whatsapp.net').includes(m.sender)
let isPrems = isROwner || global.prems.map(v => v.replace(/[^0-9]/g, '') + '@s.whatsapp.net').includes(m.sender)
let groupMetadata = m.isGroup ? await this.groupMetadata(m.chat) : {}
let participants = m.isGroup ? groupMetadata.participants : []
let user = m.isGroup ? participants.find(u => u.jid == m.sender) : {} // User Data
let bot = m.isGroup ? participants.find(u => u.jid == this.user.jid) : {} // Your Data
let isAdmin = user.isAdmin || user.isSuperAdmin || false // Is User Admin?
let isBotAdmin = bot.isAdmin || bot.isSuperAdmin || false // Are you Admin?
if (m.chat in global.DATABASE._data.chats) {
let chat = global.DATABASE._data.chats[m.chat]
if (name != 'unbanchat.js' && chat && chat.isBanned) return // Except this
}
if (plugin.before && plugin.before({
usedPrefix
})) return
let fail = plugin.fail || global.dfail // When failed
if (plugin.rowner && !isROwner) { // Real Owner
fail('rowner', m, this)
continue
}
if (plugin.owner && !isOwner) { // Number Owner
fail('owner', m, this)
continue
}
if (plugin.mods && !isMods) { // Moderator
fail('mods', m, this)
continue
}
if (plugin.premium && !isPrems) { // Premium
fail('premium', m, this)
continue
}
if (plugin.group && !m.isGroup) { // Group Only
fail('group', m, this)
continue
} else if (plugin.botAdmin && !isBotAdmin) { // You Admin
fail('botAdmin', m, this)
continue
} else if (plugin.admin && !isAdmin) { // User Admin
fail('admin', m, this)
continue
}
if (plugin.private && m.isGroup) { // Private Chat Only
fail('private', m, this)
continue
}
m.isCommand = true
let xp = 'exp' in plugin ? parseInt(plugin.exp) : 9 // XP Earning per command
if (xp > 99) m.reply('Ngecit -_-') // Hehehe
else m.exp += xp
if (!isPrems && global.DATABASE._data.users[m.sender].limit < m.limit * 1 && plugin.limit) {
this.reply(m.chat, `Limit anda habis, silahkan beli melalui *${usedPrefix}buy*`, m)
continue // Limit habis
}
try {
await plugin.call(this, m, {
usedPrefix,
noPrefix,
_args,
args,
command,
text,
conn: this,
participants,
groupMetadata,
isROwner,
isOwner,
isAdmin,
isBotAdmin,
isPrems
})
if (!isPrems) m.limit = m.limit || plugin.limit || false
} catch (e) {
// Error occured
m.error = e
console.log(e)
m.reply(util.format(e))
} finally {
if (m.limit) m.reply(+ m.limit + ' Limit terpakai')
}
break
}
}
} finally {
//console.log(global.DATABASE._data.users[m.sender])
let user, stats = global.DATABASE._data.stats
if (m) {
if (m.sender && (user = global.DATABASE._data.users[m.sender])) {
user.exp += m.exp
user.limit -= m.limit * 1
}
let stat
if (m.plugin) {
let now = + new Date
if (m.plugin in stats) {
stat = stats[m.plugin]
if (!isNumber(stat.total)) stat.total = 1
if (!isNumber(stat.success)) stat.success = m.error ? 0 : 1
if (!isNumber(stat.last)) stat.last = now
if (!isNumber(stat.lastSuccess)) stat.lastSuccess = m.error ? 0 : now
} else stat = stats[m.plugin] = {
total: 1,
success: m.error ? 0 : 1,
last: now,
lastSuccess: m.error ? 0 : now
}
stat.total += 1
stat.last = now
if (!m.error) {
stat.success += 1
stat.lastSuccess = now
}
}
}
try {
require('./lib/print')(m, this)
} catch (e) {
console.log(m, m.quoted, e)
}
}
}
conn.welcome = 'Hai, @user!\nSelamat datang di grup @subject'
conn.bye = 'Selamat tinggal @user!'
conn.onAdd = async function ({ m, participants }) {
let chat = global.DATABASE._data.chats[m.key.remoteJid]
if (!chat.welcome) return
for (let user of participants) {
let pp = './src/avatar_contact.png'
try {
pp = await this.getProfilePicture(user)
} catch (e) {
} finally {
let text = (chat.sWelcome || this.welcome || conn.welcome || 'Welcome, @user!').replace('@user', '@' + user.split('@')[0]).replace('@subject', this.getName(m.key.remoteJid))
this.sendFile(m.key.remoteJid, pp, 'pp.jpg', text, m, false, {
contextInfo: {
mentionedJid: [user]
}
})
}
}
}
conn.onLeave = async function ({ m, participants }) {
let chat = global.DATABASE._data.chats[m.key.remoteJid]
if (!chat.welcome) return
for (let user of participants) {
if (this.user.jid == user) continue
let pp = './src/avatar_contact.png'
try {
pp = await this.getProfilePicture(user)
} catch (e) {
} finally {
let text = (chat.sBye || this.bye || conn.bye || 'Bye, @user!').replace('@user', '@' + user.split('@')[0])
this.sendFile(m.key.remoteJid, pp, 'pp.jpg', text, m, false, {
contextInfo: {
mentionedJid: [user]
}
})
}
}
}
conn.onDelete = async function (m) {
if (m.key.fromMe) return
let chat = global.DATABASE._data.chats[m.key.remoteJid]
if (chat.delete) return
await this.reply(m.key.remoteJid, `
Terdeteksi @${m.participant.split`@`[0]} telah menghapus pesan
Untuk mematikan fitur ini, ketik
*.enable delete*
`.trim(), m.message, {
contextInfo: {
mentionedJid: [m.participant]
}
})
this.copyNForward(m.key.remoteJid, m.message).catch(e => console.log(e, m))
}
conn.on('message-new', conn.handler)
conn.on('message-delete', conn.onDelete)
conn.on('group-add', conn.onAdd)
conn.on('group-leave', conn.onLeave)
conn.on('error', conn.logger.error)
conn.on('close', () => {
setTimeout(async () => {
try {
if (conn.state === 'close') {
await conn.loadAuthInfo(authFile)
await conn.connect()
global.timestamp.connect = new Date
}
} catch (e) {
conn.logger.error(e)
}
}, 5000)
})
global.dfail = (type, m, conn) => {
let msg = {
rowner: 'Perintag ini hanya dapat digunakan oleh _*OWWNER!1!1!*_',
owner: 'Perintah ini hanya dapat digunakan oleh _*Owner Bot*_!',
mods: 'Perintah ini hanya dapat digunakan oleh _*Moderator*_ !',
premium: 'Perintah ini hanya untuk member _*Premium*_ !',
group: 'Perintah ini hanya dapat digunakan di grup!',
private: 'Perintah ini hanya dapat digunakan di Chat Pribadi!',
admin: 'Perintah ini hanya untuk *Admin* grup!',
botAdmin: 'Jadikan bot sebagai *Admin* untuk menggunakan perintah ini!'
}[type]
if (msg) conn.reply(m.chat, msg, m)
}
if (opts['test']) {
conn.user = {
jid: '2219191@s.whatsapp.net',
name: 'test',
phone: {}
}
conn.prepareMessageMedia = (buffer, mediaType, options = {}) => {
return {
[mediaType]: {
url: '',
mediaKey: '',
mimetype: options.mimetype,
fileEncSha256: '',
fileSha256: '',
fileLength: buffer.length,
seconds: options.duration,
fileName: options.filename || 'file',
gifPlayback: options.mimetype == 'image/gif' || undefined,
caption: options.caption,
ptt: options.ptt
}
}
}
conn.sendMessage = async (chatId, content, type, opts = {}) => {
let message = await conn.prepareMessageContent(content, type, opts)
let waMessage = conn.prepareMessageFromContent(chatId, message, opts)
if (type == 'conversation') waMessage.key.id = require('crypto').randomBytes(16).toString('hex').toUpperCase()
conn.emit('message-new', waMessage)
}
process.stdin.on('data', chunk => conn.sendMessage('123@s.whatsapp.net', chunk.toString().trimEnd(), 'conversation'))
} else {
process.stdin.on('data', chunk => {
global.DATABASE.save()
process.send(chunk.toString().trimEnd())
})
conn.connect().then(() => {
global.timestamp.connect = new Date
})
}
process.on('uncaughtException', console.error)
// let strQuot = /(["'])(?:(?=(\\?))\2.)*?\1/
let pluginFolder = path.join(__dirname, 'plugins')
let pluginFilter = filename => /\.js$/.test(filename)
global.plugins = {}
for (let filename of fs.readdirSync(pluginFolder).filter(pluginFilter)) {
try {
global.plugins[filename] = require(path.join(pluginFolder, filename))
} catch (e) {
conn.logger.error(e)
delete global.plugins[filename]
}
}
console.log(Object.keys(global.plugins))
global.reload = (event, filename) => {
if (pluginFilter(filename)) {
let dir = path.join(pluginFolder, filename)
if (dir in require.cache) {
delete require.cache[dir]
if (fs.existsSync(dir)) conn.logger.info(`re - require plugin '${filename}'`)
else {
conn.logger.warn(`deleted plugin '${filename}'`)
return delete global.plugins[filename]
}
} else conn.logger.info(`requiring new plugin '${filename}'`)
let err = syntaxerror(fs.readFileSync(dir), filename)
if (err) conn.logger.error(`syntax error while loading '${filename}'\n${err}`)
else try {
global.plugins[filename] = require(dir)
} catch (e) {
conn.logger.error(e)
}
}
}
Object.freeze(global.reload)
fs.watch(path.join(__dirname, 'plugins'), global.reload)
process.on('exit', () => global.DATABASE.save())
// Quick Test
let ffmpeg = spawnSync('ffmpeg')
let ffmpegWebp = spawnSync('ffmpeg', ['-hide_banner', '-loglevel', 'error', '-filter_complex', 'color', '-frames:v', '1', '-f', 'webp', '-'])
let convert = spawnSync('convert')
global.support = {
ffmpeg: ffmpeg.status,
ffmpegWebp: ffmpeg.status && ffmpegWebp.stderr.length == 0 && ffmpegWebp.stdout.length > 0,
convert: convert.status
}
Object.freeze(global.support)
if (!global.support.ffmpeg) conn.logger.warn('Please install ffmpeg for sending videos (pkg install ffmpeg)')
if (!global.support.ffmpegWebp) conn.logger.warn('Stickers may not animated without libwebp on ffmpeg (--emable-ibwebp while compiling ffmpeg)')
if (!global.support.convert) conn.logger.warn('Stickers may not work without imagemagick if libwebp on ffmpeg doesnt isntalled (pkg install imagemagick)')