CommandHandler

CommandHandler

A class that handles loading, registering and running slash commands.

Constructor

# new CommandHandler()

Members

# commands :Collection.<String, module:types~Command>

Collection containing all commands.

Type:
  • Collection.<String, module:types~Command>

Methods

# (static) addCommandDirectory(dir) → {module:handlers/CommandHandler~CommandHandler}

Add a directory to scan for commands.

Parameters:
Name Type Description
dir String

The absolute path of the directory to add.

Returns:

A reference to the CommandHandler class.

Type
module:handlers/CommandHandler~CommandHandler
Example
// Adds the directory "commands" to the command handler.

const { join } = require("path");

CommandHandler.addCommandDirectory(join(__dirname, "commands"));

# (static) attachClient(client) → {module:handlers/CommandHandler~CommandHandler}

Attach a client to the CommandHandler.

Parameters:
Name Type Description
client module:discord.js~Client

The Discord client to attach.

Returns:

A reference to the CommandHandler class.

Type
module:handlers/CommandHandler~CommandHandler

# (static) generateStructure(guildIDopt) → {module:discord-api-types/v10~RESTPostAPIApplicationCommandsJSONBody}

Generate the command structure in order to register commands with Discord.

Parameters:
Name Type Attributes Description
guildID module:discord.js~Snowflake <optional>

The ID of the guild to generate a structure for.

Returns:

The structure.

Type
module:discord-api-types/v10~RESTPostAPIApplicationCommandsJSONBody

# (static) getCommand(…path) → {module:types~Command}

Get a global command from a path.

Parameters:
Name Type Attributes Description
path String <repeatable>

The path of the command.

Returns:

The command.

Type
module:types~Command

# (static) getGuildCommand(guildID, …path) → {module:types~Command}

Get a guild specific command from a path.

Parameters:
Name Type Attributes Description
guildID string

The guild's ID.

path any <repeatable>

The path of the command.

Returns:

The command.

Type
module:types~Command

# (static) loadCommands() → {module:handlers/CommandHandler~CommandHandler}

Load all commands.

Returns:

A reference to the CommandHandler class.

Type
module:handlers/CommandHandler~CommandHandler
Example
// Load all commands in the directory "commands"

const { join } = require("path");

CommandHandler.addCommandDirectory(join(__dirname, "commands"));
CommandHandler.loadCommands();

# (static) removeCommandDirectory(dir) → {module:handlers/CommandHandler~CommandHandler}

Remove a directory to scan for commands.

Parameters:
Name Type Description
dir String

The absolute path of the directory to remove.

Returns:

A reference to the CommandHandler class.

Type
module:handlers/CommandHandler~CommandHandler
Example
// Removes the directory "commands" from the command handler.

const { join } = require("path");

CommandHandler.removeCommandDirectory(join(__dirname, "commands"));