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:
Almeida
2022-12-20 20:32:45 +00:00
committed by GitHub
parent 25c27eac14
commit 429dbccc85
5 changed files with 66 additions and 4 deletions

View File

@@ -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',

View File

@@ -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 =>

View File

@@ -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;
}
/**