Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/remote-kill #66

Merged
merged 4 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;