Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Slash Commands

slash commands

A slash command only appears when starting a message with /.

Instead of having the bot automatically respond to events, slash commands only run when someone uses the slash command.

creating our commands folder

Each slash command should be put in its own file, in the folder utility (found inside commands)

slash command structure

discord.js provides a slash command builder to help us make slash commands.

import { SlashCommandBuilder } from 'discord.js'

export const data = new SlashCommandBuilder()
  .name('some command');
  // can add more information about the slash command

export async function execute (interaction) {
  // do something    
}

See ping.js, in commands/utility for a basic example.

important

Every time you make a new slash command, or make changes to an existing slash command, make sure to run node deploy-commands.js.

adding options (inputs) to your commands

use .addStringOption(), addUserOption(), etc. to let users give info to your slash command. A full list of ways to add information to a command can be found here

example

Here, our command is set to take a string input. The option is called input, has description Text to repeat, and cannot be empty.

import { SlashCommandBuilder } from 'discord.js';

export const data = new SlashCommandBuilder()
    .setName('repeat')
    .setDescription('Give a message to repeat!')
    .addStringOption(option =>
        option
            .setName('input')
            .setDescription('Text to repeat')
            .setRequired(true));

export async function execute(interaction) {
    const text = interaction.options.getString('input');
    await interaction.reply(text);
}

Feel free to try this code!