mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-18 04:23:31 +01:00
refactor(CommandInteractionOptionResolver): add readonly data property (#6156)
This commit is contained in:
@@ -52,7 +52,7 @@ class CommandInteraction extends Interaction {
|
|||||||
*/
|
*/
|
||||||
this.options = new CommandInteractionOptionResolver(
|
this.options = new CommandInteractionOptionResolver(
|
||||||
this.client,
|
this.client,
|
||||||
data.data.options?.map(option => this.transformOption(option, data.data.resolved)),
|
data.data.options?.map(option => this.transformOption(option, data.data.resolved)) ?? [],
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -15,13 +15,6 @@ class CommandInteractionOptionResolver {
|
|||||||
*/
|
*/
|
||||||
Object.defineProperty(this, 'client', { value: client });
|
Object.defineProperty(this, 'client', { value: client });
|
||||||
|
|
||||||
/**
|
|
||||||
* The interaction options array.
|
|
||||||
* @type {CommandInteractionOption[]}
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
this._options = options ?? [];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The name of the sub-command group.
|
* The name of the sub-command group.
|
||||||
* @type {?string}
|
* @type {?string}
|
||||||
@@ -35,14 +28,33 @@ class CommandInteractionOptionResolver {
|
|||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
this._subCommand = null;
|
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') {
|
// Hoist sub-command if present
|
||||||
this._subCommand = this._options[0].name;
|
if (this._hoistedOptions[0]?.type === 'SUB_COMMAND') {
|
||||||
this._options = this._options[0].options ?? [];
|
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.
|
* @returns {?CommandInteractionOption} The option, if found.
|
||||||
*/
|
*/
|
||||||
get(name, required = false) {
|
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 (!option) {
|
||||||
if (required) {
|
if (required) {
|
||||||
throw new TypeError('COMMAND_INTERACTION_OPTION_NOT_FOUND', name);
|
throw new TypeError('COMMAND_INTERACTION_OPTION_NOT_FOUND', name);
|
||||||
|
|||||||
3
typings/index.d.ts
vendored
3
typings/index.d.ts
vendored
@@ -427,8 +427,9 @@ export class CommandInteraction extends Interaction {
|
|||||||
export class CommandInteractionOptionResolver {
|
export class CommandInteractionOptionResolver {
|
||||||
public constructor(client: Client, options: CommandInteractionOption[]);
|
public constructor(client: Client, options: CommandInteractionOption[]);
|
||||||
public readonly client: Client;
|
public readonly client: Client;
|
||||||
private _options: CommandInteractionOption[];
|
public readonly data: readonly CommandInteractionOption[];
|
||||||
private _group: string | null;
|
private _group: string | null;
|
||||||
|
private _hoistedOptions: CommandInteractionOption[];
|
||||||
private _subCommand: string | null;
|
private _subCommand: string | null;
|
||||||
private _getTypedOption(
|
private _getTypedOption(
|
||||||
name: string,
|
name: string,
|
||||||
|
|||||||
@@ -656,6 +656,7 @@ client.on('interactionCreate', async interaction => {
|
|||||||
if (interaction.isCommand()) {
|
if (interaction.isCommand()) {
|
||||||
assertType<CommandInteraction>(interaction);
|
assertType<CommandInteraction>(interaction);
|
||||||
assertType<CommandInteractionOptionResolver>(interaction.options);
|
assertType<CommandInteractionOptionResolver>(interaction.options);
|
||||||
|
assertType<readonly CommandInteractionOption[]>(interaction.options.data);
|
||||||
|
|
||||||
const optionalOption = interaction.options.get('name');
|
const optionalOption = interaction.options.get('name');
|
||||||
const requiredOption = interaction.options.get('name', true);
|
const requiredOption = interaction.options.get('name', true);
|
||||||
|
|||||||
Reference in New Issue
Block a user