Guide
Commands Setup

Commands Setup

CommandKit supports both slash commands and context menu commands. Since both have the same triggers (interactions) and similar command structure, they are both stored and handled from the same commands directory.

Slash Command

Here's an example /ping slash command which replies with "Pong!"

commands/Misc/ping.js
module.exports = {
    data: {
        name: 'ping',
        description: 'Pong!',
    },
 
    run: ({ interaction, client, handler }) => {
        interaction.reply(`:ping_pong: Pong! ${client.ws.ping}ms`);
    },
 
    options: {
        devOnly: true,
        userPermissions: ['Administrator', 'AddReactions'],
        botPermissions: ['Administrator', 'AddReactions'],
        deleted: false,
    },
};

Context Menu Command

Here's an example content command which replies with the content of the target message.

commands/Misc/content.js
const { ApplicationCommandType } = require("discord.js");
 
module.exports = {
    data: {
        name: 'content',
        type: ApplicationCommandType.Message,
    },
 
    run: ({ interaction, client, handler }) => {
        interaction.reply(`The message is: ${interaction.targetMessage.content}`);
    },
 
    options: {
        devOnly: true,
        userPermissions: ['Administrator', 'AddReactions'],
        botPermissions: ['Administrator', 'AddReactions'],
        deleted: false,
    },
};

Autocomplete function

In addition to the run function, you can also export an autocomplete function from your command file so that you don't have to create a new "interactionCreate" event listener.

Here's an example of how to use the autocomplete function:

commands/Fun/pet.js
const pets = require('../data/pets.json');
 
module.exports = {
    data: new SlashCommandBuilder()
        .setName('pet')
        .setDescription('Find a pet from a list of pets.')
        .addStringOption((option) =>
            option
                .setName('pet')
                .setDescription('The pet to check.')
                .setRequired(true)
                .setAutocomplete(true)
        ),
 
    run: ({ interaction, client, handler }) => {
        const targetPetId = interaction.options.getString('pet');
        const targetPetObj = pets.find((pet) => pet.id === targetPetId);
 
        interaction.reply(`Your pet name is ${targetPetObj.name}.`);
    },
 
    autocomplete: ({ interaction, client, handler }) => {
        const focusedPetOption = interaction.options.getFocused(true);
 
        const filteredChoices = pets.filter((pet) => pet.name.startsWith(focusedPetOption));
 
        const results = filteredChoices.map((pet) => {
            return {
                name: `${pet.name} | ${pet.type}`,
                value: pet.id,
            };
        });
 
        interaction.respond(results.slice(0, 25));
    }
}

Properties and Methods

data

This property contains the command's structural information, and is required to register and handle commands.

run

This function will be called when the command is executed.

autocomplete

This function will be called when an autocomplete interaction event is triggered for any option set as autocomplete in this command's data object.

options (optional)

This property contains the command's registration/handling behaviour.