commandbot
Version:
A framework that helps you create your own Discord bot easier.
86 lines (85 loc) • 2.62 kB
JavaScript
import { Command } from "./base/Command.js";
import { SubCommand } from "./SubCommand.js";
import { CommandRegExps } from "./commandsTypes.js";
/**
* Group of subcommands
* @class
*/
export class SubCommandGroup extends Command {
/**
* Group members (children)
* @type {Array<SubCommand>}
* @private
* @readonly
*/
_children = [];
/**
* Group parent command
* @type {ChatCommand}
* @public
* @readonly
*/
parent;
/**
* Group description (default: "No description")
* @type {string}
* @public
* @readonly
*/
description;
/**
* @constructor Group constructor
* @param {ChatCommand} parent - group parent command
* @param {SubCommandGroupInit} options - initialization options
*/
constructor(parent, options) {
super(parent.manager, "CHAT", {
name: options.name,
default_permission: options.default_permission,
});
this.parent = parent;
this.description = options.description ?? "No description";
if (!CommandRegExps.chatName.test(this.name)) {
throw new Error(`"${this.name}" is not a valid group name (regexp: ${CommandRegExps.chatName})`);
}
if (this.description && !CommandRegExps.chatDescription.test(this.description)) {
throw new Error(`The description of "${this.name}" doesn't match a regular expression ${CommandRegExps.chatDescription}`);
}
}
/**
* List of subcommands attached to this group
* @type {Array<SubCommand>}
* @readonly
*/
get children() {
return Object.freeze([...this._children]);
}
/**
* Attach a subcommand to this group
* @param {SubCommandInit} options - subcommand initialization options
* @returns {SubCommand} A computed {@link SubCommand} object
* @public
*/
append(options) {
const sc = new SubCommand(this, options);
if (this._children.find((c) => c.name === sc.name)) {
throw new Error(`The name "${sc.name}" is already registered in "${this.name}"`);
}
else {
this._children.push(sc);
return sc;
}
}
/**
* @returns {SubCommandGroupObject} Discord API object
* @public
*/
toObject() {
return {
...super.toObject(),
type: 2,
description: this.description,
options: this._children.map((ch) => ch.toObject()),
};
}
}