Each command requires a bit of boilerplate code. This means that basic simple commands are longer than in simpler bots, but the more advanced one's with multiple arguments and type checking are simpler.
A basic command may look like this:
const commando = require("@iceprod/discord.js-commando");
module.exports = class ExampleCommand extends commando.Command {
constructor(client) {
super(client, {
name: "example",
memberName: "example",
group: "essentials"
});
}
run(msg) {
msg.say("You use the example command!");
}
}
The default boilerplate is a simple class with a constructor that calls super with first parameter same (client
) and command options as the second.
I'll cover only the basics, for more advanced type docs, see discord.js.org.
This must be a unique name (it won't load otherwise), without spaces and in lowercase. Regex: [a-z0-9-_]
.
This must be a name of registered group, you usually just use the folder name. Same restrictions as for name.
For some reason, it's required in base commando (and I didn't change it), it has same restrictions as name except it's needed to be unique in group scope, not globally. My practice is just to use the same name.
An array of strings of aliases that can be used to invoke the command. Same restrictions as for name.
Args are array of Argument objects, their basic form is as follows:
{
key: "exampleArgument",
type: "string",
prompt: "What should be the value of example argument?"
}
Key is used as the name of property in cmd
object (the second argument to run
method). It can be anything JS permits, but I recommend going with a valid variable name so you can destructure it (for readability).
Type must be one of the registered types. Default builtins can be found here, I added url
(returns instance of URL
) and time-argument
which returns time object (see types/time-argument.js).
Prompt is the text to be asked when user doesn't fill the argument (or fills it with wrong value). Commando will append more information after it, so just make sure the user knows what they should reply with.
Run method accepts two arguments - Commando message and Args object, mapping key
to their value. (Note that this isn't all, see Command for full typedoc)
This is a normal discord.js message, with added fields from commando and alias
property, specifying the alias used.
Arguments is a simple object mapping the key
of arguments to their value specified by user.
{
[key: string]: string
}