How To Add Slash Commands to Your Discord Bot

Hero Image

According to the Verge, “Discord is quietly building an app empire of bots” and they’ve recently added Slash Commands as a cornerstone of their bot and messaging infrastructure.

Slash Commands are an extremely powerful way to provide rich interactivity for members of your Discord server, all you have to do is type “/” and you're ready to use your favorite bot. You can easily see all the bot’s commands, input validation, and error handling help you get the command right the first time.

In this blog post, I'll walk you through how to configure, register and handle Slash Commands for your own Discord bot through Fusebit.

Setting Up Your Discord Slash Command Bot

To get started, you will need to get two housekeeping items out of the way.

First, create a Fusebit account below and keep this blog post open in a separate tab.

Create Free Fusebit Account

Second, you will need to set up your own Discord App and Bot. Follow our developer guide to see how to set one up and configure it for Fusebit.

Note: For this integration to work, you will need to configure your own discord application and bot with the following scopes: applications.commands,identify, incoming.webhook, bot and set the bot permissions=2147486720

How To Test That Everything Is Configured Correctly

To demonstrate how to send a message to a channel through the Discord API using channel webhooks, we mocked out a simple ‘test’ endpoint that you can invoke right from inside the Fusebit code editor.

You will need to login to your Discord before successfully running this integration for the first time. Once you have successfully authorized, you will receive a message in the Discord channel you selected to receive incoming webhook messages from.

discord slash commands

You will also see a message in your console letting you know that the message was successfully delivered to Discord.

discord slash commands server with-shadow

In the test endpoint, we reference a discordClient.

const discordClient = await integration.tenant.getSdkByTenant(
ctx,
discordConnector,
ctx.params.tenantId
);

This is the SDK client used to invoke the Discord endpoints. Because of Fusebit magic, those objects are already supplied with the right credentials (such as API Keys and API Tokens), based on the authorization you completed earlier.

Now that those items are out of the way, there are two main parts to building a custom Discord slash command bot:

  1. Configure & Register a Discord Slash Command
  2. Listen for & Respond to a Discord Slash Command

Let’s get started below.

Configure & Register a Discord Slash Command

How To Configure your Slash Command

Slash Commands are typically one level, but Discord enables developers to make more organized and complex groups of application commands that can be one level deep within a group. Learn more about this through the Discord Developer Portal here.

In this blog post, we will walk through how to register a single Slash Command only and you can do that by using the configureSlashCommand() function.

In this example, we are using ‘command’ as the command name, along with parameterOne and parameterTwo as the command options, you will retrieve these values later in this tutorial.

function configureSlashCommand() {
const command = {
name: 'command',
description: 'Command that gets triggered',
type: 1,
options: [
{
name: 'parameterOne',
description: 'First parameter of the Command',
type: 3,
required: true,
},
{
name: 'parameterTwo',
description: 'Second parameter of the Command',
type: 3,
required: true,
},
],
};
return command;
}

Learn more about the different option types here.

How To Register your Slash Command

Once you are done configuring your Slash Command, Discord allows you to also register your Slash Commands as guild commands as well as globally to all users. This can be helpful for testing your commands locally before making them available to everyone.

You can simply register your Slash Command to your specific guild by running the router.post('/api/tenant/:tenantId/:guild/slash-command' endpoint in line 47 directly from the Run button.

If you need to find your Guild ID, here’s a quick guide.

discord slash commands

If you wish to register your Slash Command globally for all users of your Integration, you can run the router.post('/api/tenant/:tenantId/slash-command' endpoint instead.

Listen for & Respond to a Discord Slash Command

How To Listen for Slash Commands

Nothing to do here! If you configured your app correctly through the developer docs, Fusebit will automatically listen for Slash Commands from servers where your app was authenticated against.

Look for the integration.event.on('/:componentName/webhook/:eventType') endpoint in line 75 to follow this.

Responding to Slash Commands

Discord sends an Application ID & Message Token that can be used to track a series of interactions for the same message and send follow up messages to the Discord user.

integration.event.on('/:componentName/webhook/:eventType', async (ctx) => {
const {
data: { data: event, application_id, token },
} = ctx.req.body;

const [parameterOne, parameterTwo] = event.options;
const responseMessage = `You sent me ${parameterOne.value} and ${parameterTwo.value} as your parameters!`;

// Read more about interactions here: https://discord.com/developers/docs/interactions/receiving-and-responding
await superagent
.post(`https://discord.com/api/v8/webhooks/${application_id}/${token}`)
.send({ content: responseMessage });
});

In the code here, we’re extracting the parameters that we had specified as name values in the options while configuring the Slash Command and sending back a response in the form of a Discord message object.

Done! You should now have a good understanding of how to add your own Discord Slash Commands for your bot and to reply with the appropriate context!.

Pro Tip: You can also add more Connectors such as Github, Linear, etc., and work directly with their REST APIs within the same Integration.

Other Discord Bot Integration Examples

With Fusebit, it is easy to build a Discord bot that integrates deeply with any of our available connectors. Here are a few blog posts showing how to build specific integrations with advanced configurations and options such as context menus, rich embedded messages and more!

PagerDuty + Discord Integration

How To Get More Discord and Developer Tips

If you are looking to create flexible and powerful integrations using other platforms like Slack, Salesforce and GitHub, check out Fusebit to get started, and follow us on Twitter @fusebitio for our daily developer posts.


ArrowPrevious
NextArrow

Most Recent

15 September 2022
What Is Deno and Why You Should Try It

Learn the basics of what the Deno runtime environment is all about, and highlight the key features, benefits, and detractions.

1 September 2022
How to Implement GitHub OAuth in Your Node.js App

OAuth is a standard protocol used for authorization. This post will discuss implementing GitHub OAuth in your Node.js app.

31 August 2022
QuickBooks OAuth: How to Set up the Online Accounting API in Your Web App

Intuit uses OAuth2 to authorize applications for access to company data. Here's how to use their library to authorize your Node app.