more detailed jsdoc documentation (partial commands)

This commit is contained in:
GRZANA committed 2021-11-13 15:40:39 +01:00
1 parent 5aff535029
commit 95ba89e602
10 files changed
+119 -26

No files matched your search

+41 -15
View File
@@ -12,50 +12,71 @@ import { applicationState } from "../state.js";
import { InputManager } from "../structures/InputManager.js";
/**
* @class A representation of CHAT_INPUT command (also known as a slash command)
* A representation of CHAT_INPUT command (also known as a slash command)
* @class
* @extends {PermissionGuildCommand}
*/
export class ChatCommand extends PermissionGuildCommand {
/**
* Subcommands and groups of this command
* @type {Array<ChildCommandResolvable>}
* @private
* @readonly
*/
private readonly _children: ChildCommandResolvable[] = [];
/**
* List of parameters that can passed to this command
* @type {Array<Parameter<any>>}
* @public
* @readonly
*/
public readonly parameters: Parameter<any>[];
/**
* List of different names that can be used to invoke a command (when using prefix interactions)
* @type {Array<string>}
* @public
* @readonly
*/
public readonly aliases?: string[];
/**
* Command description displayed in the help message or in slash commands menu (Default description: "No description")
* @type {string}
* @public
* @readonly
*/
public readonly description: string;
/**
* Command usage displayed in the help message
* @type {string}
* @public
* @readonly
*/
public readonly usage?: string;
/**
* Whether this command is visible in the help message (default: true)
* @type {boolean}
* @public
* @readonly
*/
public readonly visible: boolean;
/**
* Whether this command should be registered as a slash command (default: true)
* @type {boolean}
* @public
* @readonly
*/
public readonly slash: boolean;
/**
* @constructor ChatCommand constructor
* @param manager - a manager that this command belongs to
* @param options - {@link ChatCommandInit} object containing all options needed to create a {@link ChatCommand}
* ChatCommand constructor
* @constructor
* @param {CommandManager} manager - a manager that this command belongs to
* @param {ChatCommandInit} options - {@link ChatCommandInit} object containing all options needed to create a {@link ChatCommand}
*/
constructor(manager: CommandManager, options: ChatCommandInit) {
super(manager, "CHAT", {
@@ -114,7 +135,10 @@ export class ChatCommand extends PermissionGuildCommand {
/**
* Invoke the command
* @param input - input data manager
* @param {InputManager} input - input data manager
* @returns {Promise<void>}
* @public
* @async
*/
public async start(input: InputManager): Promise<void> {
if (!this.slash && input.interaction instanceof Interaction) {
@@ -125,9 +149,10 @@ export class ChatCommand extends PermissionGuildCommand {
/**
* Attaches subcommand or subcommand group to this ChatCommand (this makes the base command usable only via prefix interactions)
* @param type - subcommand type
* @param options - initialization options
* @returns A computed subcommand object
* @param {T} type - subcommand type
* @param {ChildCommandInit<T>} options - initialization options
* @returns {ChildCommands<T>} A computed subcommand object
* @public
*/
public append<T extends ChildCommandType>(type: T, options: ChildCommandInit<T>): ChildCommands<T> {
const command =
@@ -149,9 +174,10 @@ export class ChatCommand extends PermissionGuildCommand {
/**
*
* @param options - parameter options
* @param interaction - Discord interaction
* @returns an {@link InputManager} containing all interaction-related data or *null*
* @param {Array<CommandInteractionOption>} options - parameter options
* @param {Interaction | Message} interaction - Discord interaction
* @returns {InputManager} an {@link InputManager} containing all interaction-related data or *null*
* @public
*/
public fetchSubcommand(options: CommandInteractionOption[], interaction: Interaction | Message): InputManager | null {
if (!this.hasSubCommands) return null;
@@ -208,9 +234,9 @@ export class ChatCommand extends PermissionGuildCommand {
/**
*
* @param name - subcommand name
* @param group - name of the group (if any)
* @returns a {@link SubCommand} object or *null*
* @param {string} name - subcommand name
* @param {string} [group] - name of the group (if any)
* @returns {SubCommand} a {@link SubCommand} object or *null*
*/
public getSubcommand(name: string, group?: string): SubCommand | null {
if (!this.hasSubCommands) return null;
@@ -228,7 +254,7 @@ export class ChatCommand extends PermissionGuildCommand {
/**
* Converts {@link ChatCommand} instance to object that is recognized by the Discord API
* @returns Discord API object
* @returns {ChatCommandObject} Discord API object
*/
public toObject(): ChatCommandObject {
const obj: ChatCommandObject = {
+12
View File
@@ -17,24 +17,32 @@ export class Command {
/**
* Manager in which this command is registered
* @type {CommandManager}
* @public
* @readonly
*/
public readonly manager: CommandManager;
/**
* Command name
* @type {string}
* @public
* @readonly
*/
public readonly name: string;
/**
* CommandBot's internal command type
* @type {CommandType}
* @public
* @readonly
*/
public readonly type: CommandType;
/**
* Discord API default_permission
* @type {boolean}
* @public
* @readonly
*/
public readonly default_permission: boolean;
@@ -62,6 +70,7 @@ export class Command {
/**
* Converts a command instance to an {@link APICommandObject}
* @returns {APICommandObject} An object that is accepted by the Discord API
* @public
*/
public toObject(): APICommandObject {
return {
@@ -74,6 +83,7 @@ export class Command {
* Check base command type
* @param {BaseCommandType} type - base command type
* @returns {boolean} Whether this command can be used as the given type
* @public
*/
public isBaseCommandType<T extends BaseCommandType>(type: T): this is BaseCommands<T> {
switch (type) {
@@ -113,6 +123,7 @@ export class Command {
* Checks command type
* @param {CommandType} type - command type
* @returns {boolean} Whether this command can be used as the given type
* @public
*/
public isCommandType<T extends CommandType>(type: T): this is Commands<T> {
switch (type) {
@@ -139,6 +150,7 @@ export class Command {
* Check child command type
* @param {ChildCommandType} type - child command type
* @returns {boolean} Whether this command can be used as a child command of the given type
* @public
*/
public isChildCommandType<T extends ChildCommandType>(type: T): this is ChildCommands<T> {
switch (type) {
+10
View File
@@ -9,16 +9,21 @@ import { InputManager } from "../../structures/InputManager.js";
/**
* Function (executable) command
* @class
* @extends {Command}
*/
export class FunctionCommand extends Command {
/**
* Command function (called on command execution)
* @type {CommandFunction}
* @private
* @readonly
*/
private readonly _function: CommandFunction;
/**
* Whether a SUCCESS message should be sent after executing the command function (when there is no other reply)
* @type {boolean}
* @public
* @readonly
*/
public readonly announceSuccess: boolean;
@@ -48,6 +53,8 @@ export class FunctionCommand extends Command {
* Invoke the command
* @param {InputManager} input - input data manager
* @returns {Promise<void>} A *Promise* that resolves after the function command is completed
* @public
* @async
*/
public async start(input: InputManager): Promise<void> {
if (input.interaction instanceof Interaction && !input.interaction.isCommand() && !input.interaction.isContextMenu()) throw new TypeError(`Interaction not recognized`);
@@ -59,6 +66,9 @@ export class FunctionCommand extends Command {
* Reply handler
* @param {Message | Interaction} interaction - Discord interaction object
* @param {void | string | MessageEmbed | ReplyMessageOptions} result - result of command function execution
* @return {Promise<void>}
* @private
* @async
*/
private async handleReply(interaction: Message | Interaction, result: void | string | MessageEmbed | ReplyMessageOptions) {
if (interaction instanceof Interaction && !interaction.isCommand() && !interaction.isContextMenu()) throw new TypeError(`Interaction not recognized`);
+8
View File
@@ -7,17 +7,22 @@ import { InputManager } from "../../structures/InputManager.js";
/**
* Guild-scoped executable command
* @class
* @extends {FunctionCommand}
*/
export class GuildCommand extends FunctionCommand {
/**
* List of Discord guild (server) IDs in which this command can be used
* @type {Array<string>}
* @public
* @readonly
*/
public readonly guilds?: string[];
/**
* If set to *false*, all interactions that get invoked from private/direct conversations (outside a guild) will result a PermissionError
* @type {boolean}
* @public
* @readonly
*/
public readonly dm: boolean;
@@ -43,6 +48,9 @@ export class GuildCommand extends FunctionCommand {
/**
* Invoke the command
* @param {InputManager} input - input data
* @returns {Promise<void>}
* @public
* @async
*/
public async start(input: InputManager): Promise<void> {
if (!input.interaction.guild && !this.dm) throw new Error(`Command "${this.name}" is only available inside a guild.`);
+6
View File
@@ -9,11 +9,14 @@ import { InputManager } from "../../structures/InputManager.js";
/**
* Executable command with attached permission system
* @class
* @extends {FunctionCommand}
*/
export class PermissionCommand extends FunctionCommand {
/**
* Object containing check functions and permission bitfields
* @type {CommandPermissions}
* @public
* @readonly
*/
public readonly permissions: CommandPermissions;
@@ -38,6 +41,9 @@ export class PermissionCommand extends FunctionCommand {
/**
* Invoke the command
* @param {InputManager} input - input data
* @returns {Promise<void>}
* @public
* @async
*/
public async start(input: InputManager): Promise<void> {
if (this.permissions.check(input.interaction)) {
@@ -8,17 +8,23 @@ import { InputManager } from "../../structures/InputManager.js";
/**
* Guild-scoped executable command with permissions attached
* @class
* @extends {PermissionCommand}
* @implements {GuildCommand}
*/
export class PermissionGuildCommand extends PermissionCommand implements GuildCommand {
/**
* List of Discord guild (server) IDs in which this command can be used
* @type {Array<string>}
* @public
* @readonly
*/
public readonly guilds?: string[];
/**
* If set to *false*, all interactions that get invoked from private/direct conversations (outside a guild) will result a PermissionError
* @type {boolean}
* @public
* @readonly
*/
public readonly dm: boolean;
@@ -45,6 +51,9 @@ export class PermissionGuildCommand extends PermissionCommand implements GuildCo
/**
* Invoke the command
* @param {InputManager} input - input data
* @returns {Promise<void>}
* @public
* @async
*/
public async start(input: InputManager): Promise<void> {
if (!input.interaction.guild && !this.dm) throw new Error(`Command "${this.name}" is only available inside a guild.`);
+1 -1
View File
@@ -1,8 +1,8 @@
import { ColorResolvable } from "discord.js";
/**
* @interface
* All properties used to customize the appearance of a help message
* @interface
*/
export interface HelpMessageParams {
/**
+9 -9
View File
@@ -3,8 +3,8 @@ import { CommandPermissionsInit } from "./permissions.js";
import { CommandFunction, ContextType } from "./commands.js";
/**
* @interface
* Initialization options of core {@link Command} object
* @interface
*/
export interface APICommandInit {
/**
@@ -20,8 +20,8 @@ export interface APICommandInit {
}
/**
* @interface
* Initialization options of base executable command
* @interface
* @extends {APICommandInit}
*/
export interface FunctionCommandInit extends APICommandInit {
@@ -38,8 +38,8 @@ export interface FunctionCommandInit extends APICommandInit {
}
/**
* @interface
* Initialization options of base guild-scoped command
* @interface
* @extends {FunctionCommandInit}
*/
export interface GuildCommandInit extends FunctionCommandInit {
@@ -56,8 +56,8 @@ export interface GuildCommandInit extends FunctionCommandInit {
}
/**
* @interface
* Initialization options of base command with attached permissions
* @interface
* @extends {FunctionCommandInit}
*/
export interface PermissionCommandInit extends FunctionCommandInit {
@@ -69,8 +69,8 @@ export interface PermissionCommandInit extends FunctionCommandInit {
}
/**
* @interface
* Initialization options of base guild-scoped command with attached permisisions
* @interface
* @extends {PermissionCommandInit}
*/
export interface PermissionGuildCommandInit extends PermissionCommandInit {
@@ -87,8 +87,8 @@ export interface PermissionGuildCommandInit extends PermissionCommandInit {
}
/**
* @interface
* Intialization options of chat command
* @interface
* @extends {PermissionGuildCommandInit}
*/
export interface ChatCommandInit extends PermissionGuildCommandInit {
@@ -125,8 +125,8 @@ export interface ChatCommandInit extends PermissionGuildCommandInit {
}
/**
* @interface
* Initialization options of context menu interactions
* @interface
* @extends {PermissionGuildCommandInit}
*/
export interface ContextMenuCommandInit extends PermissionGuildCommandInit {
@@ -138,8 +138,8 @@ export interface ContextMenuCommandInit extends PermissionGuildCommandInit {
}
/**
* @interface
* Subcommand initialization options
* @interface
* @extends {PermissionCommandInit}
*/
export interface SubCommandInit extends PermissionCommandInit {
@@ -161,8 +161,8 @@ export interface SubCommandInit extends PermissionCommandInit {
}
/**
* @interface
* Intialization options of subcommand group
* @interface
* @extends {APICommandInit}
*/
export interface SubCommandGroupInit extends APICommandInit {
+20
View File
@@ -23,21 +23,25 @@ import { InputManager } from "../../structures/InputManager.js";
/**
* Types of command bases
* @type
*/
export type BaseCommandType = "BASE" | "FUNCTION" | "GUILD" | "PERMISSION" | "PERMISSIONGUILD";
/**
* Command types
* @type
*/
export type CommandType = "CHAT" | "CONTEXT";
/**
* Child command types
* @type
*/
export type ChildCommandType = "COMMAND" | "GROUP";
/**
* Base command type selector
* @type
*/
export type BaseCommands<T extends BaseCommandType> = T extends "BASE"
? Command
@@ -53,16 +57,19 @@ export type BaseCommands<T extends BaseCommandType> = T extends "BASE"
/**
* Command type selector
* @type
*/
export type Commands<T extends CommandType> = T extends "CHAT" ? ChatCommand : T extends "CONTEXT" ? ContextMenuCommand : never;
/**
* Child command type selector
* @type
*/
export type ChildCommands<T extends ChildCommandType> = T extends "COMMAND" ? SubCommand : T extends "GROUP" ? SubCommandGroup : never;
/**
* Base command initializer selector
* @type
*/
export type BaseCommandInit<T extends BaseCommandType> = T extends "BASE"
? APICommandInit
@@ -78,46 +85,59 @@ export type BaseCommandInit<T extends BaseCommandType> = T extends "BASE"
/**
* Command initializer selector
* @type
*/
export type CommandInit<T extends CommandType> = T extends "CHAT" ? ChatCommandInit : T extends "CONTEXT" ? ContextMenuCommandInit : never;
/**
* Child command initializer selector
* @type
*/
export type ChildCommandInit<T extends ChildCommandType> = T extends "COMMAND" ? SubCommandInit : T extends "GROUP" ? SubCommandGroupInit : never;
/**
* Base command resolvables
* @type
*/
export type BaseCommandResolvable = Command | FunctionCommand | GuildCommand | PermissionCommand | PermissionGuildCommand;
/**
* Command resolvables
* @type
*/
export type CommandResolvable = ChatCommand | ContextMenuCommand;
/**
* Child command resolvables
* @type
*/
export type ChildCommandResolvable = SubCommandGroup | SubCommand;
/**
* Context menu command types
* @type
*/
export type ContextType = "USER" | "MESSAGE";
/**
* All types that can be returned from a command function
* @type
*/
export type CommandFunctionReturnTypes = void | string | MessageEmbed | ReplyMessageOptions | Promise<void | string | MessageEmbed | ReplyMessageOptions>;
/**
* Command function definition
* @type
*/
export type CommandFunction = (input: InputManager) => CommandFunctionReturnTypes;
/**
* Regular expressions used globally to perform name checking
* @property {RegExp} baseName - base command name (used for context menu)
* @property {RegExp} chatName - chat command name
* @property {RegExp} chatDescription - chat command description
* @property {RegExp} separator - argument and command separators
* @property {RegExp} prefix - global and guild-scoped prefixes
*/
export const CommandRegExps = {
baseName: /^.{1,32}$/,
+3 -1
View File
@@ -2,17 +2,19 @@ import { PermissionResolvable, Message, Interaction } from "discord.js";
/**
* Permission checking methods
* @type
*/
export type PermissionCheckTypes = "ALL" | "ANY";
/**
* Definition of permission function
* @type
*/
export type PermissionFunction = (i: Interaction | Message) => boolean;
/**
* @interface
* Command permissions initialization object
* @interface
*/
export interface CommandPermissionsInit {
/**