What Are Slash Commands?
Slash commands appear when a user types / in a Discord channel. They are the modern way to interact with Discord bots, offering auto-complete, parameter descriptions, and a cleaner interface than prefix-based commands.
Creating Slash Commands with discord.js
const { SlashCommandBuilder } = require('discord.js');
module.exports = {
data: new SlashCommandBuilder()
.setName('ping')
.setDescription('Replies with Pong!'),
async execute(interaction) {
await interaction.reply('Pong!');
},
};Registering Commands
Slash commands must be registered with Discord's API before they appear. Use the deploy-commands.js script from the discord.js guide to register globally (takes up to 1 hour) or per-guild (instant for testing).
Adding Parameters
Use .addStringOption(), .addIntegerOption(), or .addUserOption() to add required or optional parameters to your commands. Discord shows these to users as they type.
Hosting Your Bot
Once your slash commands work locally, deploy to CraftNodes Discord Bot Hosting for 24/7 uptime. Upload your files, set your bot token as an environment variable, and start the server.