Skip to content

Commit

Permalink
feat(ApplicationCommand): add support for min and max values (#6855)
Browse files Browse the repository at this point in the history
Co-authored-by: Noel <buechler.noel@outlook.com>
Co-authored-by: Suneet Tipirneni <77477100+suneettipirneni@users.noreply.github.com>
  • Loading branch information
3 people committed Nov 11, 2021
1 parent 3c857a6 commit c30a818
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
20 changes: 15 additions & 5 deletions src/structures/ApplicationCommand.js
Expand Up @@ -136,6 +136,10 @@ class ApplicationCommand extends Base {

/**
* 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
* @property {ApplicationCommandOptionType|number} type The type of the option
* @property {string} name The name of the option
Expand All @@ -146,10 +150,8 @@ class ApplicationCommand extends Base {
* @property {ApplicationCommandOptionData[]} [options] Additional options if this option is a subcommand (group)
* @property {ChannelType[]|number[]} [channelTypes] When the option type is channel,
* the allowed types of channels that can be selected
* @property {number[]} [channel_types] When the option type is channel,
* the API data for allowed types of channels that can be selected
* <warn>This is provided for compatibility with something like `@discordjs/builders`
* and will be discarded when `channelTypes` is present</warn>
* @property {number} [minValue] The minimum value for an `INTEGER` or `NUMBER` option
* @property {number} [maxValue] The maximum value for an `INTEGER` or `NUMBER` option
*/

/**
Expand Down Expand Up @@ -261,7 +263,9 @@ class ApplicationCommand extends Base {
existing.required ||
option.choices?.length !== existing.choices?.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;
}
Expand Down Expand Up @@ -311,6 +315,8 @@ class ApplicationCommand extends Base {
* @property {ApplicationCommandOption[]} [options] Additional options if this option is a subcommand (group)
* @property {ChannelType[]} [channelTypes] When the option type is channel,
* 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
*/

/**
Expand All @@ -330,6 +336,8 @@ class ApplicationCommand extends Base {
static transformOption(option, received) {
const stringType = typeof option.type === 'string' ? option.type : ApplicationCommandOptionTypes[option.type];
const channelTypesKey = received ? 'channelTypes' : 'channel_types';
const minValueKey = received ? 'minValue' : 'min_value';
const maxValueKey = received ? 'maxValue' : 'max_value';
return {
type: typeof option.type === 'number' && !received ? option.type : ApplicationCommandOptionTypes[option.type],
name: option.name,
Expand All @@ -344,6 +352,8 @@ class ApplicationCommand extends Base {
: option.channelTypes?.map(type => (typeof type === 'string' ? ChannelTypes[type] : type)) ??
// When transforming to API data, accept API data
option.channel_types,
[minValueKey]: option.minValue ?? option.min_value,
[maxValueKey]: option.maxValue ?? option.max_value,
};
}
}
Expand Down
23 changes: 21 additions & 2 deletions typings/index.d.ts
Expand Up @@ -3289,10 +3289,13 @@ export type CommandOptionDataTypeResolvable = ApplicationCommandOptionType | App
export type CommandOptionChannelResolvableType = ApplicationCommandOptionTypes.CHANNEL | 'CHANNEL';

export type CommandOptionChoiceResolvableType =
| ApplicationCommandOptionTypes.NUMBER
| 'NUMBER'
| ApplicationCommandOptionTypes.STRING
| 'STRING'
| CommandOptionNumericResolvableType;

export type CommandOptionNumericResolvableType =
| ApplicationCommandOptionTypes.NUMBER
| 'NUMBER'
| ApplicationCommandOptionTypes.INTEGER
| 'INTEGER';

Expand Down Expand Up @@ -3354,6 +3357,20 @@ export interface ApplicationCommandChoicesOption extends BaseApplicationCommandO
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'> {
type: 'SUB_COMMAND_GROUP' | ApplicationCommandOptionTypes.SUB_COMMAND_GROUP;
options?: ApplicationCommandSubCommandData[];
Expand Down Expand Up @@ -3387,13 +3404,15 @@ export type ApplicationCommandOptionData =
| ApplicationCommandNonOptionsData
| ApplicationCommandChannelOptionData
| ApplicationCommandChoicesData
| ApplicationCommandNumericOptionData
| ApplicationCommandSubCommandData;

export type ApplicationCommandOption =
| ApplicationCommandSubGroup
| ApplicationCommandNonOptions
| ApplicationCommandChannelOption
| ApplicationCommandChoicesOption
| ApplicationCommandNumericOption
| ApplicationCommandSubCommand;

export interface ApplicationCommandOptionChoice {
Expand Down

0 comments on commit c30a818

Please sign in to comment.