Using JSON to register application commands and add options

Hi there,

Is there a way to add all of my application command options in the registerApplicationCommands method on the Command class without using builders?

For example, a registerApplicationCommands method might look like this currently, to register options:

    public override registerApplicationCommands(registry: Command.Registry) {
        registry.registerChatInputCommand((builder) =>
            builder //
                .setName(this.name)
                .setDescription(this.description)
                .addSubcommand((subcommand) =>
                    subcommand
                        .setName("register")
                        .setDescription(
                            "Registers your friend code so you can share it with others."
                        )
                        .addStringOption((option) =>
                            option
                                .setName("code")
                                .setDescription("Your friend code.")
                                .setRequired(true)
                        )
                )


---

My use case is that I have a constant array of settings I'd like the user to be able to configure, and it would be nice if I could do something like automatically add a non-required slash command option for each setting. Then the command could look something like /settings set setting1 setting2 setting3 ...
Solution
Builders serialise to JSON and you can also provide that same JSON.

// Registering with the discord.js options object
registry.registerChatInputCommand({
  name: 'uwu',
  description: 'Sends a uwu in chat'
});

// Registering with the builder
const builder = new SlashCommandBuilder().setName('uwu').setDescription('Sends a uwu in chat');
registry.registerChatInputCommand(builder);
Was this page helpful?