logo

Constatic

Discord Bot BaseCommands

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;
            }
        }
    },
});

On this page