41 lines
1.5 KiB
TypeScript
41 lines
1.5 KiB
TypeScript
import { ChatInputCommandInteraction, SlashCommandBuilder, SlashCommandOptionsOnlyBuilder, SlashCommandSubcommandBuilder } from "discord.js";
|
|
import { join } from "node:path";
|
|
import { requireDirectorySync } from "../utils/requireDirectory";
|
|
import { AdminUsers } from "./admin";
|
|
|
|
export type DiscordCommandData = SlashCommandBuilder | SlashCommandOptionsOnlyBuilder | SlashCommandSubcommandBuilder
|
|
export type DiscordCommandExecute = (interaction: ChatInputCommandInteraction) => Promise<any>
|
|
|
|
export interface DiscordCommand {
|
|
data: DiscordCommandData
|
|
execute: DiscordCommandExecute
|
|
}
|
|
|
|
export function defineCommand(
|
|
data: DiscordCommandData,
|
|
execute: DiscordCommandExecute,
|
|
isAdminCommand=false,
|
|
): DiscordCommand {
|
|
if (isAdminCommand) {
|
|
return {
|
|
data: data,
|
|
execute: async (interaction: ChatInputCommandInteraction): Promise<any> => {
|
|
if (AdminUsers.includes(interaction.user.id)) {
|
|
execute(interaction);
|
|
} else {
|
|
interaction.reply("당신은 어드민이 아닙니다");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return {
|
|
data: data,
|
|
execute: execute,
|
|
}
|
|
}
|
|
|
|
const commandDirectory = join(__dirname, "commands");
|
|
export const defineCommands = requireDirectorySync<DiscordCommand>(commandDirectory);
|
|
export const commandExecuteNameHashMap: {
|
|
[key: string]: DiscordCommandExecute
|
|
} = Object.fromEntries(defineCommands.map(command => [command.data.name, command.execute]));
|