Class: Command

Command

Manages the command event firing for all commands. This is not a normal submodule, and is treated differently in the SpikeyBot class.

new Command()

Source:

Extends

Classes

CommandSetting
SingleCommand

Members


<nullable> bot :SpikeyBot

The parent SpikeyBot instance. Defaults to required cache value for autocompletion, updates to current reference at init.
Type:
Inherited From:
Source:

client :Discord~Client

The current bot client. Defaults to require cache value for editor autocompletion, updates to current reference at init.
Type:
  • Discord~Client
Inherited From:
Source:

command :Command

The command object for registering command listeners. Defaults to require cache value for editor autocompletion, updates to current reference at init.
Type:
Inherited From:
Source:

CommandSetting

Source:
See:

<constant> commit :string

The commit at HEAD at the time this module was loaded. Essentially the version of this submodule.
Type:
  • string
Inherited From:
Source:

common :Common

The common object. Defaults to require cache value for editor autocompletion, updates to current reference at init.
Type:
Inherited From:
Source:

deleteEvent

Alias for Command.removeListener.
Deprecated:
  • Yes
Source:

Discord :Discord

The current Discord object instance of the bot. Defaults to require cache value for editor autocompletion, updates to current reference at init.
Type:
  • Discord
Inherited From:
Source:

<abstract> helpMessage :undefined|string|Discord~EmbedBuilder

The help message to show the user in the main help message.
Type:
  • undefined | string | Discord~EmbedBuilder
Inherited From:
Source:

<protected, readonly> initialized :boolean

Has this subModule been initialized yet (Has begin() been called).
Type:
  • boolean
Inherited From:
Source:

<constant> loadTime :number

The time at which this module was loaded for use in checking if the module needs to be reloaded because the file has been modified since loading.
Type:
  • number
Inherited From:
Source:

<protected> myName :string

The name of this submodule. Used for differentiating in the log. Should be defined before begin().
Type:
  • string
Inherited From:
Overrides:
Source:

<abstract> postPrefix :string

The postfix for the global prefix for this subModule. Must be defined before begin(), otherwise it is ignored.
Type:
  • string
Inherited From:
Source:

SingleCommand

Source:
See:

<private, inner> cmds :object.<SingleCommand>

All tracked commands mapped by command name.
Type:
  • object.<SingleCommand>
Source:

<private, inner, constant> commandSettingsFile :string

Filename in the guild's subdirectory where command settings are stored.
Type:
  • string
Default Value:
  • /commandSettings.json
Source:

<inner> eventList

Currently registered event listeners for non-command events.
Source:

<private, inner, constant> onlyservermessage :string

The message to send to the user if they attempt a server-only command in a non-server channel.
Type:
  • string
Source:

<private, inner, constant> userSettings :object.<object.<CommandSetting>>

Specific settings defined by users as restrictions on commands. Mapped by guild id, then by the command.
Type:
  • object.<object.<CommandSetting>>
Source:

Methods


addEventListener(name, handler)

Register an event listener.
Parameters:
Name Type Description
name string The name of the event to listen for.
handler function The function to call when the event is fired.
Source:

begin(Discord, client, command, common, bot)

Initialize this submodule.
Parameters:
Name Type Description
Discord Discord The Discord object for the API library.
client Discord~Client The client that represents this bot.
command Command The command instance in which to register command listeners.
common Common Class storing common functions.
bot SpikeyBot The parent SpikeyBot instance.
Inherited From:
Source:

<protected> debug(msg)

Log using common.logDebug, but automatically set name.
Parameters:
Name Type Description
msg string The message to log.
Inherited From:
Source:

end()

Trigger subModule to shutdown and get ready for process terminating.
Inherited From:
Source:

<protected> error(msg)

Log using common.error, but automatically set name.
Parameters:
Name Type Description
msg string The message to log.
Inherited From:
Source:

export()

Export data required to maintain the bot across reloading this module. Expected to be returned directly to this.import once reloaded.
Inherited From:
Overrides:
Source:
Returns:
The data to be exported.
Type
MainModule~ModuleData

find(cmd, msg [, setCmd])

Returns the callback function for the given event.
Parameters:
Name Type Argument Default Description
cmd string <nullable>
Command to force search for, and ignore command that could be matched with msg.
msg Discord~Message Message that is to trigger this command. This object will be updated with the command name that was found as msg.cmd.
setCmd boolean <optional>
false Set the cmd variable in the msg object to match the found command.
Source:
Returns:
The single command object reference, or null if it could not be found.
Type
Command~SingleCommand

findAll(cmd, msg)

Returns all the callback functions for the given event with wildcards allowed.
Parameters:
Name Type Description
cmd string Command and subcommands to search for without guild prefixes.
msg Discord~Message Message object to use to remove command prefix if it exist.
Source:
Returns:
The command object references, or an empty array if it could not be found.
Type
Array.<Command~SingleCommand>

fire(name, args)

Fire all handlers listening for an event.
Parameters:
Name Type Description
name string The name of the event to fire.
args * The arguments to pass to the handlers.
Source:

getAllNames()

Fetches a list of all currently registered commands.
Source:
Returns:
Array of all registered commands.
Type
Array.<string>

getDefaultSettings()

Fetch all commands and their default setting values.
Source:
See:
Returns:
All currently registered commands.
Type
object.<SingleCommand>

