Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(CommandInteraction): add CommandInteractionOptionResolver #6107

Merged
merged 18 commits into from Jul 18, 2021
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 13 additions & 11 deletions src/structures/CommandInteraction.js
@@ -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');

/**
Expand Down Expand Up @@ -48,9 +48,11 @@ 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._createOptionsArray(data.data.options, data.data.resolved),
);
memikri marked this conversation as resolved.
Show resolved Hide resolved

/**
* Whether this interaction has already been replied to
Expand Down Expand Up @@ -108,7 +110,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 = this._createOptionsArray(option.options, resolved);
memikri marked this conversation as resolved.
Show resolved Hide resolved

if (resolved) {
const user = resolved.users?.[option.value];
Expand All @@ -128,19 +130,19 @@ class CommandInteraction extends Interaction {
}

/**
* Creates a collection of options from the received options array.
* Creates an array of options from the received API options array.
* @param {APIApplicationCommandOption[]} options The received options
* @param {APIApplicationCommandOptionResolved} resolved The resolved interaction data
* @returns {Collection<string, CommandInteractionOption>}
* @returns {CommandInteractionOption[]}
* @private
*/
_createOptionsCollection(options, resolved) {
const optionsCollection = new Collection();
if (typeof options === 'undefined') return optionsCollection;
_createOptionsArray(options, resolved) {
const optionsArray = [];
if (typeof options === 'undefined') return optionsArray;
for (const option of options) {
optionsCollection.set(option.name, this.transformOption(option, resolved));
optionsArray.push(this.transformOption(option, resolved));
}
return optionsCollection;
return optionsArray;
}

// These are here only for documentation purposes - they are implemented by InteractionResponses
Expand Down
159 changes: 159 additions & 0 deletions src/structures/CommandInteractionOptionResolver.js
@@ -0,0 +1,159 @@
'use strict';

/**
* A resolver for command interaction options.
*/
class CommandInteractionOptionResolver {
constructor(options) {
memikri marked this conversation as resolved.
Show resolved Hide resolved
/**
* The interaction options array.
* @type {CommandInteractionOption[]}
* @private
*/
this.options = options ?? [];
memikri marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Gets an option by its name.
* @param {string} name The name of the option.
* @param {boolean} required Whether to throw an error if the option is not found.
memikri marked this conversation as resolved.
Show resolved Hide resolved
* @returns {?CommandInteractionOption} The option, if found.
*/
get(name, required = false) {
const option = this.options.find(opt => opt.name === name);
if (option) {
return option;
} else if (required) {
throw new Error(`Missing required option "${name}"`);
memikri marked this conversation as resolved.
Show resolved Hide resolved
} else {
return null;
memikri marked this conversation as resolved.
Show resolved Hide resolved
}
}

/**
* Gets an option by name and property and checks its type.
* @param {string} name The name of the option.
* @param {ApplicationCommandOptionType[]} types The type of the option.
* @param {string[]} properties The property to check for for `required`.
memikri marked this conversation as resolved.
Show resolved Hide resolved
* @param {boolean} required Whether to throw an error if the option is not found.
memikri marked this conversation as resolved.
Show resolved Hide resolved
* @returns {*|null} The value of `property`.
memikri marked this conversation as resolved.
Show resolved Hide resolved
* @private
*/
_getTypedOption(name, types, properties, required) {
const option = this.get(name, required);
if (option) {
if (!types.includes(option.type)) {
throw new Error(`Option "${name}" is of type "${option.type}"; expected ${types.join('|')}.`);
memikri marked this conversation as resolved.
Show resolved Hide resolved
} else if (required && properties.every(prop => option[prop] === null || typeof option[prop] === 'undefined')) {
throw new Error(`Option "${name}" of type "${option.type}" is required, but is empty.`);
} else {
return option;
}
} else {
return option;
}
}

/**
* Gets a sub-command or sub-command group.
* @param {string} name The name of the sub-command or sub-command group.
* @returns {CommandInteractionOptionResolver|null}
memikri marked this conversation as resolved.
Show resolved Hide resolved
* A new resolver for the sub-command/group's options, or null if empty
*/
getSubCommand(name) {
memikri marked this conversation as resolved.
Show resolved Hide resolved
const option = this._getTypedOption(name, ['SUB_COMMAND', 'SUB_COMMAND_GROUP'], ['options'], false);
memikri marked this conversation as resolved.
Show resolved Hide resolved
return option ? new CommandInteractionOptionResolver(option.options) : null;
}

/**
* Gets a boolean option.
* @param {string} name The name of the option.
* @param {boolean} required Whether to throw an error if the option is not found.
memikri marked this conversation as resolved.
Show resolved Hide resolved
* @returns {boolean|null} The value of the option, or null if not set and not required.
memikri marked this conversation as resolved.
Show resolved Hide resolved
*/
getBoolean(name, required = false) {
const option = this._getTypedOption(name, ['BOOLEAN'], ['value'], required);
return option?.value ?? null;
memikri marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Gets a channel option.
* @param {string} name The name of the option.
* @param {boolean} required Whether to throw an error if the option is not found.
memikri marked this conversation as resolved.
Show resolved Hide resolved
* @returns {GuildChannel|APIInteractionDataResolvedChannel|null}
memikri marked this conversation as resolved.
Show resolved Hide resolved
* The value of the option, or null if not set and not required.
*/
getChannel(name, required = false) {
const option = this._getTypedOption(name, ['CHANNEL'], ['channel'], required);
return option?.channel ?? null;
memikri marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Gets a string option.
* @param {string} name The name of the option.
* @param {boolean} required Whether to throw an error if the option is not found.
memikri marked this conversation as resolved.
Show resolved Hide resolved
* @returns {string|null} The value of the option, or null if not set and not required.
memikri marked this conversation as resolved.
Show resolved Hide resolved
*/
getString(name, required = false) {
const option = this._getTypedOption(name, ['STRING'], ['value'], required);
return option?.value ?? null;
memikri marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Gets an integer option.
* @param {string} name The name of the option.
* @param {boolean} required Whether to throw an error if the option is not found.
memikri marked this conversation as resolved.
Show resolved Hide resolved
* @returns {number|null} The value of the option, or null if not set and not required.
memikri marked this conversation as resolved.
Show resolved Hide resolved
*/
getInteger(name, required = false) {
const option = this._getTypedOption(name, ['INTEGER'], ['value'], required);
return option?.value ?? null;
memikri marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Gets a user option.
* @param {string} name The name of the option.
* @param {boolean} required Whether to throw an error if the option is not found.
memikri marked this conversation as resolved.
Show resolved Hide resolved
* @returns {User|null} The value of the option, or null if not set and not required.
memikri marked this conversation as resolved.
Show resolved Hide resolved
*/
getUser(name, required = false) {
const option = this._getTypedOption(name, ['USER'], ['user'], required);
return option?.user ?? null;
memikri marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Gets a member option.
* @param {string} name The name of the option.
* @param {boolean} required Whether to throw an error if the option is not found.
memikri marked this conversation as resolved.
Show resolved Hide resolved
* @returns {GuildMember|APIInteractionDataResolvedGuildMember|null}
memikri marked this conversation as resolved.
Show resolved Hide resolved
* The value of the option, or null if not set and not required.
*/
getMember(name, required = false) {
const option = this._getTypedOption(name, ['MEMBER'], ['member'], required);
return option?.member ?? null;
memikri marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Gets a role option.
* @param {string} name The name of the option.
* @param {boolean} required Whether to throw an error if the option is not found.
memikri marked this conversation as resolved.
Show resolved Hide resolved
* @returns {Role|APIRole|null} The value of the option, or null if not set and not required.
memikri marked this conversation as resolved.
Show resolved Hide resolved
*/
getRole(name, required = false) {
const option = this._getTypedOption(name, ['ROLE'], ['role'], required);
return option?.role ?? null;
memikri marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Gets a mentionable option.
* @param {string} name The name of the option.
* @param {boolean} required Whether to throw an error if the option is not found.
memikri marked this conversation as resolved.
Show resolved Hide resolved
* @returns {User|GuildMember|Role|null} The value of the option, or null if not set and not required.
memikri marked this conversation as resolved.
Show resolved Hide resolved
*/
getMentionable(name, required = false) {
const option = this._getTypedOption(name, ['MENTIONABLE'], ['user', 'member', 'role'], required);
return option?.user ?? option?.member ?? option?.role ?? null;
memikri marked this conversation as resolved.
Show resolved Hide resolved
}
}

module.exports = CommandInteractionOptionResolver;
52 changes: 51 additions & 1 deletion typings/index.d.ts
Expand Up @@ -416,7 +416,7 @@ export class CommandInteraction extends Interaction {
public commandName: string;
public deferred: boolean;
public ephemeral: boolean | null;
public options: Collection<string, CommandInteractionOption>;
public options: CommandInteractionOptionResolver;
public replied: boolean;
public webhook: InteractionWebhook;
public defer(options?: InteractionDeferOptions & { fetchReply: true }): Promise<Message | APIMessage>;
Expand All @@ -431,6 +431,56 @@ export class CommandInteraction extends Interaction {
private _createOptionsCollection(options: unknown, resolved: unknown): Collection<string, CommandInteractionOption>;
}

export class CommandInteractionOptionResolver {
constructor(options: CommandInteractionOption[]);
memikri marked this conversation as resolved.
Show resolved Hide resolved
private options: CommandInteractionOption[];
private _getTypedOption(
name: string,
types: ApplicationCommandOptionType[],
properties: (keyof ApplicationCommandOption)[],
required: true,
): CommandInteractionOption;
private _getTypedOption(
name: string,
types: ApplicationCommandOptionType[],
properties: (keyof ApplicationCommandOption)[],
required: boolean,
): CommandInteractionOption | null;
memikri marked this conversation as resolved.
Show resolved Hide resolved

public get(name: string, required: true): CommandInteractionOption;
memikri marked this conversation as resolved.
Show resolved Hide resolved
public get(name: string, required?: boolean): CommandInteractionOption | null;
public getSubCommand(name: string): CommandInteractionOptionResolver | null;
public getBoolean(name: string, required: true): boolean;
public getBoolean(name: string, required?: boolean): boolean | null;
public getChannel(name: string, required: true): NonNullable<CommandInteractionOption['channel']>;
public getChannel(name: string, required?: boolean): NonNullable<CommandInteractionOption['channel']> | null;
public getString(name: string, required: true): string;
public getString(name: string, required?: boolean): string | null;
public getInteger(name: string, required: true): number;
public getInteger(name: string, required?: boolean): number | null;
public getUser(name: string, required: true): NonNullable<CommandInteractionOption['user']>;
public getUser(name: string, required?: boolean): NonNullable<CommandInteractionOption['user']> | null;
public getMember(name: string, required: true): NonNullable<CommandInteractionOption['member']>;
public getMember(name: string, required?: boolean): NonNullable<CommandInteractionOption['member']> | null;
public getRole(name: string, required: true): NonNullable<CommandInteractionOption['role']>;
public getRole(name: string, required?: boolean): NonNullable<CommandInteractionOption['role']> | null;
public getMentionable(
name: string,
required: true,
):
| NonNullable<CommandInteractionOption['member']>
| NonNullable<CommandInteractionOption['role']>
| NonNullable<CommandInteractionOption['user']>;
public getMentionable(
name: string,
required?: boolean,
):
| NonNullable<CommandInteractionOption['member']>
| NonNullable<CommandInteractionOption['role']>
| NonNullable<CommandInteractionOption['user']>
| null;
memikri marked this conversation as resolved.
Show resolved Hide resolved
}

export class DataResolver extends null {
private constructor();
public static resolveBase64(data: Base64Resolvable): string;
Expand Down