Commands
What do commands do?
Commands are the main way to interact with your plugins. When executing a command as normal, we intercept it, and check it against your registered commands.
What is a Command Context?
The CommandContext is the primary way of parsing input from the user for the command. It has the following functions available:
CommandContext#string(): Returns the next argument as a string.CommandContext#number(): Returns the next argument as a number.
WARNING
If an argument fails to parse, it will simply tell the client that the command failed, and will not complete the execution.
How do I register a command?
Registering a command is very simple, and can be done in a few lines of code.
ts
// The name can have spaces, but is recommended to be a single word.
Command.register("name", (ctx) => {
console.log("Hello, World.")
})How can I get input?
We provide a series of additional functions to help you get input from your CommandContext.
ts
Command.register("identity", (ctx) => {
const name: string = ctx.string();
const age: number = ctx.number();
console.log(`Hello, ${name}. You are ${age} years old.`)
})API Specification
ts
declare namespace Command {
/**
* The context that will be provided when issuing a command.
*/
type CommandContext = {
/**
* Parse a {@link string} from the {@link CommandContext}, or throw a parsing error.
*/
string(): string;
/**
* Parse a {@link number} from the {@link CommandContext}, or throw a parsing error.
* If the argument is not a number, it will throw an error.
*/
number(): number;
}
/**
* Register a command that can be executed from the command line.
* @param name The name of the command. This can be a single word, or multiple words seperated by spaces.
* @param callback The callback to execute when the command is run, you will have access to the {@link CommandContext}.
*/
function register(name: string, callback: (context: CommandContext) => void): void;
}
