mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-11 09:03:29 +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 ApplicationCommandPermissionsManager = require('../managers/ApplicationCommandPermissionsManager');
|
||||
const { ApplicationCommandOptionTypes, ApplicationCommandTypes } = require('../util/Constants');
|
||||
const { ApplicationCommandOptionTypes, ApplicationCommandTypes, ChannelTypes } = require('../util/Constants');
|
||||
const SnowflakeUtil = require('../util/SnowflakeUtil');
|
||||
|
||||
/**
|
||||
@@ -131,6 +131,12 @@ class ApplicationCommand extends Base {
|
||||
* @property {boolean} [required] Whether the option is required
|
||||
* @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 {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)) !==
|
||||
existing.required ||
|
||||
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;
|
||||
}
|
||||
@@ -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) {
|
||||
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 {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 {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) {
|
||||
const stringType = typeof option.type === 'string' ? option.type : ApplicationCommandOptionTypes[option.type];
|
||||
const channelTypesKey = received ? 'channelTypes' : 'channel_types';
|
||||
return {
|
||||
type: typeof option.type === 'number' && !received ? option.type : ApplicationCommandOptionTypes[option.type],
|
||||
name: option.name,
|
||||
@@ -303,6 +322,11 @@ class ApplicationCommand extends Base {
|
||||
option.required ?? (stringType === 'SUB_COMMAND' || stringType === 'SUB_COMMAND_GROUP' ? undefined : false),
|
||||
choices: option.choices,
|
||||
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,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user