-
-
Notifications
You must be signed in to change notification settings - Fork 79
/
server.js
133 lines (122 loc) · 4.16 KB
/
server.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
/**
* Author : Gimenz
* Name : nganu
* Version : 1.0
* Update : 09 Januari 2022
*
* If you are a reliable programmer or the best developer, please don't change anything.
* If you want to be appreciated by others, then don't change anything in this script.
* Please respect me for making this tool from the beginning.
*/
let express = require('express')
const http = require('http')
const app = express()
const fs = require('fs');
const httpServer = http.createServer(app)
const { color, humanFileSize } = require('./utils/index')
const si = require('systeminformation')
const io = require('socket.io')(httpServer)
const qrcode = require('qrcode')
const { resizeImage } = require('./lib/converter')
const { isJidUser, isJidGroup } = require('@adiwajshing/baileys')
global.qr = '';
let config = require('./src/config.json');
const { configHandler } = require('./db');
app.set('json spaces', 2);
app.use(express.json());
app.get('/', async (req, res) => {
try {
let ram = await si.mem()
let cpu = await si.cpuCurrentSpeed()
let disk = await si.fsSize()
let up = si.time()
let json = {
server_time: new Date(up.current).toLocaleString('jv'),
uptime: times(up.uptime),
memory: humanFileSize(ram.free, true, 1) + ' free of ' + humanFileSize(ram.total, true, 1),
memory_used: humanFileSize(ram.used, true, 1),
cpu: cpu.avg + ' Ghz',
disk: humanFileSize(disk[0].available, true, 1) + ' free of ' + humanFileSize(disk[0].size, true, 1),
chats: {
total: store.chats.length,
private: store.chats.filter(x => isJidUser(x.id)).length,
groups: store.chats.filter(x => isJidGroup(x.id)).length
}
}
res.status(200).json(json)
} catch (error) {
res.status(503).send(error)
}
})
app.get('/qr', async (req, res, next) => {
try {
res.setHeader("content-type", "image/png")
res.send(await resizeImage(await qrcode.toBuffer(global.qr), 512, 512))
} catch (error) {
res.send('err, ' + error.message)
}
})
app.get('/send', async (req, res, next) => {
const { id, text } = req.query;
if (!id) return res.status(403).json({
status: false,
code: 403,
creator: '@gimenz.id',
result: 'jid diperlukan'
})
if (!text) return res.status(403).json({
status: false,
code: 403,
creator: '@gimenz.id',
result: 'text diperlukan'
})
const data = await client.sendMessage(id, { text })
res.status(200).jsonp(data)
console.log(color(`[SEND] send message to ${id}`, 'green') + color(`${PORT}`, 'yellow'))
})
app.get('/user', async (req, res, next) => {
try {
const user = await client.user
res.status(200).jsonp(user)
} catch (error) {
res.status(503).jsonp({
'success': false,
'message': 'bot not authenticated'
})
}
})
app.get('/set', async (req, res, next) => {
const acceptable = ['session_id', 'removeBG', 'musixMatch']
console.log(req.query);
const mode = Object.keys(req.query)[0]
let value = Object.values(req.query)[0]
if (!acceptable.includes(mode)) return res.status(401).jsonp({
status: false,
'message': 'mode not acceptable! => only ' + acceptable.join(', ')
})
if (mode == 'session_id') value = encodeURIComponent(value);
config[mode] = value
configHandler.update(mode, value)
res.send(value)
})
const qrPrint = (qr) => {
app.get('/qr', async (req, res) => {
res.setHeader("content-type", "image/png")
res.send(await resizeImage(await qrcode.toBuffer(qr), 512, 512))
})
}
// Run the server
const PORT = config.PORT || 3000
httpServer.listen(PORT, () => {
console.log(color('[INFO] Web api Server on port: ', 'green') + color(`${PORT}`, 'yellow'))
})
function times(second) {
days = Math.floor((second / 60) / 60 / 24)
hours = Math.floor((second / 60) / 60)
minute = Math.floor(second / 60)
sec = Math.floor(second)
return days + ' days, ' + hours + ' hours, ' + minute + ' minutes, ' + sec + ' seconds'
}
module.exports = {
qrPrint
}