Creating Commands

Any file you create inside your provided commandsPath will be counted as a command file. It doesn't matter how nested it is. Take this for example:

commands/
└── ping.js

I've created a ping.js file inside of my defined commands path. This file is expected to export an object by default that looks something like this.

commands/ping.js
const { SlashCommandBuilder } = require('discord.js');

module.exports = {
  data: new SlashCommandBuilder()
          .setName('ping')
          .setDescription('Pong!'),
          
  run: ({ interaction, client, handler }) => {
    interaction.reply(`Pong! ${client.ws.ping}ms`);
  },
};

Your command file is expected to export an object with a data property which is the structure of the command itself, and a run method which is called when this command is triggered.

If you don't like using the Slash Command Builder (🙋‍♂️), then you can directly create an object that looks something like this.

Run Method Parameters

You can destructure interaction, client, and handler from the run method to help handle your command. Here's more information on each one of them.

  • interaction: This is the interaction object returned by the interactionCreate event listener.

  • client: This is your Discord.js Client instance which was defined in the main entry point of your project.

  • handler: This is the DJS-Commander CommandHandler instance which was also defined in the main entry point of your project.

Last updated