Discord

Last year, we announced the Future of Bots on Discord. Setting the stage for the work to come, we shared our investment in bots and in supporting more features for the developers who build them. We want to show bots off to the world, and our first step towards that vision was our Verified Bots program: we’ve now verified over 9000 bots, and we couldn’t be more impressed by everything you’ve made.

Our next step is Slash Commands, which will dramatically increase bot accessibility as well as improve the overall user experience of bot interactions.

How does it work?

Until now, everyone using a bot on Discord has interacted with them in the same way: text commands, where a user must type (sometimes guess) whatever the exact command for that bot needs to be in order to work. Often, it might look something like this:

Bots can add an incredible amount of value to servers, and we know they are as important to Discord as text chat, voice channels, gifs, and emojis. That’s why we wanted to make them just as easy and intuitive to use. With Slash Commands, now all you have to do is type a slash “/” to bring up a list of commands that can bot can do!

This means not only will users now be more aware of all the available commands for your bot, but they will also see new functionalities as you add them. In addition, we’ve implemented validation, error states, and helpful UI to ensure they can get it right the first time, especially on mobile (you now have one more ally in the fight against your phone’s autocorrect).

The implementation of Slash Commands also helps increase accessibility — people who use screen readers and other helpful settings will be able to use your bot as easily as they navigate the rest of Discord since Slash Commands are built-in.

Some additional context for Discord Developers

Our community library developers have done an incredible job in helping to make the Discord API more approachable. We want to help make coding on Discord even easier, too. When we started making Discord bots, we all found this kind of “getting started” example:

const client = new Discord.Client();client.on('ready', () => {
 console.log(`Logged in as ${client.user.tag}!`);
});client.on('message', msg => {
 if (msg.content === 'ping') {
   msg.reply('Pong!');
 }
});client.login('token');

Wow, that was easy! But as we built more, we realized it’s not always that simple. Before long, that 10-lines-of-code bot running on our home PC became a monolith of abstracted command handlers and docker configs as we desperately googled “How to increase memory on a VPS” and “What Discord gateway intents do I need?”

Our goal with Slash Commands is to create a user-friendly way to talk to bots, built on top of a powerful, developer-friendly framework that takes away the burden of some of those software struggles.

Validation and User Input

Slash Commands come with built-in type validation and argument handling. You can define arguments, types, and even custom options. You no longer have to worry about checking if an ID is a user or a channel — or teaching users how to get IDs in the first place — or creating an elaborate system of fuzzy-matching names so that moderators can pick between banning Mason#1337 and Máson#1337.

Need a user? type 6: USER. Need a channel, role, integer, or string? There's a type for that.

{
   "type": 2,
   "token": "A_UNIQUE_TOKEN",
   "id": "786008729715212999",
   "guild_id": "290926798626357999",
   "channel_id": "645027999969510667",
   "data": {
       "name": "usersearch",
       "id": "771825006099989984",
       "options": [
         {
           "name": "user",
           "value": "53908232506183680"
         },
       ],
       "resolved": {
         "users": {
           "53908232506183680": {
               "id": "53908232506183680",
               "username": "Mason",
               "avatar": "a_d5efa99b3eeaa7dd43acca82f5692432",
               "discriminator": "1337",
               "public_flags": 131141
           }
         }
       }
   }
}

Discoverability

Your bot has a /play command, that other bot in your server has a /play command. Every bot has a /play command. Eventually, we're going to run out of !, ~, and - to tell one command apart from another, and what happens when three bots get the same command?

Because bots receive Slash Commands as specific events and not by reading chat messages, you and your users can take the guesswork out of what bot a command is supposed to be. Autocomplete helps users search for and filter all commands on a server, meaning it’s easier to find your /play command and not someone else's.

Scalability

For those of you running large Discord bots, you know that hosting, uptime, and scalability aren’t free. Gateway connections can be expensive to maintain, and rolling out new code means eating into your IDENTIFY limits for the day.

While Slash Commands work over the gateway like any other event, you can choose to receive them as outgoing webhooks instead.

from flask import Flask, jsonify, request
app = Flask(__name__)@app.route('/', methods=['POST'])
def hello_world():
 # Ping/Pong    
 if request.json['type'] == 1:
   return jsonify({
       "type": 1
   })

 return jsonify({
     "type": 4,
     "data": {
         "content": "Hello world!"
     }
 })

Responding to Slash Commands is also done with webhooks with unique tokens per interaction, meaning you can respond to users immediately instead of waiting for your send_messagerate limit to be up. And, when you have new code to deploy, you can simply restart your webserver instead of reconnecting hundreds of shards.

Interactions: the bigger picture

It’s time to let you in on a secret: we implemented a whole new kind of API right under your nose. Slash Commands are one of many up-and-coming features built on top of a new part of our API that we call “Interactions.” These are events triggered by users interacting with your bot — they’re standardized and separated from any individual feature or UI element (in other words, we can build a whole lot more than Slash Commands, and you get to reap the benefits with little additional work).

Interactions are accessible over both the gateway and outgoing webhooks, meaning that if you don’t need a persistent gateway connection, you don’t have to maintain one. “But wait, how do you have a bot without a gateway connection?” Great question! What if you didn’t need a bot? You can read more about how bots and interactions work together here.

We’re still hard at work making more interaction-based features for you. We’ll be keeping a close eye on how you all use them, and as always we love to hear from you.

What’s Next?

Slash Commands are live on Desktop, Android, and iOS. Head over to the API documentation and learn about making Slash Commands for your app right now!

If you’re looking for some inspiration, check out what some of your favorite bots have already done so far:

If you’re interested in learning more, make sure you’ve joined our official Discord Developer Server, where we share the latest news, answer questions about Discord’s API, and sometimes even provide sneak peeks of upcoming features.

Ready? Go get started!

Contents
THE AUTHOR
MORE FROM