Creating Validations

To create validation functions, you can create a file inside of your defined validations directory with any name you'd like.

DJS-Commander allows you to organize your validation files however you want to. Functions inside this folder are executed in ascending order, so you can prioritize your validations however you see fit. Here’s an example of what your file structure could look like:

validations/
└── dev-only.js

Each one of the file inside the validations path is required to export a function by default, similar to the events functions. Your functions will look something like this:

/validations/dev-only.js
module.exports = (interaction, commandObj, handler, client) => {
  if (commandObj.devOnly) {
    if (interaction.member.id !== 'DEVELOPER_ID') {
      interaction.reply('This command is for the developer only');
      return true; // This must be added to stop the command from being executed.
    }
  }
};

A truthy value must be returned from this function if you don't want the command to be executed. This will also ensure that the next validation queued up doesn't get executed. If you directly reply to an interaction from this function (like the above example) make sure to always return true.

Let's go over the different parameters we have access to in this function:

  • interaction: This is the interaction object which is provided when the command was triggered. If you'd like to notify the user early, you can

  • commandObj: This is the information on the command that was triggered. This command object is what's exported from every command file inside the commandsPath

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

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

Since these parameters are not being destructured like with commands, you can feel free to name them whatever you like.

Last updated