feat(CommandInteraction): add CommandInteractionOptionResolver (#6107)

Co-authored-by: Sugden <28943913+NotSugden@users.noreply.github.com>
This commit is contained in:
Shino
2021-07-18 14:16:32 -04:00
committed by GitHub
parent 4d53d0fd11
commit f293132345
4 changed files with 263 additions and 21 deletions

View File

@@ -1,9 +1,9 @@
'use strict';
const CommandInteractionOptionResolver = require('./CommandInteractionOptionResolver');
const Interaction = require('./Interaction');
const InteractionWebhook = require('./InteractionWebhook');
const InteractionResponses = require('./interfaces/InteractionResponses');
const Collection = require('../util/Collection');
const { ApplicationCommandOptionTypes } = require('../util/Constants');
/**
@@ -48,9 +48,12 @@ class CommandInteraction extends Interaction {
/**
* The options passed to the command.
* @type {Collection<string, CommandInteractionOption>}
* @type {CommandInteractionOptionResolver}
*/
this.options = this._createOptionsCollection(data.data.options, data.data.resolved);
this.options = new CommandInteractionOptionResolver(
this.client,
data.data.options?.map(option => this.transformOption(option, data.data.resolved)),
);
/**
* Whether this interaction has already been replied to
@@ -108,7 +111,7 @@ class CommandInteraction extends Interaction {
};
if ('value' in option) result.value = option.value;
if ('options' in option) result.options = this._createOptionsCollection(option.options, resolved);
if ('options' in option) result.options = option.options.map(opt => this.transformOption(opt, resolved));
if (resolved) {
const user = resolved.users?.[option.value];
@@ -127,22 +130,6 @@ class CommandInteraction extends Interaction {
return result;
}
/**
* Creates a collection of options from the received options array.
* @param {APIApplicationCommandOption[]} options The received options
* @param {APIApplicationCommandOptionResolved} resolved The resolved interaction data
* @returns {Collection<string, CommandInteractionOption>}
* @private
*/
_createOptionsCollection(options, resolved) {
const optionsCollection = new Collection();
if (typeof options === 'undefined') return optionsCollection;
for (const option of options) {
optionsCollection.set(option.name, this.transformOption(option, resolved));
}
return optionsCollection;
}
// These are here only for documentation purposes - they are implemented by InteractionResponses
/* eslint-disable no-empty-function */
defer() {}