mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-17 12:03:31 +01:00
feat(CommandInteractionOptionResolver): add channelTypes option to getChannel (#8934)
* feat(CommandInteractionOptionResolver): add `channelTypes` option to `getChannel` * fix: thread types Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
@@ -135,6 +135,7 @@
|
||||
* @property {'CommandInteractionOptionEmpty'} CommandInteractionOptionEmpty
|
||||
* @property {'CommandInteractionOptionNoSubcommand'} CommandInteractionOptionNoSubcommand
|
||||
* @property {'CommandInteractionOptionNoSubcommandGroup'} CommandInteractionOptionNoSubcommandGroup
|
||||
* @property {'CommandInteractionOptionInvalidChannelType'} CommandInteractionOptionInvalidChannelType
|
||||
* @property {'AutocompleteInteractionOptionNoFocusedOption'} AutocompleteInteractionOptionNoFocusedOption
|
||||
|
||||
* @property {'ModalSubmitInteractionFieldNotFound'} ModalSubmitInteractionFieldNotFound
|
||||
@@ -281,6 +282,7 @@ const keys = [
|
||||
'CommandInteractionOptionEmpty',
|
||||
'CommandInteractionOptionNoSubcommand',
|
||||
'CommandInteractionOptionNoSubcommandGroup',
|
||||
'CommandInteractionOptionInvalidChannelType',
|
||||
'AutocompleteInteractionOptionNoFocusedOption',
|
||||
|
||||
'ModalSubmitInteractionFieldNotFound',
|
||||
|
||||
@@ -145,6 +145,8 @@ const Messages = {
|
||||
`Required option "${name}" is of type: ${type}; expected a non-empty value.`,
|
||||
[DjsErrorCodes.CommandInteractionOptionNoSubcommand]: 'No subcommand specified for interaction.',
|
||||
[DjsErrorCodes.CommandInteractionOptionNoSubcommandGroup]: 'No subcommand group specified for interaction.',
|
||||
[DjsErrorCodes.CommandInteractionOptionInvalidChannelType]: (name, type, expected) =>
|
||||
`The type of channel of the option "${name}" is: ${type}; expected ${expected}.`,
|
||||
[DjsErrorCodes.AutocompleteInteractionOptionNoFocusedOption]: 'No focused option for autocomplete interaction.',
|
||||
|
||||
[DjsErrorCodes.ModalSubmitInteractionFieldNotFound]: customId =>
|
||||
|
||||
@@ -142,12 +142,24 @@ class CommandInteractionOptionResolver {
|
||||
* Gets a channel option.
|
||||
* @param {string} name The name of the option.
|
||||
* @param {boolean} [required=false] Whether to throw an error if the option is not found.
|
||||
* @param {ChannelType[]} [channelTypes=[]] The allowed types of channels. If empty, all channel types are allowed.
|
||||
* @returns {?(GuildChannel|ThreadChannel|APIChannel)}
|
||||
* The value of the option, or null if not set and not required.
|
||||
*/
|
||||
getChannel(name, required = false) {
|
||||
getChannel(name, required = false, channelTypes = []) {
|
||||
const option = this._getTypedOption(name, [ApplicationCommandOptionType.Channel], ['channel'], required);
|
||||
return option?.channel ?? null;
|
||||
const channel = option?.channel ?? null;
|
||||
|
||||
if (channel && channelTypes.length > 0 && !channelTypes.includes(channel.type)) {
|
||||
throw new DiscordjsTypeError(
|
||||
ErrorCodes.CommandInteractionOptionInvalidChannelType,
|
||||
name,
|
||||
channel.type,
|
||||
channelTypes.join(', '),
|
||||
);
|
||||
}
|
||||
|
||||
return channel;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user