Skip to content

Instantly share code, notes, and snippets.

@danbulant
Created November 7, 2020 10:39
Show Gist options
  • Save danbulant/5f8a6ca79e16eb7595c1cabc38ea33d3 to your computer and use it in GitHub Desktop.
Save danbulant/5f8a6ca79e16eb7595c1cabc38ea33d3 to your computer and use it in GitHub Desktop.
Commando command structure

Command structure

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.

Options

Name

This must be a unique name (it won't load otherwise), without spaces and in lowercase. Regex: [a-z0-9-_].

Group

This must be a name of registered group, you usually just use the folder name. Same restrictions as for name.

Member 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.

Aliases

An array of strings of aliases that can be used to invoke the command. Same restrictions as for name.

Args

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

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

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

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

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)

Message

This is a normal discord.js message, with added fields from commando and alias property, specifying the alias used.

Arguments

Arguments is a simple object mapping the key of arguments to their value specified by user.

{
    [key: string]: string
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment