logo

Constatic

Discord Bot BaseCommandsSlash

Slash

How to create discord slash commands

Creating slash commands

First of all, import the createCommand function from base and ApplicationCommandType from discord.js

import { Command } from "#base";
import { ApplicationCommandType } from "discord.js";

See conventions to create your commands

How to create slash command

To create a slash command, you need to set name, description and type.

hello.ts
createCommand({
    name: "hello",
    description: "Hello world command",
    type: ApplicationCommandType.ChatInput,
    async run(interaction) {
        interaction.reply({ flags: ["Ephemeral"], content: "Hello world!" });
    },
});

Slash command names cannot be empty, cannot contain special characters, capital letters or spaces

Set slash command options

You can set options, subcommands and groups too

manage.ts
import { createCommand } from "#base";
import { ApplicationCommandOptionType, ApplicationCommandType } from "discord.js";
 
createCommand({
    name: "manage",
    description: "Manage command",
    type: ApplicationCommandType.ChatInput,
    options: [ 
        { 
            name: "users", 
            description: "Manage users command", 
            type: ApplicationCommandOptionType.Subcommand
            options: [ 
                { 
                    name: "user", 
                    description: "user", 
                    type: ApplicationCommandOptionType.User
                    required
                } 
            ], 
        } 
    ], 
    async run(interaction) {
        const { options } = interaction;
 
        switch(options.getSubcommand(true)){
            case "users":{
                const user = options.getUser("user", true);
                interaction.reply({ flags: ["Ephemeral"], content: `${user} managed` })
                return;
            }
        }
    },
});

Global

The global option in commands is useful when you want the command to be registered to the application rather than the guild. It is only needed when guild IDs are set in the setupCreators function.

The code below causes commands to be registered only to the valid guilds listed in the array:

src/discord/index.ts
import { setupCreators } from "#base";
 
export const { createCommand, createEvent, createResponder } = setupCreators({
    commands: {
        guilds: [
            process.env.MAIN_GUILD_ID,
            process.env.DEV_GUILD_ID,
            process.env.TEST_GUILD_ID,
        ]
    }
});

Now look at this code where some commands are created:

createCommand({
    name: "settings",
    description: "Server settings command",
    async run(interaction){
    // ...
    }
});
 
createCommand({
    name: "manage",
    description: "Command to manage members, channels and roles",
    async run(interaction){
    // ...
    }
});
 
createCommand({
    name: "information",
    description: "Displays bot information",
    global: true, 
    async run(interaction){
    // ...
    }
});

The settings and manage commands will be registered in the guilds defined in the setupCreators function, but the information command will be registered globally in the application, thus being available in any guild.

On this page