Skip to content

Commit

Permalink
feat(CommandInteraction): make options a collection (#5705)
Browse files Browse the repository at this point in the history
  • Loading branch information
iShibi committed Jun 2, 2021
1 parent 5141ea4 commit fdad140
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
25 changes: 21 additions & 4 deletions src/structures/CommandInteraction.js
Expand Up @@ -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');

Expand Down Expand Up @@ -42,9 +43,9 @@ class CommandInteraction extends Interaction {

/**
* 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
Expand Down Expand Up @@ -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<string, CommandInteractionOption>} [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
Expand Down Expand Up @@ -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);
Expand All @@ -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<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;
3 changes: 2 additions & 1 deletion typings/index.d.ts
Expand Up @@ -441,7 +441,7 @@ declare module 'discord.js' {
public commandID: string;
public commandName: string;
public deferred: boolean;
public options: CommandInteractionOption[];
public options: Collection<string, CommandInteractionOption>;
public replied: boolean;
public webhook: WebhookClient;
public defer(options?: InteractionDeferOptions): Promise<void>;
Expand All @@ -458,6 +458,7 @@ declare module 'discord.js' {
public reply(content: string | null | APIMessage | InteractionReplyOptions | MessageAdditions): Promise<void>;
public reply(content: string | null, options?: InteractionReplyOptions): Promise<void>;
private transformOption(option: unknown, resolved: unknown): CommandInteractionOption;
private _createOptionsCollection(options: unknown, resolved: unknown): Collection<string, CommandInteractionOption>;
}

type AllowedImageFormat = 'webp' | 'png' | 'jpg' | 'jpeg' | 'gif';
Expand Down

0 comments on commit fdad140

Please sign in to comment.