commandbot
Version:
A framework that helps you create your own Discord bot easier.
218 lines (217 loc) • 8.89 kB
TypeScript
import { Guild, Interaction, Message } from "discord.js";
import { ChatCommand } from "../commands/ChatCommand.js";
import { ContextMenuCommand } from "../commands/ContextMenuCommand.js";
import { Commands, CommandInit, CommandType } from "../commands/commandsTypes.js";
import { CommandPermission, RegisteredCommandObject, APICommandType } from "./apiTypes.js";
import { Bot } from "./Bot.js";
import { SubCommand } from "../commands/SubCommand.js";
import { HelpMessageParams } from "../commands/Help.js";
import { HelpMessage } from "../commands/Help.js";
import { PrefixManager } from "./PrefixManager.js";
import { Command } from "../commands/base/Command.js";
import { InputManager } from "./InputManager.js";
/**
* Object that stores the registered commands and is responsible for data exchanging with the Discord API
* @class
*/
export declare class CommandManager {
/**
* List of commands registered in the manager
* @type {Array<Command>}
* @private
* @readonly
*/
private readonly _commands;
/**
* Cache of Discord API commands data
* @type {Map<string, Map<string, RegisteredCommandObject>>}
* @private
* @readonly
*/
private readonly _registerCache;
private readonly _globalEntryName;
/**
* Client connected to this manager
* @type {Client}
* @public
* @readonly
*/
readonly client: Bot;
/**
* Help command associated with this manager
* @type {?HelpMessage}
* @public
* @readonly
*/
readonly help?: HelpMessage;
/**
* A manager holding all guild-specific prefixes and a global prefix
* @type {string}
* @public
* @readonly
*/
readonly prefix: PrefixManager;
/**
* A string used to split all incoming input data from Discord messages
* @type {string}
* @public
* @readonly
*/
readonly argumentSeparator: string;
/**
* A string used to separate subcommand groups and subcommands
* @type {string}
* @public
* @readonly
*/
readonly commandSeparator: string;
/**
* Discord API URL
* @type {string}
* @public
* @static
* @readonly
*/
static readonly baseApiUrl: string;
/**
*
* @constructor
* @param {Bot} client - client that this manager belongs to
* @param {HelpMessageParams} helpMsg - parameters defining appearance of the help message
* @param {?string} [prefix] - prefix used to respond to message interactions
* @param {?string} [argSep=','] - a string used to split all incoming input data from Discord messages
* @param {?string} [cmdSep='/'] - a string used to separate subcommand groups and subcommands
*/
constructor(client: Bot, helpMsg: HelpMessageParams, prefix?: string, argSep?: string, cmdSep?: string);
/**
* Discord API commands cache
* @type {Map<string, Map<string, RegisteredCommandObject>>}
*/
get cache(): Readonly<Map<string, Map<string, RegisteredCommandObject>>>;
/**
* Number of commands registered in this manager
* @type {number}
*/
get commandsCount(): Readonly<number>;
/**
* Creates and registers command in the manager based on the given options
* @param {T} type - a type of command that will be created and added to this manager
* @param {CommandInit<T>} options - an object containing all properties required to create this type of command
* @returns {Commands<T>} A computed command object that inherits from {@link Command}
* @public
* @remarks All commands have to be added to the instance **before starting the bot**. Adding commands while the bot is running is not possible and can cause issues.
*
* Command types
* - [CHAT](https://grz4na.github.io/commandbot-docs/interfaces/ChatCommandInit.html) - message interactions using command prefixes or slash commands
* - [USER](https://grz4na.github.io/commandbot-docs/interfaces/ContextMenuCommandInit.html) - right-click context menu interactions on users
* - [MESSAGE](https://grz4na.github.io/commandbot-docs/interfaces/ContextMenuCommandInit.html) - right-click context menu interactions on messages
*/
add<T extends CommandType>(type: T, options: CommandInit<T>): Commands<T>;
/**
* Get command registered in this manager
* @param {string} q - command name or alias
* @param {?APICommandType} [t] - type of command you want to get from this manager (if *undefined* searches in all registered commands)
* @returns {?Command} A command object
* @public
*/
get<T extends CommandType>(q: string, t?: T): (T extends "CHAT" ? Commands<T> | SubCommand : Commands<T>) | null;
/**
* Fetches command object from the Discord API
* @param {string} id - Discord command ID
* @param {?Guild | string} [guild] - ID of guild that this command belongs to
* @param {?boolean} [noCache=false] - whether not to use cached data
* @returns {Promise<RegisteredCommandObject>} Discord command object
* @public
* @async
*/
getApi(id: string, guild?: Guild | string, noCache?: boolean): Promise<RegisteredCommandObject>;
/**
* Fetches command ID by name from the Discord APi
* @param {string} name - name of the command
* @param {string} type - command type you want to get ID for
* @param {?string} [guild] - ID of guild that this command belongs to
* @returns {string} Command ID from the Discord API
* @public
* @async
*/
getIdApi(name: string, type: APICommandType, guild?: Guild | string): Promise<string | null>;
/**
* Lists all commands in the manager
* @param {APICommandType} [f] - filter, type of commands to return in the list
* @returns {Array<Command>} An array of commands registered in this manager
* @public
*/
list(): readonly Command[];
list(f: "CHAT"): readonly ChatCommand[];
list(f: "CONTEXT"): readonly ContextMenuCommand[];
/**
* Lists commands registered in the Discord API
* @param {Guild | string} [g] - Guild object or ID
* @returns {Promise<Map<string, RegisteredCommandObject>>} List of commands from Discord API
* @public
* @async
*/
listApi(g?: Guild | string): Promise<Map<string, RegisteredCommandObject>>;
/**
* Process an interaction
* @param {Interaction | Message} i - interaction object to fetch a command from
* @returns {?InputManager} An InputManager containing all input data (command, arguments, target etc.)
* @public
*/
fetch(i: Interaction | Message): InputManager | null;
/**
* Register all commands in this manager in the Discord API
* @returns {Promise<void>}
* @public
* @async
*/
register(): Promise<void>;
/**
* Set permissions using Discord Permissions API
* @param {string} id - command ID
* @param {CommandPermission[]} permissions - permissions to set
* @param {Guild | string} [g] - Guild ID or object (if command is in a guild)
* @returns {Promise<void>}
* @public
* @async
* @experimental This functionality hasn't been polished and fully tested yet. Using it might lead to errors and application crashes.
*/
setPermissionsApi(id: string, permissions: CommandPermission[], g?: Guild | string): Promise<void>;
/**
* Get permissions from Discord Permissions API for a specified command
* @param {string} id - command ID
* @param {Guild | string} [g] - Guild ID or object (if command is in a guild)
* @public
* @async
* @experimental This functionality hasn't been polished and fully tested yet. Using it might lead to errors and application crashes.
*/
getPermissionsApi(id: string, g?: Guild | string): Promise<CommandPermission[]>;
/**
*
* @param {Array<RegisteredCommandObject>} commands - list of commands to cache
* @param {?string} guildId - guild ID
* @returns {void}
* @private
*/
private updateCache;
/**
* Retrieves cache from the manager
* @param {string} q
* @param {?string} guildId
* @returns {?RegisteredCommandObject}
*/
private getCache;
/**
* Performs internal data type conversions
* @param {Array<RegisteredCommandObject>} a
* @returns {Map<string, RegisteredCommandObject>}
*/
private arrayToMap;
/**
* @param {any} c - object to check
* @returns {boolean} Whether this object is a {@link Command} object
* @public
* @static
*/
static isCommand(c: any): c is Command;
}