npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

slash-command-builder

v0.1.1

Published

Create a JSON structure to send to the Discord API and register your Slash Commands

Downloads

16

Readme

Slash Command Builder

Create a JSON structure to send to the Discord API and register your Slash Commands. TypeScript supported.

For more information, read Application Commands in the Discord documentation API.

Features

Types: Integer Number String with choices structure, this field (choices) using addChoice() or addChoices() is optional.

Specification in the Application Command Option Type section.

Note: to register subcommands and subcommand groups, read the nesting support in the documentation.

Installation

Node.js 14 or newer is required.

yarn add slash-command-builder

Or with NPM

npm i -S slash-command-builder

Getting Started

Create a new instance of SlashCommandBuilder class:

import SlashCommandBuilder from 'slash-command-builder';

const builder: SlashCommandBuilder = new SlashCommandBuilder();

builder.setName('info') // Using `/info`
builder.setDescription('command description')

Registering Application Commands

Using @discordjs/rest discord-api-types libraries.

import { REST } from  '@discordjs/rest';
import { Routes } from  'discord-api-types/v9';

const rest: REST = new REST({ version: '9' }).setToken('BOT_TOKEN');

(async () => {
  // Global Commands
  await rest.put(
    Routes.applicationCommands('BOT_APP_ID'),
    { body: [builder.toJSON()] },
  )

  // Or specific Guild
  await rest.put(
    Routes.applicationGuildCommands('BOT_APP_ID', 'GUILD_ID'),
    { body: [builder.toJSON()] },
  )
})();

To register multiple application commands:

// ...

const builderCommand_1: SlashCommandBuilder = new SlashCommandBuilder();
const builderCommand_2: SlashCommandBuilder = new SlashCommandBuilder();
const builderCommand_3: SlashCommandBuilder = new SlashCommandBuilder();

// ...

(async () => {
  await rest.put(
    ...,
    {
      body: [
        builderCommand_1.toJSON(),
        builderCommand_2.toJSON(),
        builderCommand_3.toJSON(),
      ],
    },
  )
})();

Add Options

All these options including methods:

  • builder.setName(name: string) Set a name.
  • builder.setDescription(description: string) Set a description.
  • builder.setRequired(required: boolean) (Optional) This option is required or not.

And his respective properties:

  • builder.name
  • builder.description
  • builder.required

Note: Required options must be listed before optional options

Using previous instance builder:

Option: Boolean

import { SlashCommandBooleanOption } from 'slash-command-builder';

builder.addBooleanOption((option: SlashCommandBooleanOption): SlashCommandBooleanOption => (
  option
    .setName('bool')
    .setDescription('Option description')
    // .setRequired(true) // This option is required
));

Option: Channel

import {
  ChannelTypes,
  SlashCommandChannelOption,
} from 'slash-command-builder';

builder.addChannelOption((option: SlashCommandChannelOption): SlashCommandChannelOption => (
  option
    .setName('channel')
    .setDescription('Option description')
    // .setRequired(true) // This option is required

	// Filter channels
    .addFilterBy(ChannelTypes.GUILD_NEWS)
    .addFilterBy(ChannelTypes.GUILD_CATEGORY)

    // Using array
    .addFilterBy([ChannelTypes.GUILD_TEXT])

    // Final result:
    // [ChannelTypes.GUILD_NEWS, ChannelTypes.GUILD_CATEGORY, ChannelTypes.GUILD_TEXT]
));

Note: the method addFilterBy can be chained multiple times.

Option: Integer

import { SlashCommandIntegerOption } from 'slash-command-builder';

builder.addIntegerOption((option: SlashCommandIntegerOption): SlashCommandIntegerOption => (
  option
    .setName('integer')
    .setDescription('Option description')
    // .setRequired(true) // This option is required

	// Add choices
    .addChoice('Choice #1', 1)
    .addChoice('Choice #2', 2)

    // Using array
    .addChoices([
      { name: 'Choice #3', value: 3 },
      { name: 'Choice #4', value: 4 },
    ])

    // Final result:
    [
      { name: 'Choice #1', value: 1 },
      { name: 'Choice #2', value: 2 },
      { name: 'Choice #3', value: 3 },
      { name: 'Choice #4', value: 4 },
    ]
));

Note: the methods addChoice and addChoices can be chained multiple times.

