Options
All
  • Public
  • Public/Protected
  • All
Menu

Creating commands

Commands are arguably the most important thing a Discord bot can have.

Note: commands have flags which you can access by running this.flags (the flags are a Map!)

To get started with commands, just add a file in your commands folder and use the following example for JavaScript:

const { Command } = require('@alexthemaster/moonlight');

module.exports = class extends Command {
    constructor(...args) {
        super(...args, {
            name: 'name', // defaults to the filename
            description: '',
            disabled: false, // defaults to false
            aliases: ['aliases2'], // aliases go into an array
            nsfw: false, // whether or not this command should only run in a NSFW channel - defaults to false
            canRunInDM: true, // whether or not this command can run in DMs - defaults to true
            ownerOnly: false, // whether or not this command can only be ran by the owner - defaults to false
            cooldown: 5, // the number of seconds before a user can run this command again - defaults to 0
            usage: '', // the usage string for this command to be passed to the argument parser (<> is a required argument, [] is an optional argument; for exact matches use the word you want matched inside the argument types. If you want to specify what the argument should parse to, add a : and the argument type after it (for example: <joke:string> will be a required argument called joke, which needs to be a string); you can also use * before a string to take use the all the text provided by the user (for example: [*rant:string] will be an optional argument called rant, which will process all the text))
            usageDelim: undefined, // the usage delimiter - defaults to undefined (this is the string used to separate an argument from another in usage)
            requiredPermissions: [], // an array of permissions the user is required to have to run the command
            requiredBotPermissions: [] // an array of permissions the bot requires to run the command properly
        });

        // You can also use a function called customizeResponse to customize the response of arguments that failed to parse
        // Example: the usage string is <user:user> but the user that ran the command hasn't mentioned another user or provided an invalid user ID
        // this.customizeResponse('user', "Please provide a user");
    }

    // Args will be an object containing the parsed arguments (for example: if you have <age:number> as the usage string, then args will be { age: theParsedNumber} )
    run(message, args) {

    }

    // This function runs when the bot is first started
    init() {

    }
}

or the following for TypeScript:

import { Command, MoonlightClient, BasePool } from '@alexthemaster/moonlight';
import { Message } from 'discord.js';

export default class extends Command {
    constructor(client: MoonlightClient, pool: BasePool<string, Command>) {
        super(client, pool, {
            name: 'name', // defaults to the filename
            description: '',
            disabled: false, // defaults to false
            aliases: ['aliases2'], // aliases go into an array
            nsfw: false, // whether or not this command should only run in a NSFW channel - defaults to false
            canRunInDM: true, // whether or not this command can run in DMs - defaults to true
            ownerOnly: false, // whether or not this command can only be ran by the owner - defaults to false
            cooldown: 5, // the number of seconds before a user can run this command again - defaults to 0
            usage: '', // the usage string for this command to be passed to the argument parser (<> is a required argument, [] is an optional argument; for exact matches use the word you want matched inside the argument types. If you want to specify what the argument should parse to, add a : and the argument type after it (for example: <joke:string> will be a required argument called joke, which needs to be a string); you can also use * before a string to take use the all the text provided by the user (for example: [*rant:string] will be an optional argument called rant, which will process all the text))
            usageDelim: undefined, // the usage delimiter - defaults to undefined (this is the string used to separate an argument from another in usage)
            requiredPermissions: [], // an array of permissions the user is required to have to run the command
            requiredBotPermissions: [] // an array of permissions the bot requires to run the command properly
        });

        // You can also use a function called customizeResponse to customize the response of arguments that failed to parse
        // Example: the usage string is <user:user> but the user that ran the command hasn't mentioned another user or provided an invalid user ID
        // this.customizeResponse('user', "Please provide a user");
    }

    // Any arguments the event receives will be passed to the run function (for example `message` for the message event)
    public run(message: Message, args: object) {

    }

    // This function runs when the bot is first started
    public init() {

    }
}
abstract

Hierarchy

Index

Constructors

constructor

Properties

Readonly aliases

aliases: string[]

An array of aliases

Readonly canRunInDM

canRunInDM: boolean

Whether or not this command can be called by a user when DM-ing the bot

category

category: string

The category this command is part of

Readonly client

Readonly cooldown

cooldown: number

The amount of time in seconds of the command's cooldown

Readonly customizedResponses

customizedResponses: Map<string, string> = new Map<string, string>()

The map containing the customized responses

Optional Readonly description

description: undefined | string

The description of the piece

disabled

disabled: boolean

Whether or not this piece is disabled

Readonly flags

flags: Map<string, string> = new Map<string, string>()

The flags provided by the user when running the command

name

name: string | undefined

The name of the piece

Readonly nsfw

nsfw: boolean

Whether or not this command can only be run in a nsfw channel

options

options: BasePieceOptions | undefined

Readonly ownerOnly

ownerOnly: boolean

Whether or not this command can only be run by a bot owner

Readonly pool

pool: BasePool<string, Command>

The pool this piece is part of

rawArgs

rawArgs: string[] | undefined

The raw (unprocessed) command arguments

Readonly requiredBotPermissions

requiredBotPermissions: PermissionString[]

The permissions the bot requires to run the command

Readonly requiredPermissions

requiredPermissions: PermissionString[]

The permissions the user is required to have to run the command

Readonly usage

usage: string

The usage string for the command

Readonly usageDelim

usageDelim: string | undefined

The usage delimiter

Methods

customizeResponse

  • customizeResponse(argument: string, response: string): void

disable

  • disable(): void

enable

  • enable(): void

init

  • init(): void

reload

  • reload(): void

run

  • run(_message: Message, ..._arg: any[]): void

Generated using TypeDoc