Command Arguments

Command arguments are options where the user inputs their data which can help when the bot is doing actions with the command.

minArgs and maxArgs

minArgs (Minimum Arguments) and maxArgs (Maximum Arguments) help the bot to understand how many arguments can be placed by the user when running a command. There are helpful when specifying values with expectedArgs.

What is expectedArgs?

expectedArgs (Expected Arguments) is used to specify the arguments' names, and there are also annotations which you will need to apply.

["<", "arg_name", ">"] // Required Arguments | Example: "<text>"
["[", "arg_name", "]"] // Optional Arguments | Example: "[text]"

Let's create a new command "add" to show how arguments can be used in a command.

add.js
const { NoCliCommandType } = require("nocli-handler.js");

/**
 * @type {import("nocli-handler.js").ICommand}
 */
const Command = {
    type: NoCliCommandType.Legacy,
    description: "Adds numbers together and prints out the sum",
    testOnly: true,
    minArgs: 2,
    maxArgs: 3,
    expectedArgs: "<num1> <num2> [num3]",
    callback: ({ client, message, interaction, args, text }) => {
        let sum = 0;
        for (const arg of args) {
            sum += parseInt(arg);
        }
        return 'The sum is ' + sum;
    }
}

module.exports = Command;

This "add" command adds numbers together, meaning the first and second arguments are required. So we set minArgs to 2, and num1 and num2 arguments will be wrapped in <>. Then, we will set maxArgs to the number of arguments specified in expectedArgs.

Don't worry if you are not used to this. You can apply using options (if it seems easier to you). However, this only applies to Slash Commands.

Once done, run the program, and test it out.

Last updated