diff --git a/src/structures/CommandInteraction.js b/src/structures/CommandInteraction.js index 54d6b943c941..d79dea06aec3 100644 --- a/src/structures/CommandInteraction.js +++ b/src/structures/CommandInteraction.js @@ -4,6 +4,7 @@ const APIMessage = require('./APIMessage'); const Interaction = require('./Interaction'); const WebhookClient = require('../client/WebhookClient'); const { Error } = require('../errors'); +const Collection = require('../util/Collection'); const { ApplicationCommandOptionTypes, InteractionResponseTypes } = require('../util/Constants'); const MessageFlags = require('../util/MessageFlags'); @@ -42,9 +43,9 @@ class CommandInteraction extends Interaction { /** * The options passed to the command. - * @type {CommandInteractionOption[]} + * @type {Collection} */ - this.options = data.data.options?.map(o => this.transformOption(o, data.data.resolved)) ?? []; + this.options = this._createOptionsCollection(data.data.options, data.data.resolved); /** * Whether this interaction has already been replied to @@ -194,7 +195,8 @@ class CommandInteraction extends Interaction { * @property {string} name The name of the option * @property {ApplicationCommandOptionType} type The type of the option * @property {string|number|boolean} [value] The value of the option - * @property {CommandInteractionOption[]} [options] Additional options if this option is a subcommand (group) + * @property {Collection} [options] Additional options if this option is a + * subcommand (group) * @property {User} [user] The resolved user * @property {GuildMember|Object} [member] The resolved member * @property {GuildChannel|Object} [channel] The resolved channel @@ -233,7 +235,7 @@ class CommandInteraction extends Interaction { }; if ('value' in option) result.value = option.value; - if ('options' in option) result.options = option.options.map(o => this.transformOption(o, resolved)); + if ('options' in option) result.options = this._createOptionsCollection(option.options, resolved); const user = resolved?.users?.[option.value]; if (user) result.user = this.client.users.add(user); @@ -249,6 +251,21 @@ class CommandInteraction extends Interaction { return result; } + + /** + * Creates a collection of options from the received options array. + * @param {Object[]} options The received options + * @param {Object} resolved The resolved interaction data + * @returns {Collection} + * @private + */ + _createOptionsCollection(options, resolved) { + const optionsCollection = new Collection(); + for (const option of options) { + optionsCollection.set(option.name, this.transformOption(option, resolved)); + } + return optionsCollection; + } } module.exports = CommandInteraction; diff --git a/typings/index.d.ts b/typings/index.d.ts index 3c72238990d3..84367a85c69f 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -441,7 +441,7 @@ declare module 'discord.js' { public commandID: string; public commandName: string; public deferred: boolean; - public options: CommandInteractionOption[]; + public options: Collection; public replied: boolean; public webhook: WebhookClient; public defer(options?: InteractionDeferOptions): Promise; @@ -458,6 +458,7 @@ declare module 'discord.js' { public reply(content: string | null | APIMessage | InteractionReplyOptions | MessageAdditions): Promise; public reply(content: string | null, options?: InteractionReplyOptions): Promise; private transformOption(option: unknown, resolved: unknown): CommandInteractionOption; + private _createOptionsCollection(options: unknown, resolved: unknown): Collection; } type AllowedImageFormat = 'webp' | 'png' | 'jpg' | 'jpeg' | 'gif';