From 71369cd91a2c175c45fa25ece34de3bcf7115222 Mon Sep 17 00:00:00 2001 From: binsarjr Date: Thu, 21 Mar 2024 12:41:42 +0700 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(messages):=20add=20server=20in?= =?UTF-8?q?fo=20command?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds a new command that displays server information, including hostname, uptime, OS, CPU, memory, and disk usage. --- .../group/mutation/KickMemberAction.ts | 2 +- .../message/random/InfoServerAction.ts | 31 --- src/actions/message/random/PingAction.ts | 190 ++++++++++++++++-- .../commands/messagesHandler/random.ts | 2 - 4 files changed, 176 insertions(+), 49 deletions(-) delete mode 100644 src/actions/message/random/InfoServerAction.ts diff --git a/src/actions/message/group/mutation/KickMemberAction.ts b/src/actions/message/group/mutation/KickMemberAction.ts index c205c2e..412a54d 100644 --- a/src/actions/message/group/mutation/KickMemberAction.ts +++ b/src/actions/message/group/mutation/KickMemberAction.ts @@ -13,7 +13,7 @@ import type { MessagePattern } from "../../../../types/MessagePattern.js"; export default class extends GroupMessageHandlerAction { patterns(): MessagePattern { - return withSignRegex("kick .*"); + return [withSignRegex("kick .*"), withSignRegex("rm .*")]; } protected eligableIfBotIsAdmin(socket: WASocket, metadata: GroupMetadata) { diff --git a/src/actions/message/random/InfoServerAction.ts b/src/actions/message/random/InfoServerAction.ts deleted file mode 100644 index 2125a7a..0000000 --- a/src/actions/message/random/InfoServerAction.ts +++ /dev/null @@ -1,31 +0,0 @@ -import type { WAMessage, WASocket } from "@whiskeysockets/baileys"; -import BaseMessageHandlerAction from "../../../foundation/actions/BaseMessageHandlerAction.js"; -import { withSign } from "../../../supports/flag.js"; -import { getJid, sendWithTyping } from "../../../supports/message.js"; -import type { MessagePattern } from "../../../types/MessagePattern.js"; - -export default class extends BaseMessageHandlerAction { - patterns(): MessagePattern { - return withSign("info"); - } - - async isEligibleToProcess( - socket: WASocket, - message: WAMessage - ): Promise { - return !!message.key.fromMe; - } - - async process(socket: WASocket, message: WAMessage): Promise { - const rtf = new Intl.RelativeTimeFormat("id-ID", { numeric: "auto" }); - const ping = - Date.now() - (+(message.messageTimestamp as number) || Date.now()); - - await sendWithTyping( - socket, - { text: `info server. not yet` }, - getJid(message), - { quoted: message } - ); - } -} diff --git a/src/actions/message/random/PingAction.ts b/src/actions/message/random/PingAction.ts index 0ff9bb7..6c8ac15 100644 --- a/src/actions/message/random/PingAction.ts +++ b/src/actions/message/random/PingAction.ts @@ -1,31 +1,191 @@ import type { WAMessage, WASocket } from "@whiskeysockets/baileys"; +import { exec } from "child_process"; +import os from "os"; +import util from "util"; import BaseMessageHandlerAction from "../../../foundation/actions/BaseMessageHandlerAction.js"; +import { Queue } from "../../../services/queue.js"; import { withSign } from "../../../supports/flag.js"; import { getJid, sendWithTyping } from "../../../supports/message.js"; import type { MessagePattern } from "../../../types/MessagePattern.js"; +const execPromise = util.promisify(exec); export default class extends BaseMessageHandlerAction { patterns(): MessagePattern { return withSign("ping"); } - async isEligibleToProcess( - socket: WASocket, - message: WAMessage - ): Promise { - return !!message.key.fromMe; + getPing(messageTimestamp: number | Long) { + const rtf = new Intl.RelativeTimeFormat("id-ID", { numeric: "auto" }); + + let timestamp = messageTimestamp.toString().padEnd(13, "0"); + const ping = Date.now() - (+timestamp || Date.now()); + return `Pong! ${rtf.format(+ping / 1_000, "seconds")}`; + } + + async getServerInformation() { + const serverInfo: { + hostname: string; + uptime: string; + os: { + platform: string; + type: string; + release: string; + arch: string; + }; + cpu: { + model: string; + speed: number; + cores: number; + }; + memory: { + total: number; + free: number; + used: number; + }; + disk: { + fs: string; + size: string; + used: string; + available: string; + capacity: string; + mount: string; + }[]; + } = { + hostname: "", + uptime: "", + os: { + platform: "", + type: "", + release: "", + arch: "", + }, + cpu: { + model: "", + speed: 0, + cores: 0, + }, + memory: { + total: 0, + free: 0, + used: 0, + }, + disk: [], + }; + + // Hostname + serverInfo.hostname = os.hostname(); + + // Uptime + const uptime = os.uptime(); + serverInfo.uptime = `${Math.floor(uptime / 3600)} hours, ${Math.floor( + (uptime % 3600) / 60 + )} minutes`; + + // OS Information + serverInfo.os = { + platform: os.platform(), + type: os.type(), + release: os.release(), + arch: os.arch(), + }; + + // CPU Information + serverInfo.cpu = { + model: os.cpus()[0].model, + speed: os.cpus()[0].speed, + cores: os.cpus().length, + }; + + // Memory Information + serverInfo.memory = { + total: os.totalmem(), + free: os.freemem(), + used: os.totalmem() - os.freemem(), + }; + + // Disk Information + const diskSpace = await execPromise("df -h"); + + const diskLines = diskSpace.stdout + .split("\n") + .filter((line) => line.startsWith("/")); + serverInfo.disk = diskLines.map((line) => { + const [fs, size, used, available, capacity, mount] = line.split(/\s+/); + return { + fs, + size, + used, + available, + capacity, + mount, + }; + }); + + return serverInfo; + } + bytesToGB(bytes: number) { + return (bytes / (1024 * 1024 * 1024)).toFixed(2) + " GB"; } async process(socket: WASocket, message: WAMessage): Promise { - const rtf = new Intl.RelativeTimeFormat("id-ID", { numeric: "auto" }); - const ping = - Date.now() - (+(message.messageTimestamp as number) || Date.now()); - - await sendWithTyping( - socket, - { text: `Pong! ${rtf.format(-ping / 1_000, "seconds")}` }, - getJid(message), - { quoted: message } - ); + const totalMemory = os.totalmem(); + + // Memori yang digunakan oleh sistem dalam bytes + const usedMemory = totalMemory - os.freemem(); + + Queue.add(async () => { + const [serverInfo] = await Promise.all([this.getServerInformation()]); + + const textDisk: string[] = []; + + serverInfo.disk.map((disk) => { + textDisk.push( + ` +*${disk.fs}* +Size: ${disk.size} +Used: ${disk.used} +Available: ${disk.available} +Capacity: ${disk.capacity} +Mount: ${disk.mount} + + +`.trim() + ); + }); + await sendWithTyping( + socket, + { + text: ` + + +${this.getPing(message.messageTimestamp!)} + +*Hostname:* ${serverInfo.hostname} +*Uptime:* ${serverInfo.uptime} + +*OS:* ${serverInfo.os.platform} ${serverInfo.os.type} ${ + serverInfo.os.release + } ${serverInfo.os.arch} +*CPU:* ${serverInfo.cpu.model} ${serverInfo.cpu.speed} MHz ${ + serverInfo.cpu.cores + } cores +*Virtual Memory:* ${this.bytesToGB(usedMemory)} GB / ${this.bytesToGB( + totalMemory + )} GB (${Math.round((usedMemory / totalMemory) * 100)}%) + +*Disk:* +${textDisk.join("\n\n")} + + + + + `.trim(), + }, + getJid(message), + { + quoted: message, + } + ); + }); } } diff --git a/src/configs/commands/messagesHandler/random.ts b/src/configs/commands/messagesHandler/random.ts index e3d7058..a4c6353 100644 --- a/src/configs/commands/messagesHandler/random.ts +++ b/src/configs/commands/messagesHandler/random.ts @@ -1,10 +1,8 @@ -import InfoServerAction from "../../../actions/message/random/InfoServerAction.js"; import PingAction from "../../../actions/message/random/PingAction.js"; import ResolveToHdAction from "../../../actions/message/random/ResolveToHdAction.js"; import type BaseMessageHandlerAction from "../../../foundation/actions/BaseMessageHandlerAction.js"; export default [ new PingAction(), - new InfoServerAction(), new ResolveToHdAction(), ];