Create Discord Command
Create one command module that exports command and matches the repository type pattern.
Workflow
- Confirm requested command shape.
- Determine command name, description, options/subcommands, and response style (ephemeral/public).
- Choose command file location.
- Use
src/commands/<name>.tsfor a simple command. - Use
src/commands/<group>/index.tsfor grouped or complex command sets. - For complex implementations, prefer barrel-file organization by default: create a dedicated subdirectory and place the command in
index.ts.
- Implement typed command module.
- Import command types from
discord.jsandCommandfrom@/types. - Export
commandexactly. - Use
SlashCommandBuilderfor schema andexecutefor behavior.
- Handle runtime behavior safely.
- Use
MessageFlags.Ephemeralwhen response should be private. - Call
interaction.deferReply()before long operations. - Wrap non-trivial logic with clear error paths if needed.
- Validate and deploy.
- Run
npm run typecheck. - Run
npm run check. - Run
npm run deploy-commands(ornpm run deploy-commands -- --globalwhen explicitly requested).
Command Template
import { type ChatInputCommandInteraction, MessageFlags, SlashCommandBuilder } from 'discord.js';
import type { Command } from '@/types';
export const command: Command<ChatInputCommandInteraction> = {
data: new SlashCommandBuilder().setName('command-name').setDescription('Command description'),
execute: async (interaction) => {
await interaction.reply({
content: 'Response',
flags: MessageFlags.Ephemeral
});
}
};
Constraints
- Use
@/path aliases, not relative imports for internal modules. - Keep strict typing; avoid non-null assertions unless required.
- Keep behavior aligned with
src/events/interaction-create.tscommand routing.