mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-15 19:13:31 +01:00
refactor(OptionResolver): accept single type instead of an array (#6154)
This commit is contained in:
@@ -134,7 +134,7 @@ const Messages = {
|
|||||||
|
|
||||||
COMMAND_INTERACTION_OPTION_NOT_FOUND: name => `Required option "${name}" not found.`,
|
COMMAND_INTERACTION_OPTION_NOT_FOUND: name => `Required option "${name}" not found.`,
|
||||||
COMMAND_INTERACTION_OPTION_TYPE: (name, type, expected) =>
|
COMMAND_INTERACTION_OPTION_TYPE: (name, type, expected) =>
|
||||||
`Option "${name}" is of type: ${type}; expected one of: ${expected.join(', ')}`,
|
`Option "${name}" is of type: ${type}; expected ${expected}.`,
|
||||||
COMMAND_INTERACTION_OPTION_EMPTY: (name, type) =>
|
COMMAND_INTERACTION_OPTION_EMPTY: (name, type) =>
|
||||||
`Required option "${name}" is of type: ${type}; expected a non-empty value.`,
|
`Required option "${name}" is of type: ${type}; expected a non-empty value.`,
|
||||||
COMMAND_INTERACTION_OPTION_NO_SUB_COMMAND: 'No sub-command specified for interaction.',
|
COMMAND_INTERACTION_OPTION_NO_SUB_COMMAND: 'No sub-command specified for interaction.',
|
||||||
|
|||||||
@@ -65,18 +65,18 @@ class CommandInteractionOptionResolver {
|
|||||||
/**
|
/**
|
||||||
* Gets an option by name and property and checks its type.
|
* Gets an option by name and property and checks its type.
|
||||||
* @param {string} name The name of the option.
|
* @param {string} name The name of the option.
|
||||||
* @param {ApplicationCommandOptionType[]} types The type of the option.
|
* @param {ApplicationCommandOptionType} type The type of the option.
|
||||||
* @param {string[]} properties The properties to check for for `required`.
|
* @param {string[]} properties The properties to check for for `required`.
|
||||||
* @param {boolean} required Whether to throw an error if the option is not found.
|
* @param {boolean} required Whether to throw an error if the option is not found.
|
||||||
* @returns {?CommandInteractionOption} The option, if found.
|
* @returns {?CommandInteractionOption} The option, if found.
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
_getTypedOption(name, types, properties, required) {
|
_getTypedOption(name, type, properties, required) {
|
||||||
const option = this.get(name, required);
|
const option = this.get(name, required);
|
||||||
if (!option) {
|
if (!option) {
|
||||||
return null;
|
return null;
|
||||||
} else if (!types.includes(option.type)) {
|
} else if (option.type !== type) {
|
||||||
throw new TypeError('COMMAND_INTERACTION_OPTION_TYPE', name, option.type, types);
|
throw new TypeError('COMMAND_INTERACTION_OPTION_TYPE', name, option.type, type);
|
||||||
} else if (required && properties.every(prop => option[prop] === null || typeof option[prop] === 'undefined')) {
|
} else if (required && properties.every(prop => option[prop] === null || typeof option[prop] === 'undefined')) {
|
||||||
throw new TypeError('COMMAND_INTERACTION_OPTION_EMPTY', name, option.type);
|
throw new TypeError('COMMAND_INTERACTION_OPTION_EMPTY', name, option.type);
|
||||||
}
|
}
|
||||||
@@ -112,7 +112,7 @@ class CommandInteractionOptionResolver {
|
|||||||
* @returns {?boolean} The value of the option, or null if not set and not required.
|
* @returns {?boolean} The value of the option, or null if not set and not required.
|
||||||
*/
|
*/
|
||||||
getBoolean(name, required = false) {
|
getBoolean(name, required = false) {
|
||||||
const option = this._getTypedOption(name, ['BOOLEAN'], ['value'], required);
|
const option = this._getTypedOption(name, 'BOOLEAN', ['value'], required);
|
||||||
return option?.value ?? null;
|
return option?.value ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -124,7 +124,7 @@ class CommandInteractionOptionResolver {
|
|||||||
* The value of the option, or null if not set and not required.
|
* The value of the option, or null if not set and not required.
|
||||||
*/
|
*/
|
||||||
getChannel(name, required = false) {
|
getChannel(name, required = false) {
|
||||||
const option = this._getTypedOption(name, ['CHANNEL'], ['channel'], required);
|
const option = this._getTypedOption(name, 'CHANNEL', ['channel'], required);
|
||||||
return option?.channel ?? null;
|
return option?.channel ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -135,7 +135,7 @@ class CommandInteractionOptionResolver {
|
|||||||
* @returns {?string} The value of the option, or null if not set and not required.
|
* @returns {?string} The value of the option, or null if not set and not required.
|
||||||
*/
|
*/
|
||||||
getString(name, required = false) {
|
getString(name, required = false) {
|
||||||
const option = this._getTypedOption(name, ['STRING'], ['value'], required);
|
const option = this._getTypedOption(name, 'STRING', ['value'], required);
|
||||||
return option?.value ?? null;
|
return option?.value ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -146,7 +146,7 @@ class CommandInteractionOptionResolver {
|
|||||||
* @returns {?number} The value of the option, or null if not set and not required.
|
* @returns {?number} The value of the option, or null if not set and not required.
|
||||||
*/
|
*/
|
||||||
getInteger(name, required = false) {
|
getInteger(name, required = false) {
|
||||||
const option = this._getTypedOption(name, ['INTEGER'], ['value'], required);
|
const option = this._getTypedOption(name, 'INTEGER', ['value'], required);
|
||||||
return option?.value ?? null;
|
return option?.value ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,7 +157,7 @@ class CommandInteractionOptionResolver {
|
|||||||
* @returns {?User} The value of the option, or null if not set and not required.
|
* @returns {?User} The value of the option, or null if not set and not required.
|
||||||
*/
|
*/
|
||||||
getUser(name, required = false) {
|
getUser(name, required = false) {
|
||||||
const option = this._getTypedOption(name, ['USER'], ['user'], required);
|
const option = this._getTypedOption(name, 'USER', ['user'], required);
|
||||||
return option?.user ?? null;
|
return option?.user ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -169,7 +169,7 @@ class CommandInteractionOptionResolver {
|
|||||||
* The value of the option, or null if not set and not required.
|
* The value of the option, or null if not set and not required.
|
||||||
*/
|
*/
|
||||||
getMember(name, required = false) {
|
getMember(name, required = false) {
|
||||||
const option = this._getTypedOption(name, ['USER'], ['member'], required);
|
const option = this._getTypedOption(name, 'USER', ['member'], required);
|
||||||
return option?.member ?? null;
|
return option?.member ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -180,7 +180,7 @@ class CommandInteractionOptionResolver {
|
|||||||
* @returns {?(Role|APIRole)} The value of the option, or null if not set and not required.
|
* @returns {?(Role|APIRole)} The value of the option, or null if not set and not required.
|
||||||
*/
|
*/
|
||||||
getRole(name, required = false) {
|
getRole(name, required = false) {
|
||||||
const option = this._getTypedOption(name, ['ROLE'], ['role'], required);
|
const option = this._getTypedOption(name, 'ROLE', ['role'], required);
|
||||||
return option?.role ?? null;
|
return option?.role ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -192,7 +192,7 @@ class CommandInteractionOptionResolver {
|
|||||||
* The value of the option, or null if not set and not required.
|
* The value of the option, or null if not set and not required.
|
||||||
*/
|
*/
|
||||||
getMentionable(name, required = false) {
|
getMentionable(name, required = false) {
|
||||||
const option = this._getTypedOption(name, ['MENTIONABLE'], ['user', 'member', 'role'], required);
|
const option = this._getTypedOption(name, 'MENTIONABLE', ['user', 'member', 'role'], required);
|
||||||
return option?.member ?? option?.user ?? option?.role ?? null;
|
return option?.member ?? option?.user ?? option?.role ?? null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
4
typings/index.d.ts
vendored
4
typings/index.d.ts
vendored
@@ -432,13 +432,13 @@ export class CommandInteractionOptionResolver {
|
|||||||
private _subCommand: string | null;
|
private _subCommand: string | null;
|
||||||
private _getTypedOption(
|
private _getTypedOption(
|
||||||
name: string,
|
name: string,
|
||||||
types: ApplicationCommandOptionType[],
|
type: ApplicationCommandOptionType,
|
||||||
properties: (keyof ApplicationCommandOption)[],
|
properties: (keyof ApplicationCommandOption)[],
|
||||||
required: true,
|
required: true,
|
||||||
): CommandInteractionOption;
|
): CommandInteractionOption;
|
||||||
private _getTypedOption(
|
private _getTypedOption(
|
||||||
name: string,
|
name: string,
|
||||||
types: ApplicationCommandOptionType[],
|
type: ApplicationCommandOptionType,
|
||||||
properties: (keyof ApplicationCommandOption)[],
|
properties: (keyof ApplicationCommandOption)[],
|
||||||
required: boolean,
|
required: boolean,
|
||||||
): CommandInteractionOption | null;
|
): CommandInteractionOption | null;
|
||||||
|
|||||||
Reference in New Issue
Block a user