CommandInteractionOptionResolver.js
9 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
'use strict';
const { TypeError } = require('../errors');
/**
* A resolver for command interaction options.
*/
class CommandInteractionOptionResolver {
constructor(client, options, resolved) {
/**
* The client that instantiated this.
* @name CommandInteractionOptionResolver#client
* @type {Client}
* @readonly
*/
Object.defineProperty(this, 'client', { value: client });
/**
* The name of the subcommand group.
* @type {?string}
* @private
*/
this._group = null;
/**
* The name of the subcommand.
* @type {?string}
* @private
*/
this._subcommand = null;
/**
* The bottom-level options for the interaction.
* If there is a subcommand (or subcommand and group), this is the options for the subcommand.
* @type {CommandInteractionOption[]}
* @private
*/
this._hoistedOptions = options;
// Hoist subcommand group if present
if (this._hoistedOptions[0]?.type === 'SUB_COMMAND_GROUP') {
this._group = this._hoistedOptions[0].name;
this._hoistedOptions = this._hoistedOptions[0].options ?? [];
}
// Hoist subcommand if present
if (this._hoistedOptions[0]?.type === 'SUB_COMMAND') {
this._subcommand = this._hoistedOptions[0].name;
this._hoistedOptions = this._hoistedOptions[0].options ?? [];
}
/**
* The interaction options array.
* @name CommandInteractionOptionResolver#data
* @type {ReadonlyArray<CommandInteractionOption>}
* @readonly
*/
Object.defineProperty(this, 'data', { value: Object.freeze([...options]) });
/**
* The interaction resolved data
* @name CommandInteractionOptionResolver#resolved
* @type {Readonly<CommandInteractionResolvedData>}
*/
Object.defineProperty(this, 'resolved', { value: Object.freeze(resolved) });
}
/**
* Gets an option by its name.
* @param {string} name The name of the option.
* @param {boolean} [required=false] Whether to throw an error if the option is not found.
* @returns {?CommandInteractionOption} The option, if found.
*/
get(name, required = false) {
const option = this._hoistedOptions.find(opt => opt.name === name);
if (!option) {
if (required) {
throw new TypeError('COMMAND_INTERACTION_OPTION_NOT_FOUND', name);
}
return null;
}
return option;
}
/**
* Gets an option by name and property and checks its type.
* @param {string} name The name of the option.
* @param {ApplicationCommandOptionType} type The type of the option.
* @param {string[]} properties The properties to check for for `required`.
* @param {boolean} required Whether to throw an error if the option is not found.
* @returns {?CommandInteractionOption} The option, if found.
* @private
*/
_getTypedOption(name, type, properties, required) {
const option = this.get(name, required);
if (!option) {
return null;
} else if (option.type !== type) {
throw new TypeError('COMMAND_INTERACTION_OPTION_TYPE', name, option.type, type);
} else if (required && properties.every(prop => option[prop] === null || typeof option[prop] === 'undefined')) {
throw new TypeError('COMMAND_INTERACTION_OPTION_EMPTY', name, option.type);
}
return option;
}
/**
* Gets the selected subcommand.
* @param {boolean} [required=true] Whether to throw an error if there is no subcommand.
* @returns {?string} The name of the selected subcommand, or null if not set and not required.
*/
getSubcommand(required = true) {
if (required && !this._subcommand) {
throw new TypeError('COMMAND_INTERACTION_OPTION_NO_SUB_COMMAND');
}
return this._subcommand;
}
/**
* Gets the selected subcommand group.
* @param {boolean} [required=true] Whether to throw an error if there is no subcommand group.
* @returns {?string} The name of the selected subcommand group, or null if not set and not required.
*/
getSubcommandGroup(required = true) {
if (required && !this._group) {
throw new TypeError('COMMAND_INTERACTION_OPTION_NO_SUB_COMMAND_GROUP');
}
return this._group;
}
/**
* Gets a boolean option.
* @param {string} name The name of the option.
* @param {boolean} [required=false] Whether to throw an error if the option is not found.
* @returns {?boolean} The value of the option, or null if not set and not required.
*/
getBoolean(name, required = false) {
const option = this._getTypedOption(name, 'BOOLEAN', ['value'], required);
return option?.value ?? null;
}
/**
* Gets a channel option.
* @param {string} name The name of the option.
* @param {boolean} [required=false] Whether to throw an error if the option is not found.
* @returns {?(GuildChannel|ThreadChannel|APIChannel)}
* The value of the option, or null if not set and not required.
*/
getChannel(name, required = false) {
const option = this._getTypedOption(name, 'CHANNEL', ['channel'], required);
return option?.channel ?? null;
}
/**
* Gets a string option.
* @param {string} name The name of the option.
* @param {boolean} [required=false] Whether to throw an error if the option is not found.
* @returns {?string} The value of the option, or null if not set and not required.
*/
getString(name, required = false) {
const option = this._getTypedOption(name, 'STRING', ['value'], required);
return option?.value ?? null;
}
/**
* Gets an integer option.
* @param {string} name The name of the option.
* @param {boolean} [required=false] Whether to throw an error if the option is not found.
* @returns {?number} The value of the option, or null if not set and not required.
*/
getInteger(name, required = false) {
const option = this._getTypedOption(name, 'INTEGER', ['value'], required);
return option?.value ?? null;
}
/**
* Gets a number option.
* @param {string} name The name of the option.
* @param {boolean} [required=false] Whether to throw an error if the option is not found.
* @returns {?number} The value of the option, or null if not set and not required.
*/
getNumber(name, required = false) {
const option = this._getTypedOption(name, 'NUMBER', ['value'], required);
return option?.value ?? null;
}
/**
* Gets a user option.
* @param {string} name The name of the option.
* @param {boolean} [required=false] Whether to throw an error if the option is not found.
* @returns {?User} The value of the option, or null if not set and not required.
*/
getUser(name, required = false) {
const option = this._getTypedOption(name, 'USER', ['user'], required);
return option?.user ?? null;
}
/**
* Gets a member option.
* @param {string} name The name of the option.
* @param {boolean} [required=false] Whether to throw an error if the option is not found.
* @returns {?(GuildMember|APIGuildMember)}
* The value of the option, or null if not set and not required.
*/
getMember(name, required = false) {
const option = this._getTypedOption(name, 'USER', ['member'], required);
return option?.member ?? null;
}
/**
* Gets a role option.
* @param {string} name The name of the option.
* @param {boolean} [required=false] Whether to throw an error if the option is not found.
* @returns {?(Role|APIRole)} The value of the option, or null if not set and not required.
*/
getRole(name, required = false) {
const option = this._getTypedOption(name, 'ROLE', ['role'], required);
return option?.role ?? null;
}
/**
* Gets a mentionable option.
* @param {string} name The name of the option.
* @param {boolean} [required=false] Whether to throw an error if the option is not found.
* @returns {?(User|GuildMember|APIGuildMember|Role|APIRole)}
* The value of the option, or null if not set and not required.
*/
getMentionable(name, required = false) {
const option = this._getTypedOption(name, 'MENTIONABLE', ['user', 'member', 'role'], required);
return option?.member ?? option?.user ?? option?.role ?? null;
}
/**
* Gets a message option.
* @param {string} name The name of the option.
* @param {boolean} [required=false] Whether to throw an error if the option is not found.
* @returns {?(Message|APIMessage)}
* The value of the option, or null if not set and not required.
*/
getMessage(name, required = false) {
const option = this._getTypedOption(name, '_MESSAGE', ['message'], required);
return option?.message ?? null;
}
/**
* Gets the focused option.
* @param {boolean} [getFull=false] Whether to get the full option object
* @returns {string|number|ApplicationCommandOptionChoice}
* The value of the option, or the whole option if getFull is true
*/
getFocused(getFull = false) {
const focusedOption = this._hoistedOptions.find(option => option.focused);
if (!focusedOption) throw new TypeError('AUTOCOMPLETE_INTERACTION_OPTION_NO_FOCUSED_OPTION');
return getFull ? focusedOption : focusedOption.value;
}
}
module.exports = CommandInteractionOptionResolver;