Creating Discord Slash Commands with Discord.js
Tags: Discord Bot, Slash Commands, Guides And Tutorials, Discord, Discord Js

Step 0: Creating the Bot

If you already have a Discord bot ready to go, you can skip ahead to step 1. Otherwise, we’ll refer you to the Discord.js guide, which has very helpful articles on creating a bot https://discordjs.guide/preparations/setting-up-a-bot-application.html and getting setup with Discord.js https://discordjs.guide/creating-your-bot/

Step 1: Creating the Commands

Before we start, it’s important to ensure that your bot has the applications.commands scope selected when creating the bot invite link. If your bot has already been added, you can use the link below to authorize it again for your guild with the commands scope (make sure to replace your bot ID). Slash commands will only work in guilds with that have this scope until January, when Discord will do it automatically.

https://discord.com/api/oauth2/authorize?client_id=<YOUR_BOT_ID_HERE>&scope=applications.commands

Since slash commands are still in beta, there is no UI yet for creating and managing the commands. Instead, we have to use the Discord API through HTTP requests.

There are two types of slash commands: guild commands and global commands. As implied by the name, guild commands exist per guild, and allow bots to create custom commands for each guild. Global commands, on the other hand, exist in any server that the bot is present in, so they’re handy to use for any general commands related to your bot. For the purposes of this guide we’ll use guild commands, but the process is largely the same when using global commands.

Each slash command is represented by a JSON object that contains data about what Discord should feed the user when they invoke your command. Below is annotated JSON for a sample command that would search GIFs for a specific type of penguin.

If you want to learn more about the command structure, visit this documentation here: https://discord.com/developers/docs/interactions/slash-commands#applicationcommand

Step 2: Registering the Commands

Now that we’ve got our command figured out, we can add it to Discord. This is a one time operation, so you can do it however you like. We’ll show you how to do it both using Insomnia Core, a GUI application, and inside Node.js itself with node-fetch .

To register a command with Discord, we’ll need three things:

The API endpoint Our bot token Our command data

The API endpoint differs if you’re trying to create a guild command or a global command. You’ll need to send a POST request here, with your bot token as the authorization and your command data in JSON as the body.

Global commands:

https://discord.com/api/v8/applications/<bot_id>/commands

Guild commands:

https://discord.com/api/v8/applications/<bot_id>/guilds/<guild_id>/commands

Now that we’ve gathered everything, we can finally create our command!

Using a GUI application:

Add your chosen endpoint to the URL bar at the top. Make sure to enter your bot ID (and guild ID, if applicable).

Then, paste in the JSON you created in step 1 for the body. Make sure the content type is set to JSON so Discord doesn’t return an error. Here’s the sample JSON again for easy copying: