Skip to main content

NPM version NPM downloads ESLint status DeepScan grade discord chat

Creator and handler for Discord’s slash commands.

You can create commands similar to Discord.JS Commando.

Features

  • Multiple server support (Express, Fastify, etc.)
  • Hook into an existing Discord bot client
  • Command syncing - Sync commands with your creator automatically.
  • Load commands from a folder
  • Command throttling/cooldowns

Installation

npm i slash-create
yarn add slash-create

Using webservers

In order to use a specific webserver, you will need to install the dependency associated with that server. The following server types require these dependencies:

Example

Creating a SlashCreator

const { SlashCreator } = require('slash-create');
const creator = new SlashCreator({
  applicationID: '12345678901234567',
  publicKey: 'CLIENT_PUBLIC_KEY',
  token: 'BOT_TOKEN_HERE',
});

Adding commands and syncing them

const path = require('path');

creator
    // Registers all of your commands in the ./commands/ directory
    .registerCommandsIn(path.join(__dirname, 'commands'))
    // This will sync commands to Discord, it must be called after commands are loaded.
    // This also returns itself for more chaining capabilities.
    .syncCommands();

Adding a webserver

const { ExpressServer } = require('slash-create');
const path = require('path');

creator
    .withServer(new ExpressServer())
    // Depending on what server is used, this may not be needed.
    .startServer();

/**
 * By default, this serves to `127.0.0.1:80/interactions`.
 * You can change the `serverPort` and `endpointPath` to affect where to serve to.
 */

/**
 * You can also initialize the server with an existing application.
 * If you are doing this with express applications, the express application must already have `express.json()` as middleware.
 */

creator
    // Set `alreadyListening` if the server has already started.
    .withServer(new ExpressServer(app, { alreadyListening: true }));

Using a Discord Bot with /create

const { GatewayServer } = require('slash-create');
const Discord = require('discord.js');
const client = new Discord.Client();

creator
  .withServer(
    new GatewayServer(
      (handler) => client.ws.on('INTERACTION_CREATE', handler)
    )
  );

client.login('BOT_TOKEN_HERE');

Example Command

const { SlashCommand } = require('slash-create');

module.exports = class HelloCommand extends SlashCommand {
  constructor(creator) {
    super(creator, {
      name: 'hello',
      description: 'Says hello to you.'
    });
    this.filePath = __filename;
  }

  async run(ctx) {
    return `Hello, ${ctx.member.displayName}!`;
  }
}
Resources & References

This project borrows resources and references from the following repositories: