Skip to content

Commit

Permalink
refactor(CommandInteractionOptionResolver): add readonly data property (
Browse files Browse the repository at this point in the history
  • Loading branch information
memikri committed Jul 21, 2021
1 parent 07017a9 commit 328501b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/structures/CommandInteraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) ?? [],
);

/**
Expand Down
40 changes: 26 additions & 14 deletions src/structures/CommandInteractionOptionResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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]) });
}

/**
Expand All @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,8 +427,9 @@ export class CommandInteraction extends Interaction {
export class CommandInteractionOptionResolver {
public constructor(client: Client, options: CommandInteractionOption[]);
public readonly client: Client;
private _options: CommandInteractionOption[];
public readonly data: readonly CommandInteractionOption[];
private _group: string | null;
private _hoistedOptions: CommandInteractionOption[];
private _subCommand: string | null;
private _getTypedOption(
name: string,
Expand Down
1 change: 1 addition & 0 deletions typings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,7 @@ client.on('interactionCreate', async interaction => {
if (interaction.isCommand()) {
assertType<CommandInteraction>(interaction);
assertType<CommandInteractionOptionResolver>(interaction.options);
assertType<readonly CommandInteractionOption[]>(interaction.options.data);

const optionalOption = interaction.options.get('name');
const requiredOption = interaction.options.get('name', true);
Expand Down

0 comments on commit 328501b

Please sign in to comment.