56 lines
1.5 KiB
TypeScript
56 lines
1.5 KiB
TypeScript
import {
|
|
ChatInputCommandInteraction,
|
|
SlashCommandBuilder,
|
|
type SlashCommandOptionsOnlyBuilder,
|
|
SlashCommandSubcommandBuilder,
|
|
} from "discord.js";
|
|
import { join } from "node:path";
|
|
import { requireDirectory } 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(import.meta.dirname, "commands");
|
|
export const defineCommands =
|
|
await requireDirectory<DiscordCommand>(commandDirectory);
|
|
export const commandExecuteNameHashMap: {
|
|
[key: string]: DiscordCommandExecute;
|
|
} = Object.fromEntries(
|
|
defineCommands.map((command) => [command.data.name, command.execute]),
|
|
);
|