It is common for the discord bot libraries to expect having an active bot hosted on a server, also known as a gateway bot. One of the valuable things about slash commands is that it doesn’t require that gateway bot connection and persistence. It can use the alternative approach of providing an API endpoint that the command can hit.
- You need to create a bot for some of the permissions.[^1] Let’s assume we will use those permissions and create on. Click the Bot option on the sidebar and create a bot. It seems easier to just use a bot in my case since I will want to hit those APIs eventually.
- You also need to authorize the bot to set commands. Go to bot and OAuth2, generate a url and visit it. Choose to
bot
andapplications.commands
checkboxes, and it should produce a url for you to visit. - We need to set our function to verify the signature is coming from discord. They have a library that can help with this. Note, if you don’t do this, they will remove your command. (https://discord.com/developers/docs/interactions/slash-commands#security-and-authorization)
- We need to reply with a ping/pong
{type: 1}
so discord knows that it is active. If it fails security or is not active, then the command will be removed. - We need to host the endpoint somewhere so it is able to respond with the “pong”. I did it through Netlify in my specific case. For testing purposes, I used
netlify dev --live
to start a server and test out the bot. - The bot needs the endpoint url in the developer portal. We can use the test one for now, and swap it out later. It can also be removed for it to switch back to the “gateway API” version of the bot.
- We need to post the JSON so discord can offer up the /command options within the client.
- The command should be active!
https://discord.com/developers/applications https://discord.com/developers/docs/interactions/slash-commands