-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
178 lines (167 loc) · 4.95 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
// TODO: reduce global variabel usage
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
process.on('uncaughtException', console.error)
import './config.js'
import {
spawn
} from 'child_process'
import {
protoType,
serialize
} from './lib/simple.js'
import {
plugins,
filesInit,
reload,
pluginFolder,
pluginFilter
} from './lib/plugins.js'
import Connection from './lib/connection.js'
import Helper from './lib/helper.js'
import db, {
loadDatabase
} from './lib/database.js'
import clearTmp from './lib/clearTmp.js';
import axios from 'axios';
import former from 'form-data';
import cheri from 'cheerio';
import fetch from 'node-fetch';
import cron from 'node-cron';
import fs from 'fs';
global.fetch = fetch
global.fs = fs
global.axios = axios
global.former = former
global.cheerio = cheri
global.db = db
global.loadDatabase = await loadDatabase()
global.delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
global.plugins = plugins
var PORT = process.env.PORT || process.env.SERVER_PORT || 3000
protoType()
serialize()
if (db.data == null) loadDatabase
Object.assign(global, Helper)
// global.Fn = function functionCallBack(fn, ...args) { return fn.call(Connection.conn, ...args) }
global.timestamp = {
start: new Date
}
// global.opts['db'] = process.env['db']
global.conn = Object.defineProperty(Connection, 'conn', {
value: await Connection.conn,
enumerable: true,
configurable: true,
writable: true
}).conn
global.store = Connection.store
// load plugins
filesInit(pluginFolder, pluginFilter, conn).then(_ => console.log(Object.keys(plugins))).catch(console.error)
Object.freeze(reload)
if (!opts['test']) {
setInterval(async () => {
await Promise.allSettled([
db.data ? db.write() : Promise.reject('db.data is null'),
(opts['autocleartmp'] || opts['cleartmp']) ? clearTmp() : Promise.resolve()
])
Connection.store.writeToFile(Connection.storeFile)
}, 60 * 1000)
}
if (opts['server'])(await import('./server.js')).default(conn, PORT)
// Quick Test
async function _quickTest() {
var 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))
})
])
}))
var [ffmpeg, ffprobe, ffmpegWebp, convert, magick, gm, find] = test
console.log(test)
var s = global.support = {
ffmpeg,
ffprobe,
ffmpegWebp,
convert,
magick,
gm,
find
}
// require('./lib/sticker').support = s
Object.freeze(global.support)
if (!s.ffmpeg)(conn?.logger || console).warn('Please install ffmpeg for sending videos (pkg install ffmpeg)')
if (s.ffmpeg && !s.ffmpegWebp)(conn?.logger || console).warn('Stickers may not animated without libwebp on ffmpeg (--enable-ibwebp while compiling ffmpeg)')
if (!s.convert && !s.magick && !s.gm)(conn?.logger || console).warn('Stickers may not work without imagemagick if libwebp on ffmpeg doesnt isntalled (pkg install imagemagick)')
}
async function expired() {
return new Promise(async (resolve, reject) => {
var user = Object.keys(db.data.users)
var chat = Object.keys(db.data.chats)
for (var jid of user) {
var users = db.data.users[jid]
var {
name,
premium,
expired
} = users
if (users.premium) {
if (Date.now() >= users.expired) {
users.premium = false
users.expired = 0
users.limitjoinprem = 0
users.limitjoinfree = 0
conn.reply(jid, 'hai\nmasa premium kamu sekarang sudah habis.\njika ingin memperpanjang lagi silahkan chat owner.\nterima kasih telah menggunakan bot :)\n' + `owner: @${nomorown}`, null, {
mentions: [nomorown + '@s.whatsapp.net']
})
resolve(console.log(`masa premium ${name} sudah habis`))
}
}
if (users.sewa) {
if (users.limitjoinprem == 0) {
users.sewa = false
users.limitjoinfree = 1
}
}
}
for (var id of chat) {
if (id.endsWith('g.us')) {
var chats = db.data.chats[id]
if (chats.grouprental) {
if (Date.now() >= chats.expired) {
chats.grouprental = false
chats.expired = 0
await conn.reply(id, `masa menetap di ${await conn.getName(id)} sudah habis.\nBot sebentar lagi akan keluar.\nterima kasih telah menggunakan bot kami.\nChat owner kami jika ingin menyewa lagi :)\nowner: @${nomorown}`, null, {
mentions: [nomorown + '@s.whatsapp.net']
})
await delay(3000)
await conn.groupLeave(id)
resolve(console.log(`masa menetap di ${await conn.getName(id)} sudah habis`))
}
}
}
}
})
}
setInterval(async () => {
var b = await expired()
}, 1000)
setInterval(async () => {
var a = await clearTmp()
console.log(`successfully clear tmp`)
}, 180000)
_quickTest()
.then(() => (conn?.logger.info || console.log)('Quick Test Done'))
.catch(console.error)