feat(CommandInteraction): make options a collection (#5705)

This commit is contained in:
Shubham Parihar
2021-06-02 22:14:50 +05:30
committed by GitHub
parent 5141ea4f06
commit fdad140997
2 changed files with 23 additions and 5 deletions

View File

@@ -4,6 +4,7 @@ const APIMessage = require('./APIMessage');
const Interaction = require('./Interaction'); const Interaction = require('./Interaction');
const WebhookClient = require('../client/WebhookClient'); const WebhookClient = require('../client/WebhookClient');
const { Error } = require('../errors'); const { Error } = require('../errors');
const Collection = require('../util/Collection');
const { ApplicationCommandOptionTypes, InteractionResponseTypes } = require('../util/Constants'); const { ApplicationCommandOptionTypes, InteractionResponseTypes } = require('../util/Constants');
const MessageFlags = require('../util/MessageFlags'); const MessageFlags = require('../util/MessageFlags');
@@ -42,9 +43,9 @@ class CommandInteraction extends Interaction {
/** /**
* The options passed to the command. * The options passed to the command.
* @type {CommandInteractionOption[]} * @type {Collection<string, CommandInteractionOption>}
*/ */
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 * 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 {string} name The name of the option
* @property {ApplicationCommandOptionType} type The type of the option * @property {ApplicationCommandOptionType} type The type of the option
* @property {string|number|boolean} [value] The value 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<string, CommandInteractionOption>} [options] Additional options if this option is a
* subcommand (group)
* @property {User} [user] The resolved user * @property {User} [user] The resolved user
* @property {GuildMember|Object} [member] The resolved member * @property {GuildMember|Object} [member] The resolved member
* @property {GuildChannel|Object} [channel] The resolved channel * @property {GuildChannel|Object} [channel] The resolved channel
@@ -233,7 +235,7 @@ class CommandInteraction extends Interaction {
}; };
if ('value' in option) result.value = option.value; 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]; const user = resolved?.users?.[option.value];
if (user) result.user = this.client.users.add(user); if (user) result.user = this.client.users.add(user);
@@ -249,6 +251,21 @@ class CommandInteraction extends Interaction {
return result; 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<string, CommandInteractionOption>}
* @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; module.exports = CommandInteraction;

3
typings/index.d.ts vendored
View File

@@ -441,7 +441,7 @@ declare module 'discord.js' {
public commandID: string; public commandID: string;
public commandName: string; public commandName: string;
public deferred: boolean; public deferred: boolean;
public options: CommandInteractionOption[]; public options: Collection<string, CommandInteractionOption>;
public replied: boolean; public replied: boolean;
public webhook: WebhookClient; public webhook: WebhookClient;
public defer(options?: InteractionDeferOptions): Promise<void>; public defer(options?: InteractionDeferOptions): Promise<void>;
@@ -458,6 +458,7 @@ declare module 'discord.js' {
public reply(content: string | null | APIMessage | InteractionReplyOptions | MessageAdditions): Promise<void>; public reply(content: string | null | APIMessage | InteractionReplyOptions | MessageAdditions): Promise<void>;
public reply(content: string | null, options?: InteractionReplyOptions): Promise<void>; public reply(content: string | null, options?: InteractionReplyOptions): Promise<void>;
private transformOption(option: unknown, resolved: unknown): CommandInteractionOption; private transformOption(option: unknown, resolved: unknown): CommandInteractionOption;
private _createOptionsCollection(options: unknown, resolved: unknown): Collection<string, CommandInteractionOption>;
} }
type AllowedImageFormat = 'webp' | 'png' | 'jpg' | 'jpeg' | 'gif'; type AllowedImageFormat = 'webp' | 'png' | 'jpg' | 'jpeg' | 'gif';