The commandkit create command helps you quickly generate boilerplate code for commands, events, and custom plugin templates.
Basic Usage
Generate files using the create command:
commandkit create <template> [args...]
Generators automatically detect whether to use TypeScript or JavaScript based on your project configuration.
Built-in Templates
CommandKit includes these built-in generators:
Command
Generate a new bot commandcommandkit create command <name>
Event
Generate a new event handlercommandkit create event <name>
Generating Commands
Create a new command with a single command:
commandkit create command ping
This generates a command file in src/app/commands/:
// src/app/commands/ping.ts
import type { CommandData, ChatInputCommand, MessageCommand } from 'commandkit';
export const command: CommandData = {
name: 'ping',
description: 'ping command',
};
export const chatInput: ChatInputCommand = async (ctx) => {
await ctx.interaction.reply('Hello from ping!');
};
export const message: MessageCommand = async (ctx) => {
await ctx.message.reply('Hello from ping!');
};
// src/app/commands/ping.js
/**
* @type {import('commandkit').CommandData}
*/
export const command = {
name: 'ping',
description: 'ping command',
};
/**
* @type {import('commandkit').ChatInputCommand}
*/
export const chatInput = async (ctx) => {
await ctx.interaction.reply('Hello from ping!');
};
/**
* @type {import('commandkit').MessageCommand}
*/
export const message = async (ctx) => {
await ctx.message.reply('Hello from ping!');
};
Command Structure
Generated commands include:
command - Command metadata (name, description, options)
chatInput - Slash command handler
message - Text command handler
You can remove the message export if you only want slash commands, or vice versa.
Console Output
Command ping created at ./src/app/commands/ping.ts
Generating Events
Create a new event handler:
commandkit create event ready
This generates an event file in src/app/events/<event-name>/:
// src/app/events/ready/event.ts
import type { EventHandler } from 'commandkit';
const Ready: EventHandler<'ready'> = async () => {
console.log('ready event fired!');
};
export default Ready;
// src/app/events/ready/event.js
/**
* @type {import('commandkit').EventHandler<'ready'>}
*/
const onReady = async () => {
console.log('ready event fired!');
};
export default onReady;
Event Directory Structure
Events are organized in directories:
src/app/events/
├── ready/
│ └── event.ts
├── messageCreate/
│ └── event.ts
└── interactionCreate/
├── event.ts
└── 01_event.ts # Multiple handlers for same event
Multiple Event Handlers
If an event handler already exists, the generator creates a numbered file:
# First handler
commandkit create event messageCreate
# Creates: src/app/events/messageCreate/event.ts
# Second handler
commandkit create event messageCreate
# Creates: src/app/events/messageCreate/01_event.ts
# Third handler
commandkit create event messageCreate
# Creates: src/app/events/messageCreate/02_event.ts
Multiple event handlers are useful for organizing complex event logic into separate concerns.
Console Output
Event ready created at ./src/app/events/ready/event.ts
Custom Plugin Templates
Compiler plugins can register custom templates:
Using Custom Templates
commandkit create <template-name> [args...]
Example Plugin with Template
// plugins/my-plugin.js
import { defineCompilerPlugin } from 'commandkit/plugin'
import { writeFile } from 'fs/promises'
import { join } from 'path'
export default defineCompilerPlugin((options, environment) => {
return {
name: 'my-plugin',
templates: {
// Register a 'service' template
service: async (args) => {
const [name] = args
if (!name) {
throw new Error('Service name is required')
}
const content = `
export class ${name}Service {
constructor() {
console.log('${name}Service initialized')
}
async execute() {
// TODO: Implement service logic
}
}
`
const filePath = join('src/services', `${name}.service.ts`)
await writeFile(filePath, content)
console.log(`Service ${name} created at ${filePath}`)
},
},
}
})
Using the Custom Template
commandkit create service database
This would call your plugin’s template function and create:
// src/services/database.service.ts
export class DatabaseService {
constructor() {
console.log('DatabaseService initialized')
}
async execute() {
// TODO: Implement service logic
}
}
Template Discovery
The CLI shows available templates when an invalid template is used:
commandkit create invalid
# Output:
Template "invalid" not found. Available templates: "command", "event", "service"
Available templates include built-in templates (command, event) plus any templates registered by your plugins.
Error Handling
Missing Arguments
commandkit create command
# Output:
Error: Command name is required
File Already Exists
commandkit create command ping
# If ping.ts already exists:
Error: Command ping already exists.
Generators will not overwrite existing files. Delete or rename the existing file first.
Language Detection
The generator automatically detects your project’s language:
// Checks for tsconfig.json
if (existsSync('tsconfig.json')) {
// Generate .ts files
} else {
// Generate .js files
}
TypeScript Project
JavaScript Project
Detection: tsconfig.json existsOutput: .ts files with TypeScript types Detection: No tsconfig.jsonOutput: .js files with JSDoc types
Best Practices
Use kebab-case or lowercase for command names:# Good
commandkit create command user-info
commandkit create command serverinfo
# Avoid
commandkit create command UserInfo
commandkit create command Server_Info
Use Discord.js event names exactly:# Correct Discord.js event names
commandkit create event messageCreate
commandkit create event interactionCreate
commandkit create event guildMemberAdd
# Will work but won't trigger
commandkit create event message_create # Wrong name
Organize commands in subdirectories:# Create command in subdirectory
mkdir -p src/app/commands/admin
cd src/app/commands/admin
commandkit create command ban
# Results in:
src/app/commands/admin/ban.ts
After generating, customize the template:
- Update the command description
- Add command options
- Implement the actual logic
- Add error handling
- Add permissions and validations
The generated code is just a starting point!
Integration with Development
Generators work seamlessly with the dev server:
Generate new command (in another terminal)
commandkit create command hello
Edit the generated file
The dev server automatically detects and hot-reloads the new command
Test immediately
Your new command is instantly available in Discord
You don’t need to restart the dev server when generating new files. Hot reloading handles it automatically!
Examples
Generate Multiple Commands
# Generate several commands at once
commandkit create command ping
commandkit create command help
commandkit create command userinfo
commandkit create command serverinfo
Generate Event Handlers
# Common event handlers
commandkit create event ready
commandkit create event messageCreate
commandkit create event interactionCreate
commandkit create event guildMemberAdd
Organize in Subdirectories
# Create admin commands
mkdir -p src/app/commands/admin
cd src/app/commands/admin
commandkit create command ban
commandkit create command kick
commandkit create command mute
# Create utility commands
mkdir -p src/app/commands/utils
cd src/app/commands/utils
commandkit create command ping
commandkit create command uptime
Next Steps
Development Mode
Use hot reloading with your generated files
Build for Production
Bundle your commands and events