Skip to content

Commit

Permalink
Merge pull request #66 from su-its/feature/remote-kill
Browse files Browse the repository at this point in the history
  • Loading branch information
KinjiKawaguchi authored Oct 2, 2024
2 parents 1b28d8b + c3f3f41 commit 715671f
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/commands/kill.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { type CommandInteraction, SlashCommandBuilder } from "discord.js";
import type CommandWithArgs from "../types/commandWithArgs";
import checkIsAdmin from "../utils/checkMemberRole";

const killCommand: CommandWithArgs = {
data: new SlashCommandBuilder()
.setName("kill")
.setDescription("指定されたボットプロセスを終了します")
.addStringOption((option) =>
option
.setName("pid")
.setDescription("終了するプロセスのPID")
.setRequired(true),
) as SlashCommandBuilder,
execute: killCommandHandler,
};

async function killCommandHandler(interaction: CommandInteraction) {
//adminロールを持っているか確認
const isAdmin: boolean = await checkIsAdmin(interaction);
if (!isAdmin)
return await interaction.reply("このコマンドは管理者のみ使用可能です。");

const targetPid = interaction.options.get("pid")?.value as string;
const currentPid = process.pid.toString();

if (!targetPid) return await interaction.reply("PIDを指定してください");
if (targetPid && targetPid === currentPid) {
console.log(`Killing process ${currentPid}`);
await interaction.reply(`プロセス ${currentPid} を終了します...`);
process.exit(0);
} else {
await interaction.reply(
`PID ${targetPid} は現在のプロセスではありません。`,
);
}
}

export default killCommand;
32 changes: 32 additions & 0 deletions src/commands/ps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import * as os from "node:os";
import { type CommandInteraction, SlashCommandBuilder } from "discord.js";
import type CommandWithArgs from "../types/commandWithArgs";
import checkIsAdmin from "../utils/checkMemberRole";

const psCommand: CommandWithArgs = {
data: new SlashCommandBuilder()
.setName("ps")
.setDescription("現在のボットプロセス情報を表示します"),
execute: psCommandHandler,
};

async function psCommandHandler(interaction: CommandInteraction) {
//adminロールを持っているか確認
const isAdmin: boolean = await checkIsAdmin(interaction);
if (!isAdmin)
return await interaction.reply("このコマンドは管理者のみ使用可能です。");

const processInfo = await getProcessInfo();
await interaction.reply(
`ボットプロセス情報:\nPID: ${processInfo.pid}\nホスト名: ${processInfo.hostname}`,
);
}

async function getProcessInfo(): Promise<{ pid: number; hostname: string }> {
return {
pid: process.pid,
hostname: os.hostname(),
};
}

export default psCommand;

0 comments on commit 715671f

Please sign in to comment.