getUserSettings(gId)

Fetch all user-defined settings for a guild.
Parameters:
Name Type Description
gId string The guild id of which to fetch the settings.
Source:
Returns:
The settings for the guild mapped by command name. If it doesn't exist, an object will first be created.
Type
object.<CommandSetting>

import(data)

Imports data from a previous instance of this class in order to maintain references to other objects and classes across reloads.
Parameters:
Name Type Argument Description
data MainModule~ModuleData <nullable>
The data that was exported previously, or null if no data to import.
Inherited From:
Overrides:
Source:

<protected> initialize()

The function called at the end of begin() for further initialization specific to the subModule. Must be defined before begin() is called.
Inherited From:
Overrides:
Source:

<protected> log(msg)

Log using common.log, but automatically set name.
Parameters:
Name Type Description
msg string The message to log.
Inherited From:
Source:

on(cmd [, cb] [, onlyserver])

Registers a listener for a command.
Parameters:
Name Type Argument Default Description
cmd string | Array.<string> | Command~SingleCommand Command to listen for.
cb commandHandler <optional>
Function to call when command is triggered.
onlyserver boolean <optional>
false Whether the command is only allowed on a server.
Source:

registerSlashCommands()

Register all commands currently loaded as slash commands to the Discord API.
Source:
Returns:
REST API request Promise.
Type
Promise

<abstract> reloadable()

Check if this module is in a state that is ready to be reloaded. If false is returned, this module should not be unloaded and doing such may risk putting the module into an uncontrollable state. This is different from unloadable, which checks if this module can be stopped completely, this checks if the module can be stopped and restarted.
Inherited From:
Source:
See:
  • SubModule~unloadable
Returns:
True if can be reloaded, false if cannot.
Type
boolean

removeEventListener(name, handler)

Remove an event listener.
Parameters:
Name Type Description
name string The name of the event to listen for.
handler function The handler that is currently registered to listen on this event.
Source:

removeListener(cmd)

Remove listener for a command.
Parameters:
Name Type Description
cmd string | Array.<string> Command or alias of command to remove listener for.
Source:

save( [opt])

Saves all data to files necessary for saving current state.
Parameters:
Name Type Argument Default Description
opt string <optional>
'sync' Can be 'async', otherwise defaults to synchronous.
Inherited From:
Overrides:
Source:

<protected> shutdown()

Shutdown and disable this submodule. Removes all event listeners.
Inherited From:
Overrides:
Source:

<abstract> terminate()

Signal that the bot is shutting down and will not be restarting immediately. This is triggered on all shutdowns where all MainModules and SubModules will be unloaded.
Inherited From:
Source:

trigger(msg [, msg2])

Trigger a command firing and call it's handler passing in msg as only argument.
Parameters:
Name Type Argument Description
msg Discord~Message | string Message received from Discord to pass to handler and to use to find the correct handler, OR a string to override the command to trigger from msg.
msg2 Discord~Message <optional>
The message received from Discord if the first argument is a string.
Source:
Returns:
True if command was handled by us.
Type
boolean

<abstract> unloadable()

Check if this module is in a state that is ready to be unloaded. If false is returned, this module should not be unloaded and doing such may risk putting the module into an uncontrollable state.
Inherited From:
Source:
See:
  • SubModule~reloadable
Returns:
True if can be unloaded, false if cannot.
Type
boolean

validate(cmd, msg [, func])

Checks that the given command can be run with the given context. Does not actually fire the event.
Parameters:
Name Type Argument Description
cmd string <nullable>
The command to validate. Null to use msg to find the command to validate.
msg Discord~Message <nullable>
The message that will fire the event. If null, checks for channel and guild specific changes will not be validated.
func Command~SingleCommand <optional>
A command handler override to use for settings lookup. If this is not specified, the handler associated with cmd will be fetched.
Source:
Returns:
Message causing failure, or null if valid.
Type
string

<protected> warn(msg)

Log using common.logWarning, but automatically set name.
Parameters:
Name Type Description
msg string The message to log.
Inherited From:
Source:

<private, inner> commandAllowCmd(msg)

Allow user to unmute a command.
Parameters:
Name Type Description
msg Discord~Message The message the user sent that triggered this.
Source:

<private, inner> commandBlockCmd(msg)

Allow user to mute a command.
Parameters:
Name Type Description
msg Discord~Message The message the user sent that triggered this.
Source:

<private, inner> commandDisable(msg)

Allow user to disable a command.
Parameters:
Name Type Description
msg Discord~Message The message the user sent that triggered this.
Source:

<private, inner> commandEnable(msg)

Allow user to enable a command.
Parameters:
Name Type Description
msg Discord~Message The message the user sent that triggered this.
Source:

<private, inner> commandReset(msg)

Reset all custom command settings to default.
Parameters:
Name Type Description
msg Discord~Message The message the user sent that triggered this.
Source:
Fires:
  • Command.events#event:settingsReset

<private, inner> commandShow(msg)

Show user the currently configured settings for commands.
Parameters:
Name Type Description
msg Discord~Message The message the user sent that triggered this.
Source:

<private, inner> escapeRegExp(str)

Escape a given string to be passed into a regular expression.
Parameters:
Name Type Description
str string Input to escape.
Source:
Returns:
Escaped string.
Type
string