mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-16 19:43:29 +01:00
feat(ApplicationCommand): add support for min and max values (#6855)
Co-authored-by: Noel <buechler.noel@outlook.com> Co-authored-by: Suneet Tipirneni <77477100+suneettipirneni@users.noreply.github.com>
This commit is contained in:
@@ -136,6 +136,10 @@ class ApplicationCommand extends Base {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* An option for an application command or subcommand.
|
* An option for an application command or subcommand.
|
||||||
|
* <info>In addition to the listed properties, when used as a parameter,
|
||||||
|
* API style `snake_case` properties can be used for compatibility with generators like `@discordjs/builders`.</info>
|
||||||
|
* <warn>Note that providing a value for the `camelCase` counterpart for any `snake_case` property
|
||||||
|
* will discard the provided `snake_case` property.</warn>
|
||||||
* @typedef {Object} ApplicationCommandOptionData
|
* @typedef {Object} ApplicationCommandOptionData
|
||||||
* @property {ApplicationCommandOptionType|number} type The type of the option
|
* @property {ApplicationCommandOptionType|number} type The type of the option
|
||||||
* @property {string} name The name of the option
|
* @property {string} name The name of the option
|
||||||
@@ -146,10 +150,8 @@ class ApplicationCommand extends Base {
|
|||||||
* @property {ApplicationCommandOptionData[]} [options] Additional options if this option is a subcommand (group)
|
* @property {ApplicationCommandOptionData[]} [options] Additional options if this option is a subcommand (group)
|
||||||
* @property {ChannelType[]|number[]} [channelTypes] When the option type is channel,
|
* @property {ChannelType[]|number[]} [channelTypes] When the option type is channel,
|
||||||
* the allowed types of channels that can be selected
|
* the allowed types of channels that can be selected
|
||||||
* @property {number[]} [channel_types] When the option type is channel,
|
* @property {number} [minValue] The minimum value for an `INTEGER` or `NUMBER` option
|
||||||
* the API data for allowed types of channels that can be selected
|
* @property {number} [maxValue] The maximum value for an `INTEGER` or `NUMBER` option
|
||||||
* <warn>This is provided for compatibility with something like `@discordjs/builders`
|
|
||||||
* and will be discarded when `channelTypes` is present</warn>
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -261,7 +263,9 @@ class ApplicationCommand extends Base {
|
|||||||
existing.required ||
|
existing.required ||
|
||||||
option.choices?.length !== existing.choices?.length ||
|
option.choices?.length !== existing.choices?.length ||
|
||||||
option.options?.length !== existing.options?.length ||
|
option.options?.length !== existing.options?.length ||
|
||||||
(option.channelTypes ?? option.channel_types)?.length !== existing.channelTypes?.length
|
(option.channelTypes ?? option.channel_types)?.length !== existing.channelTypes?.length ||
|
||||||
|
(option.minValue ?? option.min_value) !== existing.minValue ||
|
||||||
|
(option.maxValue ?? option.max_value) !== existing.maxValue
|
||||||
) {
|
) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -311,6 +315,8 @@ class ApplicationCommand extends Base {
|
|||||||
* @property {ApplicationCommandOption[]} [options] Additional options if this option is a subcommand (group)
|
* @property {ApplicationCommandOption[]} [options] Additional options if this option is a subcommand (group)
|
||||||
* @property {ChannelType[]} [channelTypes] When the option type is channel,
|
* @property {ChannelType[]} [channelTypes] When the option type is channel,
|
||||||
* the allowed types of channels that can be selected
|
* the allowed types of channels that can be selected
|
||||||
|
* @property {number} [minValue] The minimum value for an `INTEGER` or `NUMBER` option
|
||||||
|
* @property {number} [maxValue] The maximum value for an `INTEGER` or `NUMBER` option
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -330,6 +336,8 @@ class ApplicationCommand extends Base {
|
|||||||
static transformOption(option, received) {
|
static transformOption(option, received) {
|
||||||
const stringType = typeof option.type === 'string' ? option.type : ApplicationCommandOptionTypes[option.type];
|
const stringType = typeof option.type === 'string' ? option.type : ApplicationCommandOptionTypes[option.type];
|
||||||
const channelTypesKey = received ? 'channelTypes' : 'channel_types';
|
const channelTypesKey = received ? 'channelTypes' : 'channel_types';
|
||||||
|
const minValueKey = received ? 'minValue' : 'min_value';
|
||||||
|
const maxValueKey = received ? 'maxValue' : 'max_value';
|
||||||
return {
|
return {
|
||||||
type: typeof option.type === 'number' && !received ? option.type : ApplicationCommandOptionTypes[option.type],
|
type: typeof option.type === 'number' && !received ? option.type : ApplicationCommandOptionTypes[option.type],
|
||||||
name: option.name,
|
name: option.name,
|
||||||
@@ -344,6 +352,8 @@ class ApplicationCommand extends Base {
|
|||||||
: option.channelTypes?.map(type => (typeof type === 'string' ? ChannelTypes[type] : type)) ??
|
: option.channelTypes?.map(type => (typeof type === 'string' ? ChannelTypes[type] : type)) ??
|
||||||
// When transforming to API data, accept API data
|
// When transforming to API data, accept API data
|
||||||
option.channel_types,
|
option.channel_types,
|
||||||
|
[minValueKey]: option.minValue ?? option.min_value,
|
||||||
|
[maxValueKey]: option.maxValue ?? option.max_value,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
23
typings/index.d.ts
vendored
23
typings/index.d.ts
vendored
@@ -3289,10 +3289,13 @@ export type CommandOptionDataTypeResolvable = ApplicationCommandOptionType | App
|
|||||||
export type CommandOptionChannelResolvableType = ApplicationCommandOptionTypes.CHANNEL | 'CHANNEL';
|
export type CommandOptionChannelResolvableType = ApplicationCommandOptionTypes.CHANNEL | 'CHANNEL';
|
||||||
|
|
||||||
export type CommandOptionChoiceResolvableType =
|
export type CommandOptionChoiceResolvableType =
|
||||||
| ApplicationCommandOptionTypes.NUMBER
|
|
||||||
| 'NUMBER'
|
|
||||||
| ApplicationCommandOptionTypes.STRING
|
| ApplicationCommandOptionTypes.STRING
|
||||||
| 'STRING'
|
| 'STRING'
|
||||||
|
| CommandOptionNumericResolvableType;
|
||||||
|
|
||||||
|
export type CommandOptionNumericResolvableType =
|
||||||
|
| ApplicationCommandOptionTypes.NUMBER
|
||||||
|
| 'NUMBER'
|
||||||
| ApplicationCommandOptionTypes.INTEGER
|
| ApplicationCommandOptionTypes.INTEGER
|
||||||
| 'INTEGER';
|
| 'INTEGER';
|
||||||
|
|
||||||
@@ -3354,6 +3357,20 @@ export interface ApplicationCommandChoicesOption extends BaseApplicationCommandO
|
|||||||
choices?: ApplicationCommandOptionChoice[];
|
choices?: ApplicationCommandOptionChoice[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface ApplicationCommandNumericOptionData extends ApplicationCommandChoicesData {
|
||||||
|
type: CommandOptionNumericResolvableType;
|
||||||
|
minValue?: number;
|
||||||
|
min_value?: number;
|
||||||
|
maxValue?: number;
|
||||||
|
max_value?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ApplicationCommandNumericOption extends ApplicationCommandChoicesOption {
|
||||||
|
type: Exclude<CommandOptionNumericResolvableType, ApplicationCommandOptionTypes>;
|
||||||
|
minValue?: number;
|
||||||
|
maxValue?: number;
|
||||||
|
}
|
||||||
|
|
||||||
export interface ApplicationCommandSubGroupData extends Omit<BaseApplicationCommandOptionsData, 'required'> {
|
export interface ApplicationCommandSubGroupData extends Omit<BaseApplicationCommandOptionsData, 'required'> {
|
||||||
type: 'SUB_COMMAND_GROUP' | ApplicationCommandOptionTypes.SUB_COMMAND_GROUP;
|
type: 'SUB_COMMAND_GROUP' | ApplicationCommandOptionTypes.SUB_COMMAND_GROUP;
|
||||||
options?: ApplicationCommandSubCommandData[];
|
options?: ApplicationCommandSubCommandData[];
|
||||||
@@ -3387,6 +3404,7 @@ export type ApplicationCommandOptionData =
|
|||||||
| ApplicationCommandNonOptionsData
|
| ApplicationCommandNonOptionsData
|
||||||
| ApplicationCommandChannelOptionData
|
| ApplicationCommandChannelOptionData
|
||||||
| ApplicationCommandChoicesData
|
| ApplicationCommandChoicesData
|
||||||
|
| ApplicationCommandNumericOptionData
|
||||||
| ApplicationCommandSubCommandData;
|
| ApplicationCommandSubCommandData;
|
||||||
|
|
||||||
export type ApplicationCommandOption =
|
export type ApplicationCommandOption =
|
||||||
@@ -3394,6 +3412,7 @@ export type ApplicationCommandOption =
|
|||||||
| ApplicationCommandNonOptions
|
| ApplicationCommandNonOptions
|
||||||
| ApplicationCommandChannelOption
|
| ApplicationCommandChannelOption
|
||||||
| ApplicationCommandChoicesOption
|
| ApplicationCommandChoicesOption
|
||||||
|
| ApplicationCommandNumericOption
|
||||||
| ApplicationCommandSubCommand;
|
| ApplicationCommandSubCommand;
|
||||||
|
|
||||||
export interface ApplicationCommandOptionChoice {
|
export interface ApplicationCommandOptionChoice {
|
||||||
|
|||||||
Reference in New Issue
Block a user