This repository has been archived by the owner on Dec 2, 2023. It is now read-only.
/
SubCommandGroup.ts
105 lines (98 loc) · 3.05 KB
/
SubCommandGroup.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { SubCommandGroupObject } from "../structures/apiTypes.js";
import { APICommandInit, Command } from "./base/Command.js";
import { ChatCommand } from "./ChatCommand.js";
import { SubCommand, SubCommandInit } from "./SubCommand.js";
import { CommandRegExps } from "./commandsTypes.js";
/**
* Intialization options of subcommand group
* @interface
* @extends {APICommandInit}
*/
export interface SubCommandGroupInit extends APICommandInit {
/**
* Command description
* @type {?string}
*/
description?: string;
}
/**
* Group of subcommands
* @class
*/
export class SubCommandGroup extends Command {
/**
* Group members (children)
* @type {Array<SubCommand>}
* @private
* @readonly
*/
private readonly _children: SubCommand[] = [];
/**
* Group parent command
* @type {ChatCommand}
* @public
* @readonly
*/
public readonly parent: ChatCommand;
/**
* Group description (default: "No description")
* @type {string}
* @public
* @readonly
*/
public readonly description: string;
/**
* @constructor Group constructor
* @param {ChatCommand} parent - group parent command
* @param {SubCommandGroupInit} options - initialization options
*/
constructor(parent: ChatCommand, options: SubCommandGroupInit) {
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
*/
public append(options: SubCommandInit): SubCommand {
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
*/
public toObject(): SubCommandGroupObject {
return {
...super.toObject(),
type: 2 as 2,
description: this.description,
options: this._children.map((ch) => ch.toObject()),
};
}
}