mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
feat(CommandInteraction): make options a collection (#5705)
This commit is contained in:
@@ -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<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
|
||||
@@ -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
|
||||
@@ -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<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
typings/index.d.ts
vendored
3
typings/index.d.ts
vendored
@@ -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>;
|
||||
@@ -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';
|
||||
|
||||
Reference in New Issue
Block a user