Skip to content

Commit 1b151db

Browse files
refactor(CommandInteractionOptionResolver): Loosen mentionable checks (#8910)
refactor(CommandInteractionOptionResolver): loosen mentionable checks Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent aa8c57d commit 1b151db

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

packages/discord.js/src/structures/CommandInteractionOptionResolver.js

+30-15
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,18 @@ class CommandInteractionOptionResolver {
8585
/**
8686
* Gets an option by name and property and checks its type.
8787
* @param {string} name The name of the option.
88-
* @param {ApplicationCommandOptionType} type The type of the option.
88+
* @param {ApplicationCommandOptionType[]} allowedTypes The allowed types of the option.
8989
* @param {string[]} properties The properties to check for for `required`.
9090
* @param {boolean} required Whether to throw an error if the option is not found.
9191
* @returns {?CommandInteractionOption} The option, if found.
9292
* @private
9393
*/
94-
_getTypedOption(name, type, properties, required) {
94+
_getTypedOption(name, allowedTypes, properties, required) {
9595
const option = this.get(name, required);
9696
if (!option) {
9797
return null;
98-
} else if (option.type !== type) {
99-
throw new DiscordjsTypeError(ErrorCodes.CommandInteractionOptionType, name, option.type, type);
98+
} else if (!allowedTypes.includes(option.type)) {
99+
throw new DiscordjsTypeError(ErrorCodes.CommandInteractionOptionType, name, option.type, allowedTypes.join(', '));
100100
} else if (required && properties.every(prop => option[prop] === null || typeof option[prop] === 'undefined')) {
101101
throw new DiscordjsTypeError(ErrorCodes.CommandInteractionOptionEmpty, name, option.type);
102102
}
@@ -134,7 +134,7 @@ class CommandInteractionOptionResolver {
134134
* @returns {?boolean} The value of the option, or null if not set and not required.
135135
*/
136136
getBoolean(name, required = false) {
137-
const option = this._getTypedOption(name, ApplicationCommandOptionType.Boolean, ['value'], required);
137+
const option = this._getTypedOption(name, [ApplicationCommandOptionType.Boolean], ['value'], required);
138138
return option?.value ?? null;
139139
}
140140

@@ -146,7 +146,7 @@ class CommandInteractionOptionResolver {
146146
* The value of the option, or null if not set and not required.
147147
*/
148148
getChannel(name, required = false) {
149-
const option = this._getTypedOption(name, ApplicationCommandOptionType.Channel, ['channel'], required);
149+
const option = this._getTypedOption(name, [ApplicationCommandOptionType.Channel], ['channel'], required);
150150
return option?.channel ?? null;
151151
}
152152

@@ -157,7 +157,7 @@ class CommandInteractionOptionResolver {
157157
* @returns {?string} The value of the option, or null if not set and not required.
158158
*/
159159
getString(name, required = false) {
160-
const option = this._getTypedOption(name, ApplicationCommandOptionType.String, ['value'], required);
160+
const option = this._getTypedOption(name, [ApplicationCommandOptionType.String], ['value'], required);
161161
return option?.value ?? null;
162162
}
163163

@@ -168,7 +168,7 @@ class CommandInteractionOptionResolver {
168168
* @returns {?number} The value of the option, or null if not set and not required.
169169
*/
170170
getInteger(name, required = false) {
171-
const option = this._getTypedOption(name, ApplicationCommandOptionType.Integer, ['value'], required);
171+
const option = this._getTypedOption(name, [ApplicationCommandOptionType.Integer], ['value'], required);
172172
return option?.value ?? null;
173173
}
174174

@@ -179,7 +179,7 @@ class CommandInteractionOptionResolver {
179179
* @returns {?number} The value of the option, or null if not set and not required.
180180
*/
181181
getNumber(name, required = false) {
182-
const option = this._getTypedOption(name, ApplicationCommandOptionType.Number, ['value'], required);
182+
const option = this._getTypedOption(name, [ApplicationCommandOptionType.Number], ['value'], required);
183183
return option?.value ?? null;
184184
}
185185

@@ -190,7 +190,12 @@ class CommandInteractionOptionResolver {
190190
* @returns {?User} The value of the option, or null if not set and not required.
191191
*/
192192
getUser(name, required = false) {
193-
const option = this._getTypedOption(name, ApplicationCommandOptionType.User, ['user'], required);
193+
const option = this._getTypedOption(
194+
name,
195+
[ApplicationCommandOptionType.User, ApplicationCommandOptionType.Mentionable],
196+
['user'],
197+
required,
198+
);
194199
return option?.user ?? null;
195200
}
196201

@@ -201,7 +206,12 @@ class CommandInteractionOptionResolver {
201206
* The value of the option, or null if the user is not present in the guild or the option is not set.
202207
*/
203208
getMember(name) {
204-
const option = this._getTypedOption(name, ApplicationCommandOptionType.User, ['member'], false);
209+
const option = this._getTypedOption(
210+
name,
211+
[ApplicationCommandOptionType.User, ApplicationCommandOptionType.Mentionable],
212+
['member'],
213+
false,
214+
);
205215
return option?.member ?? null;
206216
}
207217

@@ -212,7 +222,12 @@ class CommandInteractionOptionResolver {
212222
* @returns {?(Role|APIRole)} The value of the option, or null if not set and not required.
213223
*/
214224
getRole(name, required = false) {
215-
const option = this._getTypedOption(name, ApplicationCommandOptionType.Role, ['role'], required);
225+
const option = this._getTypedOption(
226+
name,
227+
[ApplicationCommandOptionType.Role, ApplicationCommandOptionType.Mentionable],
228+
['role'],
229+
required,
230+
);
216231
return option?.role ?? null;
217232
}
218233

@@ -223,7 +238,7 @@ class CommandInteractionOptionResolver {
223238
* @returns {?Attachment} The value of the option, or null if not set and not required.
224239
*/
225240
getAttachment(name, required = false) {
226-
const option = this._getTypedOption(name, ApplicationCommandOptionType.Attachment, ['attachment'], required);
241+
const option = this._getTypedOption(name, [ApplicationCommandOptionType.Attachment], ['attachment'], required);
227242
return option?.attachment ?? null;
228243
}
229244

@@ -237,7 +252,7 @@ class CommandInteractionOptionResolver {
237252
getMentionable(name, required = false) {
238253
const option = this._getTypedOption(
239254
name,
240-
ApplicationCommandOptionType.Mentionable,
255+
[ApplicationCommandOptionType.Mentionable],
241256
['user', 'member', 'role'],
242257
required,
243258
);
@@ -252,7 +267,7 @@ class CommandInteractionOptionResolver {
252267
* The value of the option, or null if not set and not required.
253268
*/
254269
getMessage(name, required = false) {
255-
const option = this._getTypedOption(name, '_MESSAGE', ['message'], required);
270+
const option = this._getTypedOption(name, ['_MESSAGE'], ['message'], required);
256271
return option?.message ?? null;
257272
}
258273

packages/discord.js/typings/index.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1089,13 +1089,13 @@ export class CommandInteractionOptionResolver<Cached extends CacheType = CacheTy
10891089
private _subcommand: string | null;
10901090
private _getTypedOption(
10911091
name: string,
1092-
type: ApplicationCommandOptionType,
1092+
allowedTypes: ApplicationCommandOptionType[],
10931093
properties: (keyof ApplicationCommandOption)[],
10941094
required: true,
10951095
): CommandInteractionOption<Cached>;
10961096
private _getTypedOption(
10971097
name: string,
1098-
type: ApplicationCommandOptionType,
1098+
allowedTypes: ApplicationCommandOptionType[],
10991099
properties: (keyof ApplicationCommandOption)[],
11001100
required: boolean,
11011101
): CommandInteractionOption<Cached> | null;

0 commit comments

Comments
 (0)