mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-19 04:53:30 +01:00
feat(ApplicationCommand): add support for channel_types (#6640)
Co-authored-by: Tiemen <ThaTiemsz@users.noreply.github.com> Co-authored-by: Sugden <28943913+NotSugden@users.noreply.github.com>
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
const Base = require('./Base');
|
const Base = require('./Base');
|
||||||
const ApplicationCommandPermissionsManager = require('../managers/ApplicationCommandPermissionsManager');
|
const ApplicationCommandPermissionsManager = require('../managers/ApplicationCommandPermissionsManager');
|
||||||
const { ApplicationCommandOptionTypes, ApplicationCommandTypes } = require('../util/Constants');
|
const { ApplicationCommandOptionTypes, ApplicationCommandTypes, ChannelTypes } = require('../util/Constants');
|
||||||
const SnowflakeUtil = require('../util/SnowflakeUtil');
|
const SnowflakeUtil = require('../util/SnowflakeUtil');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -131,6 +131,12 @@ class ApplicationCommand extends Base {
|
|||||||
* @property {boolean} [required] Whether the option is required
|
* @property {boolean} [required] Whether the option is required
|
||||||
* @property {ApplicationCommandOptionChoice[]} [choices] The choices of the option for the user to pick from
|
* @property {ApplicationCommandOptionChoice[]} [choices] The choices of the option for the user to pick from
|
||||||
* @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,
|
||||||
|
* 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>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -239,7 +245,8 @@ class ApplicationCommand extends Base {
|
|||||||
(option.required ?? (['SUB_COMMAND', 'SUB_COMMAND_GROUP'].includes(optionType) ? undefined : false)) !==
|
(option.required ?? (['SUB_COMMAND', 'SUB_COMMAND_GROUP'].includes(optionType) ? undefined : false)) !==
|
||||||
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
|
||||||
) {
|
) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -262,6 +269,15 @@ class ApplicationCommand extends Base {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (existing.channelTypes) {
|
||||||
|
const newTypes = (option.channelTypes ?? option.channel_types).map(type =>
|
||||||
|
typeof type === 'number' ? ChannelTypes[type] : type,
|
||||||
|
);
|
||||||
|
for (const type of existing.channelTypes) {
|
||||||
|
if (!newTypes.includes(type)) return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (existing.options) {
|
if (existing.options) {
|
||||||
return this.optionsEqual(existing.options, option.options, enforceOptionOrder);
|
return this.optionsEqual(existing.options, option.options, enforceOptionOrder);
|
||||||
}
|
}
|
||||||
@@ -277,6 +293,8 @@ class ApplicationCommand extends Base {
|
|||||||
* @property {boolean} [required] Whether the option is required
|
* @property {boolean} [required] Whether the option is required
|
||||||
* @property {ApplicationCommandOptionChoice[]} [choices] The choices of the option for the user to pick from
|
* @property {ApplicationCommandOptionChoice[]} [choices] The choices of the option for the user to pick from
|
||||||
* @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,
|
||||||
|
* the allowed types of channels that can be selected
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -295,6 +313,7 @@ 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';
|
||||||
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,
|
||||||
@@ -303,6 +322,11 @@ class ApplicationCommand extends Base {
|
|||||||
option.required ?? (stringType === 'SUB_COMMAND' || stringType === 'SUB_COMMAND_GROUP' ? undefined : false),
|
option.required ?? (stringType === 'SUB_COMMAND' || stringType === 'SUB_COMMAND_GROUP' ? undefined : false),
|
||||||
choices: option.choices,
|
choices: option.choices,
|
||||||
options: option.options?.map(o => this.transformOption(o, received)),
|
options: option.options?.map(o => this.transformOption(o, received)),
|
||||||
|
[channelTypesKey]: received
|
||||||
|
? option.channel_types?.map(type => ChannelTypes[type])
|
||||||
|
: option.channelTypes?.map(type => (typeof type === 'string' ? ChannelTypes[type] : type)) ??
|
||||||
|
// When transforming to API data, accept API data
|
||||||
|
option.channel_types,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
45
typings/index.d.ts
vendored
45
typings/index.d.ts
vendored
@@ -3032,6 +3032,8 @@ export interface BaseApplicationCommandData {
|
|||||||
|
|
||||||
export type CommandOptionDataTypeResolvable = ApplicationCommandOptionType | ApplicationCommandOptionTypes;
|
export type CommandOptionDataTypeResolvable = ApplicationCommandOptionType | ApplicationCommandOptionTypes;
|
||||||
|
|
||||||
|
export type CommandOptionChannelResolvableType = ApplicationCommandOptionTypes.CHANNEL | 'CHANNEL';
|
||||||
|
|
||||||
export type CommandOptionChoiceResolvableType =
|
export type CommandOptionChoiceResolvableType =
|
||||||
| ApplicationCommandOptionTypes.NUMBER
|
| ApplicationCommandOptionTypes.NUMBER
|
||||||
| 'NUMBER'
|
| 'NUMBER'
|
||||||
@@ -3048,7 +3050,7 @@ export type CommandOptionSubOptionResolvableType =
|
|||||||
|
|
||||||
export type CommandOptionNonChoiceResolvableType = Exclude<
|
export type CommandOptionNonChoiceResolvableType = Exclude<
|
||||||
CommandOptionDataTypeResolvable,
|
CommandOptionDataTypeResolvable,
|
||||||
CommandOptionChoiceResolvableType | CommandOptionSubOptionResolvableType
|
CommandOptionChoiceResolvableType | CommandOptionSubOptionResolvableType | CommandOptionChannelResolvableType
|
||||||
>;
|
>;
|
||||||
|
|
||||||
export interface BaseApplicationCommandOptionsData {
|
export interface BaseApplicationCommandOptionsData {
|
||||||
@@ -3076,35 +3078,68 @@ export type ApplicationCommandData =
|
|||||||
| MessageApplicationCommandData
|
| MessageApplicationCommandData
|
||||||
| ChatInputApplicationCommandData;
|
| ChatInputApplicationCommandData;
|
||||||
|
|
||||||
|
export interface ApplicationCommandChannelOptionData extends BaseApplicationCommandOptionsData {
|
||||||
|
type: CommandOptionChannelResolvableType;
|
||||||
|
channelTypes?: (keyof typeof ChannelTypes | ChannelTypes)[];
|
||||||
|
channel_types?: ChannelTypes[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ApplicationCommandChannelOption extends BaseApplicationCommandOptionsData {
|
||||||
|
type: 'CHANNEL';
|
||||||
|
channelTypes?: (keyof typeof ChannelTypes)[];
|
||||||
|
}
|
||||||
|
|
||||||
export interface ApplicationCommandChoicesData extends BaseApplicationCommandOptionsData {
|
export interface ApplicationCommandChoicesData extends BaseApplicationCommandOptionsData {
|
||||||
type: CommandOptionChoiceResolvableType;
|
type: CommandOptionChoiceResolvableType;
|
||||||
choices?: ApplicationCommandOptionChoice[];
|
choices?: ApplicationCommandOptionChoice[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface ApplicationCommandChoicesOption extends BaseApplicationCommandOptionsData {
|
||||||
|
type: Exclude<CommandOptionChoiceResolvableType, ApplicationCommandOptionTypes>;
|
||||||
|
choices?: ApplicationCommandOptionChoice[];
|
||||||
|
}
|
||||||
|
|
||||||
export interface ApplicationCommandSubGroupData extends BaseApplicationCommandOptionsData {
|
export interface ApplicationCommandSubGroupData extends BaseApplicationCommandOptionsData {
|
||||||
type: 'SUB_COMMAND_GROUP' | ApplicationCommandOptionTypes.SUB_COMMAND_GROUP;
|
type: 'SUB_COMMAND_GROUP' | ApplicationCommandOptionTypes.SUB_COMMAND_GROUP;
|
||||||
options?: ApplicationCommandSubCommandData[];
|
options?: ApplicationCommandSubCommandData[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface ApplicationCommandSubGroup extends BaseApplicationCommandOptionsData {
|
||||||
|
type: 'SUB_COMMAND_GROUP';
|
||||||
|
options?: ApplicationCommandSubCommand[];
|
||||||
|
}
|
||||||
|
|
||||||
export interface ApplicationCommandSubCommandData extends BaseApplicationCommandOptionsData {
|
export interface ApplicationCommandSubCommandData extends BaseApplicationCommandOptionsData {
|
||||||
type: 'SUB_COMMAND' | ApplicationCommandOptionTypes.SUB_COMMAND;
|
type: 'SUB_COMMAND' | ApplicationCommandOptionTypes.SUB_COMMAND;
|
||||||
options?: (ApplicationCommandChoicesData | ApplicationCommandNonOptionsData)[];
|
options?: (ApplicationCommandChoicesData | ApplicationCommandNonOptionsData)[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface ApplicationCommandSubCommand extends BaseApplicationCommandOptionsData {
|
||||||
|
type: 'SUB_COMMAND';
|
||||||
|
options?: (ApplicationCommandChoicesOption | ApplicationCommandNonOptions)[];
|
||||||
|
}
|
||||||
|
|
||||||
export interface ApplicationCommandNonOptionsData extends BaseApplicationCommandOptionsData {
|
export interface ApplicationCommandNonOptionsData extends BaseApplicationCommandOptionsData {
|
||||||
type: CommandOptionNonChoiceResolvableType;
|
type: CommandOptionNonChoiceResolvableType;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface ApplicationCommandNonOptions extends BaseApplicationCommandOptionsData {
|
||||||
|
type: Exclude<CommandOptionNonChoiceResolvableType, ApplicationCommandOptionTypes>;
|
||||||
|
}
|
||||||
|
|
||||||
export type ApplicationCommandOptionData =
|
export type ApplicationCommandOptionData =
|
||||||
| ApplicationCommandSubGroupData
|
| ApplicationCommandSubGroupData
|
||||||
| ApplicationCommandNonOptionsData
|
| ApplicationCommandNonOptionsData
|
||||||
|
| ApplicationCommandChannelOptionData
|
||||||
| ApplicationCommandChoicesData
|
| ApplicationCommandChoicesData
|
||||||
| ApplicationCommandSubCommandData;
|
| ApplicationCommandSubCommandData;
|
||||||
|
|
||||||
export type ApplicationCommandOption = ApplicationCommandOptionData & {
|
export type ApplicationCommandOption =
|
||||||
type: ApplicationCommandOptionType;
|
| ApplicationCommandSubGroup
|
||||||
options?: ApplicationCommandOption[];
|
| ApplicationCommandNonOptions
|
||||||
};
|
| ApplicationCommandChannelOption
|
||||||
|
| ApplicationCommandChoicesOption
|
||||||
|
| ApplicationCommandSubCommand;
|
||||||
|
|
||||||
export interface ApplicationCommandOptionChoice {
|
export interface ApplicationCommandOptionChoice {
|
||||||
name: string;
|
name: string;
|
||||||
|
|||||||
Reference in New Issue
Block a user