diff --git a/package.json b/package.json index c86fa9c..7c1e34e 100644 --- a/package.json +++ b/package.json @@ -47,12 +47,16 @@ "chalk": "^4.1.0", "child_process": "^1.0.2", "discord.js": "^12.5.1", + "ejs": "^3.1.5", + "express": "^4.17.1", "fluent-ffmpeg": "^2.1.2", "moment-timezone": "^0.5.32", "node-fetch": "^2.6.1", + "node-os-utils": "^1.3.2", "pg": "^8.5.1", "release-it": "^14.2.2", "remove.bg": "^1.3.0", + "socket.io": "^3.1.0", "source-map-loader": "^2.0.0", "spinnies": "^0.5.1", "typescript": "^4.1.3", diff --git a/src/command/id.ts b/src/command/id.ts index d63cd0c..ce06979 100644 --- a/src/command/id.ts +++ b/src/command/id.ts @@ -5,10 +5,10 @@ module.exports = { execute (client, chat, pesan) { const uid = client.sender if (client.isGroup) { - const gid = client.groupId - client.reply(`*ID* kamu : ${uid}\nGroup *ID* : ${gid}`) + const gid = client.groupId + client.reply(`*ID* kamu : ${uid}\nGroup *ID* : ${gid}`) } else { - client.reply(`*ID* kamu : ${uid}`) + client.reply(`*ID* kamu : ${uid}`) } } } diff --git a/src/krypton.ts b/src/krypton.ts index 6bdfbad..7368b4b 100644 --- a/src/krypton.ts +++ b/src/krypton.ts @@ -1,3 +1,4 @@ +export {} const { WAConnection, MessageType } = require('@adiwajshing/baileys') @@ -11,8 +12,14 @@ const moment = require('moment-timezone') const { welcome, goodbye } = require('./utils/greeting') const time = moment.tz('Asia/Jakarta').format('DD/MM HH:mm:ss') const { databaseView, databaseInput } = require('./utils/db') +const { web } = require('./utils/web') async function krypton () { + const client = new WAConnection() + client.cmd = new Collection() + client.runtimeDb = new Collection() + const cooldowns = new Collection() + /*** * Initial Database **/ @@ -43,10 +50,6 @@ async function krypton () { databaseInput('CREATE TABLE IF NOT EXISTS afks( uid VARCHAR(30) PRIMARY KEY NOT NULL, afk VARCHAR(10) NOT NULL, reason CHAR(225) NOT NULL, timestart VARCHAR(100) NOT NULL )') .catch(err => console.log(err)) - const client = new WAConnection() - client.cmd = new Collection() - client.runtimeDb = new Collection() - const cooldowns = new Collection() client.logger.level = 'warn' await client.on('qr', () => { console.log(color('[', 'white'), color('!', 'red'), color(']', 'white'), color(' Scan the QR code above')) @@ -77,6 +80,9 @@ async function krypton () { await client.connect({ timeoutMs: 30 * 1000 }) fs.writeFileSync('./sessions/krypton-sessions.json', JSON.stringify(client.base64EncodedAuthInfo(), null, '\t')) + // Web api proses + web(client) + await client.on('group-participants-update', async (greeting) => { try { const num = greeting.participants[0] diff --git a/src/utils/web.ts b/src/utils/web.ts new file mode 100644 index 0000000..fd072a5 --- /dev/null +++ b/src/utils/web.ts @@ -0,0 +1,68 @@ +export {} +// REQUIRE NPM PACKAGES +const { + MessageType +} = require('@adiwajshing/baileys') +const http = require('http') +const express = require('express') +const app = express() +const httpServer = http.createServer(app) +const osUtils = require('node-os-utils') +const os = require('os') +const io = require('socket.io')(httpServer) +const { color } = require('./color') + +const web = async (client) => { + const apiKey = process.env.WEB_API + // View Engine and static public folder + app.set('view engine', 'ejs') + app.use(express.static('./views')) + + // Root Route + app.get('/', (req, res) => { + res.render('index.ejs') + }) + + app.get('/send/:id/:text/:api', (req, res) => { + const { id, text, api } = req.params + if (api !== apiKey) return res.json({ info: 'Api Key salah', status: 502 }) + client.sendMessage(id, text, MessageType.text) + .then(() => { + res.json({ info: 'Berhasil mengirim', status: 200 }) + }).catch((err) => res.json({ info: 'Gagal mengirim', status: 502 })) + }) + + // CPU USAGE + const cpu = osUtils.cpu + + // USER and OS + const username = os.userInfo([{ encoding: 'buffer' }]).username + const osInfo = os.type() + + // SOCKET IO + io.on('connection', socket => { + console.log(color(`[INFO] ${socket.id} Server socket connected`, 'green')) + // USE SET INTERVAL TO CHECK RAM USAGE EVERY SECOND + setInterval(async () => { + // RAM USED tot - free + const ramUsed = Math.round(os.totalmem()) - Math.round(os.freemem()) + // RAM percentage + const ram = (ramUsed * 100 / Math.round(os.totalmem())).toFixed(0) + // Uptime and Chat + const chat = await client.chats.all().length + const uptime = Math.round(process.uptime()).toFixed(0) + // CPU USAGE PERCENTAGE + cpu.usage().then(cpu => socket.emit('ram-usage', { ram, cpu, username, osInfo, chat, uptime })) + }, 1000) + }) + + // Run the server + const PORT = 3000 + httpServer.listen(PORT, () => { + console.log(color('[INFO] Web api Server on port: ', 'green') + color(`${PORT}`, 'yellow')) + }) +} + +module.exports = { + web +} diff --git a/views/css/style.css b/views/css/style.css new file mode 100644 index 0000000..60bed7f --- /dev/null +++ b/views/css/style.css @@ -0,0 +1,82 @@ +@import url('https://fonts.googleapis.com/css2?family=Oswald&display=swap'); +* { + margin: 0; + padding: 0; + box-sizing: border-box; +} + +body { + font-family: 'Oswald', sans-serif; + color: #808080; +} + +h1 { + font-size: 4rem; + padding: 10px; + margin: 10px auto; +} + +.content { + display: flex; + flex-direction: column; + justify-content: center; + align-items: center; + width: 100%; + height: 100%; +} + +.uptime-label, +.bot, +.server, +.chat, +.user, +.os { + font-size: 1.1rem; + height: 30px; +} + +label { + margin: 5px auto; +} +.innerBar-ram, +.innerBar-cpu { + background: linear-gradient(to right, #3fffa2 0%, #ffdb3a 50%, #e5405e 100%); + height: 24px; + width: 0%; +} +.outerContainer-ram, +.outerContainer-cpu { + width: 400px; + height: 24px; + border-radius: 5px; + overflow: hidden; + background: lightgray; +} + +footer { + position: fixed; + bottom: 10px; + text-align: center; + padding: 10px; + left: 50%; + transform: translate(-50%); +} + +.home { + text-decoration: none; + position: sticky; + top: 10px; + left: 50%; + transform: translate(-50%); +} + +.home .home-svg { + width: 20px; + display: inline-block; +} + +@media (max-width: 768px) { + h1 { + font-size: 3rem; + } +} diff --git a/views/index.ejs b/views/index.ejs new file mode 100644 index 0000000..e6a2d8c --- /dev/null +++ b/views/index.ejs @@ -0,0 +1,44 @@ + + + + + + + Monitoring Bot + + + + + + +
+

KryPtoN Bot Resources

+
Hello
+ +
OS Type
+ + +
+
+
+ + +
+
+
+ + +
Total CHAT
+ + +
+
+ + + + + + + + + diff --git a/views/js/main.js b/views/js/main.js new file mode 100644 index 0000000..ea2bd36 --- /dev/null +++ b/views/js/main.js @@ -0,0 +1,70 @@ +// SOCKET IO +const socket = io(); +// SELECT ELEMENTS +const labelRam = document.querySelector('.ram-label'); +const labelCpu = document.querySelector('.cpu-label'); +const user = document.querySelector('.user'); +const os = document.querySelector('.os'); +const chatTotal = document.querySelector('.chat'); +const onTime = document.querySelector('.uptime'); + +// ON CONNECT EVENT +socket.on('connect', () => { + console.log('Connected'); +}); +// ON RAM USAGE EVENT +socket.on('ram-usage', ({ ram, cpu, username, osInfo, chat, uptime }) => { + // SHOW OS USER INFO + user.innerHTML = `Hello ${username}`; + os.innerHTML = `OS type: ${osInfo === 'Windows_NT' ? 'Microsoft Windows' : osInfo}` + // Set ram label + labelRam.innerHTML = `RAM ${ram} % `; + // Set Ram bar + $('.innerBar-ram').animate({ width: `${ram}%` }, 2000); + // Set cpu label + labelCpu.innerHTML = `CPU ${cpu} % `; + // Set cpu bar + $('.innerBar-cpu').animate({ width: `${cpu}%` }, 2000); + // Check + if (cpu > 90) { + notify(cpu) + } + chatTotal.innerHTML = `Total CHAT: ${chat}` + +function botUpTime(seconds){ + function pad(s){ + return (s < 10 ? '0' : '') + s; + } + var hours = Math.floor(seconds / (60*60)); + var minutes = Math.floor(seconds % (60*60) / 60); + var seconds = Math.floor(seconds % 60); + + //return pad(hours) + ':' + pad(minutes) + ':' + pad(seconds) + return onTime.innerHTML = `${pad(hours)}Jam ${pad(minutes)}Menit ${pad(seconds)}Detik` +} + +botUpTime(uptime) + +}); + +// NOTIFICATION FUNCTION +let notify = (info) => { + // If granted + if (Notification.permission === 'granted') { + new Notification('Title', { + body: `CPU over ${info} %` + }); + } + // If denied + if (Notification.permission !== 'denied') { + Notification.requestPermission() + .then((permission) => { + if (permission === 'granted') { + new Notification('Title', { + body: `CPU over ${info} %` + }); + }; + }); + }; + +};