Skip to main content
Version: Next

Command File

The src/app/commands/<command>.ts (or src/app/commands/<command>.js) file is used to define the commands for your application. Each command is defined in its own file, and the file name should match the command name. For example, if you have a command called ping, you should create a file called ping.ts (or ping.js) in the src/app/commands directory.

The command file can export multiple handler functions, such as:

  • chatInput: This function is executed when the command is triggered by a chat input event (slash command).
  • message: This function is executed when the command is triggered by a message event (text command).
  • userContextMenu: This function is executed when the command is triggered by a user context menu event.
  • messageContextMenu: This function is executed when the command is triggered by a message context menu event.

When defining a command, if you export userContextMenu or messageContextMenu, commandkit will automatically register the command as a context menu command without you having to explicitly define them as one.

Example command

src/app/commands/avatar.ts
import {
MessageCommand,
ChatInputCommand,
UserContextMenuCommand,
MessageContextMenuCommand,
} from 'commandkit';
import { ApplicationCommandOptionType } from 'discord.js';

export const command = {
name: 'avatar',
description: 'This is an avatar command.',
options: [
{
name: 'user',
description: 'The user to get the avatar for.',
type: ApplicationCommandOptionType.User,
},
],
};

export const userContextMenu: UserContextMenuCommand = async (ctx) => {
const target = ctx.interaction.targetUser;

await ctx.interaction.reply({
embeds: [
{
title: `Avatar of ${target.username}`,
image: {
url: target.displayAvatarURL({ size: 2048 }),
},
color: 0x7289da,
},
],
});
};

export const messageContextMenu: MessageContextMenuCommand = async (ctx) => {
const target = ctx.interaction.targetMessage.author;

await ctx.interaction.reply({
embeds: [
{
title: `Avatar of ${target.username}`,
image: {
url: target.displayAvatarURL({ size: 2048 }),
},
color: 0x7289da,
},
],
});
};

export const chatInput: ChatInputCommand = async (ctx) => {
const user = ctx.options.getUser('user') ?? ctx.interaction.user;

await ctx.interaction.reply({
embeds: [
{
title: `Avatar of ${user.username}`,
image: {
url: user.displayAvatarURL({ size: 2048 }),
},
color: 0x7289da,
},
],
});
};

export const message: MessageCommand = async (ctx) => {
const user = ctx.options.getUser('user') ?? ctx.message.author;

await ctx.message.reply({
embeds: [
{
title: `Avatar of ${user.username}`,
image: {
url: user.displayAvatarURL({ size: 2048 }),
},
color: 0x7289da,
},
],
});
};