Getting Started
CommandKit is a powerful framework for building Discord bots. In this guide, we'll walk you through the process of getting started with CommandKit.
Prerequisites
Before you begin, make sure you have installed discord.js
and commandkit
in your project.
Building a simple bot
It is recommended to use modules
instead of commonjs
for commandkit projects. CommandKit will allow you to use both require
and import
in modules
projects.
First of all, create a commandkit.config.ts
at the root of your project. This file is used to configure CommandKit and its plugins.
import { defineConfig } from 'commandkit';
export default defineConfig({});
Then, create a src
folder and add app.ts
file inside it.
You can also use .js
files instead of .ts
. CommandKit supports both JavaScript and TypeScript out of the box.
The app.ts
file is the entrypoint of your bot. This file exports your discord.js client.
import { Client } from 'discord.js';
const client = new Client({
intents: [...],
});
// must export the client here
export default client;
You don't need to call client.login()
as commandkit will do that for you. You should store your bot token in .env
file under DISCORD_TOKEN
or TOKEN
variable.
Now, lets create a simple command. Create a src/app/commands
folder and add ping.ts
file inside it.
import type { SlashCommand, CommandData, MessageCommand } from 'commandkit';
export const command: CommandData = {
name: 'ping',
description: 'Ping pong!',
};
export const chatInput: SlashCommand = async (ctx) => {
await ctx.interaction.reply('Pong!');
};
// you can also add a message command handler
export const message: MessageCommand = async (ctx) => {
await ctx.message.reply('Pong!');
};
This command will reply with Pong!
when the user types /ping
or !ping
in the chat.
You can override the default prefix for message commands using:
import { commandkit } from 'commandkit';
commandkit.setPrefixResolver(async (message) => {
// you can use `message` to fetch guild specific prefixes if needed
return '?';
});
An example of this could be:
import { commandkit, cacheTag } from 'commandkit';
import { database } from '../database.ts';
// this function's result will be automatically cached
async function fetchGuildPrefix(guildId: string) {
'use cache';
cacheTag(`prefix:${guildId}`);
const setting = await database.findUnique({
where: { guildId },
select: { prefix: true },
});
// if no prefix is found, return the default prefix
return setting?.prefix ?? '!';
}
commandkit.setPrefixResolver((message) => {
return fetchGuildPrefix(message.guildId);
});
You can also return array of prefixes.
Now, lets run the bot. You can use commandkit dev
to start the bot in development mode. This will automatically reload the bot when you make changes to your code.
npx commandkit dev
When running in development mode, CommandKit will generate a .commandkit
folder in your project directory. This folder contains the compiled files used in development mode. You should not commit this folder to your version control system. You should add it to your .gitignore
file.
Production build
When you are ready to deploy your bot, you can use commandkit build
to create a production build of your bot. This will create a dist
folder in your project directory containing the compiled files.
npx commandkit build
Starting the bot in production mode
You can use commandkit start
to start the bot in production mode. This will load the environment variables from .env
file in your project directory.
npx commandkit start
If you want to manually start the bot in production mode, you can use the following command:
node dist/index.js
The index.js
file is autogenerated by CommandKit
.