commandbot
Version:
A framework that helps you create your own Discord bot easier.
72 lines (71 loc) • 2.68 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SubCommandGroup = void 0;
const Command_js_1 = require("./base/Command.js");
const SubCommand_js_1 = require("./SubCommand.js");
const commandsTypes_js_1 = require("./commandsTypes.js");
/**
* Group of subcommands
* @class
*/
class SubCommandGroup extends Command_js_1.Command {
/**
* @constructor Group constructor
* @param {ChatCommand} parent - group parent command
* @param {SubCommandGroupInit} options - initialization options
*/
constructor(parent, options) {
var _a;
super(parent.manager, "CHAT", {
name: options.name,
default_permission: options.default_permission,
});
/**
* Group members (children)
* @type {Array<SubCommand>}
* @private
* @readonly
*/
this._children = [];
this.parent = parent;
this.description = (_a = options.description) !== null && _a !== void 0 ? _a : "No description";
if (!commandsTypes_js_1.CommandRegExps.chatName.test(this.name)) {
throw new Error(`"${this.name}" is not a valid group name (regexp: ${commandsTypes_js_1.CommandRegExps.chatName})`);
}
if (this.description && !commandsTypes_js_1.CommandRegExps.chatDescription.test(this.description)) {
throw new Error(`The description of "${this.name}" doesn't match a regular expression ${commandsTypes_js_1.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_js_1.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 Object.assign(Object.assign({}, super.toObject()), { type: 2, description: this.description, options: this._children.map((ch) => ch.toObject()) });
}
}
exports.SubCommandGroup = SubCommandGroup;