-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from su-its/feature/remote-kill
- Loading branch information
Showing
2 changed files
with
71 additions
and
0 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
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; |
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 |
---|---|---|
@@ -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; |