Skip to content

Commit

Permalink
✨ feat(messages): add server info command
Browse files Browse the repository at this point in the history
Adds a new command that displays server information, including hostname, uptime, OS, CPU, memory, and disk usage.
  • Loading branch information
binsarjr committed Mar 21, 2024
1 parent 6e5b34c commit 71369cd
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 49 deletions.
2 changes: 1 addition & 1 deletion src/actions/message/group/mutation/KickMemberAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
31 changes: 0 additions & 31 deletions src/actions/message/random/InfoServerAction.ts

This file was deleted.

190 changes: 175 additions & 15 deletions src/actions/message/random/PingAction.ts
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,
}
);
});
}
}
2 changes: 0 additions & 2 deletions src/configs/commands/messagesHandler/random.ts
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(),
];

0 comments on commit 71369cd

Please sign in to comment.