Option: Mentionable

import { SlashCommandMentionableOption } from 'slash-command-builder';

builder.addMentionableOption(
  (option: SlashCommandMentionableOption): SlashCommandMentionableOption => (
    option
      .setName('mentionable')
      .setDescription('Option description')
      // .setRequired(true) // This option is required
  )
);

Option: Number

import { SlashCommandNumberOption } from 'slash-command-builder';

builder.addNumberOption(
  (option: SlashCommandNumberOption): SlashCommandNumberOption => (
    option
      .setName('number')
      .setDescription('Option description')
      // .setRequired(true) // This option is required

	// Add choices
    .addChoice('Choice #1', 1.1)
    .addChoice('Choice #2', 2.2)

    // Using array
    .addChoices([
      { name: 'Choice #3', value: 3.3 },
      { name: 'Choice #4', value: 4.4 },
    ])

    // Final result:
    [
      { name: 'Choice #1', value: 1.1 },
      { name: 'Choice #2', value: 2.2 },
      { name: 'Choice #3', value: 3.3 },
      { name: 'Choice #4', value: 4.4 },
    ]
  )
);

Option: Role

import { SlashCommandRoleOption } from 'slash-command-builder';

builder.addRoleOption(
  (option: SlashCommandRoleOption): SlashCommandRoleOption => (
    option
      .setName('role')
      .setDescription('Option description')
      // .setRequired(true) // This option is required
  )
);

Option: String

import { SlashCommandStringOption } from 'slash-command-builder';

builder.addStringOption((option: SlashCommandStringOption): SlashCommandStringOption => (
  option
    .setName('string')
    .setDescription('Option description')
    // .setRequired(true) // This option is required

	// Add choices
    .addChoice('Choice #1', 'option:1')
    .addChoice('Choice #2', 'option:2')

    // Using array
    .addChoices([
      { name: 'Choice #3', value: 'option:3' },
      { name: 'Choice #4', value: 'option:4' },
    ])

    // Final result:
    [
      { name: 'Choice #1', value: 'option:1' },
      { name: 'Choice #2', value: 'option:2' },
      { name: 'Choice #3', value: 'option:3' },
      { name: 'Choice #4', value: 'option:4' },
    ]
));

Note: the methods addChoice and addChoices can be chained multiple times.

Option: User

import { SlashCommandUserOption } from 'slash-command-builder';

builder.addUserOption(
  (option: SlashCommandUserOption): SlashCommandUserOption => (
    option
      .setName('user')
      .setDescription('Option description')
      // .setRequired(true) // This option is required
  )
);

Option: Subcommand

import { SlashCommandSubcommand } from 'slash-command-builder';

builder.addSubcommand(
  (sub: SlashCommandSubcommand): SlashCommandSubcommand => (
    sub
      .setName('subcommand')
      .setDescription('subcommand description')

      // Add options
      .addStringOption(...)
      .addUserOption(...)
  )
);

Option: SubcommandGroup

import { SlashCommandSubcommandGroup } from 'slash-command-builder';

builder.addSubcommandGroup(
  (group: SlashCommandSubcommandGroup): SlashCommandSubcommandGroup => (
    group
      .setName('subcommand-group')
      .setDescription('subcommand-group description')

      // Multiple subcommands
      .addSubcommand(
        (sub: SlashCommandSubcommand): SlashCommandSubcommand => (
          sub
            .setName('subcommand-one')
            .setDescription('subcommand-one description')

            // Add options
            .addStringOption(...)
            .addUserOption(...)
      )
      .addSubcommand(
        (sub: SlashCommandSubcommand): SlashCommandSubcommand => (
          sub
            .setName('subcommand-two')
            .setDescription('subcommand-two description')

            // Add options
            .addRoleOption(...)
            .addMentionableOption(...)
      )
    )
  )
);

Interfaces/Types

  • ApplicationCommandInteractionDataOptionStructure
  • ApplicationCommandJSON
  • ApplicationCommandOptionChoiceStructure
  • ApplicationCommandOptionStructure
  • ApplicationCommandOptionTypes
  • ApplicationCommandStructure
  • ApplicationCommandTypes
  • ChannelTypes
  • Choices
  • Snowflake

Contributing

Feel free to report any bug by creating an Issue or a Pull Request. It will be much appreciated! :D