The Basics

Clear and to-the-point tutorials with everything you'll need to integrate your bot with Discord.JS Slash commands, starting with the basics, and with many more advanced examples to use.

This slash commands documentation is adapted from advaith1's 'Slash Commands in Discord.js' GitHub guide, but I have improved on them with better clarity and more examples.

discord.js doesn't have full support for slash commands yet (there's a pr) but you can still use the underlying api and websocket to use them. Note that discord.js doesn't officially support using client.api, this is basically just a workaround until they fully release support.

Please read Discord's Slash Command docs since they have actual docs and details for slash commands; the code examples below are just how you can implement it using discord.js.

Note that slash commands won't show in a server unless that server has authorized it with the applications.commands oauth2 scope (not just the bot scope).

This does not require a discord.js update! It should work as long as you're using a modern version (anything v12 would probably work, obviously v12.5.1/latest is recommended)

Alternatively, you can manage commands and handle interactions with slash-create.

Registering a command

Here's how to register a command. You only need to register each command one time.

It is recommended to use an eval() command for this, although this will work without the check.

There are two ways of setting up slash commands: For testing/guild-specific (instant changes), and for deployment/global (can take up to an hour to update).

bot.js
client.api.applications(client.user.id).guilds('guild-id-here').commands.post({
        data: {
            name: "help",
            description: "Get information about the bot."
            // possible options here e.g. options: [{...}]
        }
});

Receiving & Handling the Event

Here is the bare-bones code for receiving the event, as well as an example where you can respond to the event by utilizing if statements to detect the command sent.

Send an Interaction Response object

interaction is an Interaction object

bot.js
client.ws.on('INTERACTION_CREATE', async interaction => {
  // do stuff and respond here
})

Responding with Embeds

You can respond to the interaction with an embed rather than basic text. To do this, check Embeds.

Sending a Follow-up Message

This goes inside the "Receiving the Event" block. see Webhook#send docs

bot.js
new Discord.WebhookClient(client.user.id, interaction.token).send('hello world')

Last updated