-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat(messages): add server info command
Adds a new command that displays server information, including hostname, uptime, OS, CPU, memory, and disk usage.
- Loading branch information
Showing
4 changed files
with
176 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<boolean> { | ||
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<void> { | ||
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, | ||
} | ||
); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <BaseMessageHandlerAction[]>[ | ||
new PingAction(), | ||
new InfoServerAction(), | ||
new ResolveToHdAction(), | ||
]; |