refactor(CommandInteractionOptionResolver): add readonly data property (#6156)

This commit is contained in:
Shino
2021-07-21 17:22:47 -04:00
committed by GitHub
parent 07017a9699
commit 328501bd8e
4 changed files with 30 additions and 16 deletions

View File

@@ -52,7 +52,7 @@ class CommandInteraction extends Interaction {
*/
this.options = new CommandInteractionOptionResolver(
this.client,
data.data.options?.map(option => this.transformOption(option, data.data.resolved)),
data.data.options?.map(option => this.transformOption(option, data.data.resolved)) ?? [],
);
/**

View File

@@ -15,13 +15,6 @@ class CommandInteractionOptionResolver {
*/
Object.defineProperty(this, 'client', { value: client });
/**
* The interaction options array.
* @type {CommandInteractionOption[]}
* @private
*/
this._options = options ?? [];
/**
* The name of the sub-command group.
* @type {?string}
@@ -35,14 +28,33 @@ class CommandInteractionOptionResolver {
* @private
*/
this._subCommand = null;
if (this._options[0]?.type === 'SUB_COMMAND_GROUP') {
this._group = this._options[0].name;
this._options = this._options[0].options ?? [];
/**
* The bottom-level options for the interaction.
* If there is a sub-command (or sub-command and group), this is the options for the sub-command.
* @type {CommandInteractionOption[]}
* @private
*/
this._hoistedOptions = options;
// Hoist sub-command group if present
if (this._hoistedOptions[0]?.type === 'SUB_COMMAND_GROUP') {
this._group = this._hoistedOptions[0].name;
this._hoistedOptions = this._hoistedOptions[0].options ?? [];
}
if (this._options[0]?.type === 'SUB_COMMAND') {
this._subCommand = this._options[0].name;
this._options = this._options[0].options ?? [];
// Hoist sub-command if present
if (this._hoistedOptions[0]?.type === 'SUB_COMMAND') {
this._subCommand = this._hoistedOptions[0].name;
this._hoistedOptions = this._hoistedOptions[0].options ?? [];
}
/**
* The interaction options array.
* @name CommandInteractionOptionResolver#data
* @type {ReadonlyArray<CommandInteractionOption>}
* @readonly
*/
Object.defineProperty(this, 'data', { value: Object.freeze([...options]) });
}
/**
@@ -52,7 +64,7 @@ class CommandInteractionOptionResolver {
* @returns {?CommandInteractionOption} The option, if found.
*/
get(name, required = false) {
const option = this._options.find(opt => opt.name === name);
const option = this._hoistedOptions.find(opt => opt.name === name);
if (!option) {
if (required) {
throw new TypeError('COMMAND_INTERACTION_OPTION_NOT_FOUND', name);