DEV Community

CoolePizza
CoolePizza

Posted on

Register Discord Slash Commands via the Discord REST API

If you've built a small Discord bot and don't want to register commands through your bot code, you can easily register them manually through the Rest API.

Firstly, you need an application to make requests from. I personally use Hoppscotch. Because it's a web application you don't have to install anything. You can also save the requests for later use, which is pretty useful in this case. Of course you can also use a CLI tool like curl. I will use Hoppscotchin this demo.

First, let's start with the Guild Application Commands. According to Discord's documentation, to bulk overwrite the commands, we need to send a PUT request to /applications/{application id}/guilds/{guild}/commands with an array of commands as the body. For test purposes I use this simple payload with just one command. If you want to do something more complicated, take a look at this table in Discord's documentation. This will create a slash Command called test. Keep in mind that there are other types of commands, like message or user commands. If you want to create these, you have to set the type property, the default is set to slash commands so we don't have to set this property.

[
  {
    "name": "test",
    "description": "This is a test command!"
  }
]
Enter fullscreen mode Exit fullscreen mode

Replace the guild and application id in the url with your application data from the dev portal and put https://discord.com/api/v10 in the front (v10 is the api version, as of writing this, the latest is v10). You should get something like this: https://discord.com/api/v10/applications/885151175224201237/guilds/828931295592710195/commands

Next, put it all into Hoppscotch or the tool you're using to create web requests.

Okay, now we've only one thing left. Authorization. Grab your Bot's token and go to the "Authorization" in Hoppscotch. Set Authorization Type to API Key and Pass by to Headers to send your token via the request's headers to discord. Set Authorization as the key and Bot YOUR_BOT_TOKEN as the value.
Image description

Now, set the request method in the top left from GET to PUT and press send. If you got a 200 OK and a Json response body with the data of the commands, everything went well.

If you want to create Global Application Commands, just remove /guilds/{guild id} from the url and you should be fine.

Top comments (0)