What a slash command is
Slash commands are registered with Discord ahead of time. When someone runs one, your bot receives an interaction and must answer within 3 seconds (or acknowledge it and answer later).
const { Client, GatewayIntentBits, Events } = require("discord.js");
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.once(Events.ClientReady, async (c) => {
await c.application.commands.set([
{ name: "ping", description: "Check that the bot is alive" },
]);
});
client.on(Events.InteractionCreate, async (interaction) => {
if (interaction.isChatInputCommand() && interaction.commandName === "ping") {
await interaction.reply("Pong!"); // reply within 3 seconds
}
});
client.login(process.env.DISCORD_TOKEN);Options
Commands can take typed options: text, integers, true/false, users, roles and channels. Required options must come before optional ones. In the sandbox, the command picker shows the same options.
Who can use a command
Set required permissions (for example Kick Members) so Discord hides the command from members who don't have them. The sandbox simulates this.
Ephemeral replies
Replies can be ephemeral: only the person who ran the command sees them. Great for confirmations and errors.
Global vs. server commands
Generated bots register global commands. While testing, set the secret DISCORD_GUILD_ID to your test server's ID to register them there instantly.
Ready to build your first bot?
Create a bot — free