Modern new way to use Discord.js command options (Robo.js)

You can now access your Discord bot's slash command options as a simple object in command functions, as of Robo.js v0.10!

We believe this is more straightforward and easier to understand than before. Keep in mind, you can still use the old way if you prefer!

Quick comparison

Before (v0.9 and below)

You had to access the options from the interaction object:

export const config = {
    description: 'Responds with Pong!',
    options: [
        {
            name: 'loud',
            description: 'Respond loudly?',
            type: 'boolean'
        }
    ]
}

export default (interaction) => {
    const loud = interaction.options.get('loud')?.value
    return loud ? 'PONG!!!' : 'Pong!'
}

After (v0.10 and above)

Directly access the options from the second parameter:

export const config = {
    description: 'Responds with Pong!',
    options: [
        {
            name: 'loud',
            description: 'Respond loudly?',
            type: 'boolean'
        }
    ]
}

export default (interaction, options) => {
    return options.loud ? 'PONG!!!' : 'Pong!'
}

Don't worry, you can still get the options from the interaction object if you prefer. All existing code will work the same. The new way is just a more convenient and modern approach.

TypeScript support

The new way also has full TypeScript support! This gets you better type checking and autocompletion in your editor, so you know exactly what options are available for your command.

import { createCommandConfig } from 'robo.js'
import type { CommandOptions, CommandResult } from 'robo.js'
import type { CommandInteraction } from 'discord.js'

export const config = createCommandConfig({
    description: 'Responds with Pong!',
    options: [
        {
            name: 'loud',
            description: 'Respond loudly?',
            required: true,
            type: 'boolean'
        },
        {
            name: 'text',
            description: 'The text to respond with',
            type: 'string'
        }
    ]
} as const)

export default (interaction: CommandInteraction, options: CommandOptions<typeof config>): CommandResult => {
    const { loud, text = 'Pong!' } = options
    return loud ? text.toUpperCase() : text
}
Notice the createCommandConfig function and as const keyword. These are important for TypeScript to infer types correctly.

The options object is so smart, that it will tell you not only the name and type of the option, but also whether it may be undefined or not, depending on the option's required property.

What is Robo.js?

Oh yeah, if you don't already know, Robo.js is a powerful framework for building Discord activities, bots, web servers, and more. It's designed to be simple, yet powerful, with a focus on developer experience and modern JavaScript/TypeScript features.

Bot development is just Discord.js, but with a sprinkle of Robo magic on top. Existing Discord.js code works with Robo.js because it is Discord.js. The main difference is that Robo.js provides a more structured way to build your bot, without boilerplate, and following best practices by default.

It's also super easy to use! For example, you can code an entire Discord bot with just one line of code:

// src/command/ping.js
export default () => 'Pong!'

The above is all you need to create a /ping command that responds with "Pong!". Robo.js takes care of the rest, including registering the command with Discord, handling interactions, and more. You're not limited by what you can do with Robo.js, but you're empowered to do more with less code.

Ready to get started?

Creating a new Robo.js project is as simple as running:

npx create-robo my-project

This will guide you through setting up your project. Check out our docs and tutorials for more!

🚀 Community: Join our Discord server

📚 Documentation: Getting started

📖 Tutorial: Creating a Discord Activity in seconds