Skip to content

Commit 9e2683e

Browse files
author
ItsAlexIK
committed
trigger code scanning
1 parent 53ce1c6 commit 9e2683e

1 file changed

Lines changed: 34 additions & 17 deletions

File tree

src/commands/pidinfo.js

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
const { SlashCommandBuilder, EmbedBuilder, MessageFlags } = require("discord.js");
1+
const {
2+
SlashCommandBuilder,
3+
EmbedBuilder,
4+
MessageFlags,
5+
} = require("discord.js");
26
const { execFile } = require("node:child_process");
37
const { promisify } = require("node:util");
48
const fs = require("node:fs/promises");
@@ -8,6 +12,12 @@ const ALLOWED_USER_ID = process.env.ALLOWED_USER_ID || "";
812
const ALLOWED_ROLE_ID = process.env.ALLOWED_ROLE_ID || "";
913
const execFileAsync = promisify(execFile);
1014

15+
if (!ALLOWED_USER_ID && !ALLOWED_ROLE_ID) {
16+
throw new Error(
17+
"PIDInfo command is disabled: ALLOWED_USER_ID and ALLOWED_ROLE_ID are not set."
18+
);
19+
}
20+
1121
function truncate(text, max = 1000) {
1222
return text.length <= max ? text : text.slice(0, max - 3) + "...";
1323
}
@@ -25,7 +35,7 @@ function formatEtime(etime) {
2535
for (const re of patterns) {
2636
const m = etime.match(re);
2737
if (!m) continue;
28-
const [ , d = "0", h = "0", m1 = "0", s = "0" ] = m;
38+
const [, d = "0", h = "0", m1 = "0", s = "0"] = m;
2939
const days = Number(d || 0);
3040
const hours = Number(h || 0);
3141
const mins = Number(m1 || 0);
@@ -78,15 +88,19 @@ async function fetchPidInfo(pid) {
7888
};
7989
} catch (err) {
8090
const stderr = err?.stderr || "";
81-
const busyboxPs = /unrecognized option: p/i.test(stderr) || /BusyBox/i.test(stderr);
91+
const busyboxPs =
92+
/unrecognized option: p/i.test(stderr) || /BusyBox/i.test(stderr);
8293
if (!busyboxPs) throw err;
8394
return fetchPidInfoBusybox(pid);
8495
}
8596
}
8697

8798
async function fetchPidInfoBusybox(pid) {
8899
// BusyBox ps lacks -p and has limited columns; pull all and filter
89-
const { stdout } = await execFileAsync("ps", ["-o", "pid,ppid,user,etime,time,stat,args"]);
100+
const { stdout } = await execFileAsync("ps", [
101+
"-o",
102+
"pid,ppid,user,etime,time,stat,args",
103+
]);
90104

91105
const lines = stdout.trim().split("\n");
92106
if (lines.length < 2) {
@@ -99,9 +113,9 @@ async function fetchPidInfoBusybox(pid) {
99113
throw new Error("PID not found");
100114
}
101115

102-
const match = row.trim().match(
103-
/^(\d+)\s+(\d+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.+)$/
104-
);
116+
const match = row
117+
.trim()
118+
.match(/^(\d+)\s+(\d+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.+)$/);
105119

106120
if (!match) {
107121
throw new Error("Unable to parse ps output");
@@ -154,7 +168,7 @@ async function sampleCpuPercent(pid, delayMs = 250) {
154168
const startTicks = proc2.start;
155169
const clkTck = getClkTck();
156170
const elapsed = Math.max(0.01, uptime - startTicks / clkTck);
157-
return ((procTicks / clkTck) / elapsed) * 100;
171+
return (procTicks / clkTck / elapsed) * 100;
158172
}
159173

160174
async function sampleMemPercent(pid) {
@@ -237,18 +251,20 @@ module.exports = {
237251
),
238252

239253
async execute(interaction) {
240-
const isAllowedUser = interaction.user.id === ALLOWED_USER_ID;
241-
const hasAllowedRole =
254+
const allowedByUser =
255+
ALLOWED_USER_ID && interaction.user.id === ALLOWED_USER_ID;
256+
257+
const allowedByRole =
258+
ALLOWED_ROLE_ID &&
242259
interaction.inGuild() &&
243-
Boolean(interaction.member?.roles?.cache?.has(ALLOWED_ROLE_ID));
260+
interaction.member?.roles?.cache.has(ALLOWED_ROLE_ID);
244261

245-
if (!isAllowedUser && !hasAllowedRole) {
262+
if (!allowedByUser && !allowedByRole) {
246263
return interaction.reply({
247264
content: "❌ You do not have permission to use this command.",
248265
flags: MessageFlags.Ephemeral,
249266
});
250267
}
251-
252268
const pid = interaction.options.getInteger("pid", true);
253269

254270
await interaction.deferReply({ flags: MessageFlags.Ephemeral });
@@ -259,11 +275,12 @@ module.exports = {
259275
await interaction.editReply({ embeds: [embed] });
260276
} catch (err) {
261277
console.error("Error in /pidinfo command:", err);
262-
const message = err?.message === "PID not found"
263-
? "❌ No process found for that PID."
264-
: "❌ Failed to fetch PID info.";
278+
const message =
279+
err?.message === "PID not found"
280+
? "❌ No process found for that PID."
281+
: "❌ Failed to fetch PID info.";
265282

266283
await interaction.editReply({ content: message });
267284
}
268285
},
269-
};
286+
};

0 commit comments

Comments
 (0)