Argument Validation

The "validate" property may be slightly confusing to new users, so we decided to explain it with its own section! The validate property will accept a class called "Validator", this class provides 3 functions that you can customize to your liking, "validate", "onSuccess", and "onError", each performing their own actions. Out of the three, onSuccess is optional, and the other two are required for functionality.

JavaScript
TypeScript
JavaScript
// ./commands/Ping.js
const { Command, Validator } = require("cdcommands");
module.exports = new Command({
...commandOptions,
validate: new Validator({
validate: ({ message, args, client, prefix, language }) => {
if (args[0] !== "test") return "INCORRECT_ARGS";
},
onError: ({ error, message, args, client, prefix, language }) => {
if (error === "INCORRECT_ARGS")
message.channel.send('args[0] was not equal to "test"');
},
onSuccess: (message) => {
console.log('Command "ping" was run successfully!');
},
}),
});
TypeScript
// ./commands/Ping.ts
import { Command, Validator } from "cdcommands";
import { Message } from "discord.js";
export default new Command({
...commandOptions,
validate: new Validator({
validate: ({ message, args, client, prefix, language }) => {
if (args[0] !== "test") return "INCORRECT_ARGS";
},
onError: ({ error, message, args, client, prefix, language }) => {
if (error === "INCORRECT_ARGS")
message.channel.send('args[0] was not equal to "test"');
},
onSuccess: (message: Message) => {
console.log('Command "ping" was run successfully!');
},
}),
});

Replacing the validate property shown previously with the one shown above will now validate your command! Now if you don't provide your first argument as "test", you will get a response saying args[0] was not equal to "test". You now must run your command as ?ping test or ?pong test.