test

Constatic

Discord Bot BaseCommands

Slash

How to create discord slash commands

To create a command, you need to import the Command class from the base and ApplicationCommandType enum 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
new Command({
    name: "hello",
    description: "Hello world command",
    type: ApplicationCommandType.ChatInput,
    async run(interaction) {
        interaction.reply({ 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 { Command } from "#base";
import { ApplicationCommandOptionType, ApplicationCommandType } from "discord.js";
 
new Command({
    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({ ephemeral, content: `${user} managed` })
                return;
            }
        }
    },
});

On this page