Skip to content
This repository was archived by the owner on Oct 13, 2022. It is now read-only.

Ciensprog/slash-command-builder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

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

Topics

Resources

License

Stars

Watchers

Forks