Skip to content

Commit

Permalink
revert: support for nested arrays of components, fix error handling (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
monbrey authored Jul 12, 2021
1 parent dee5c83 commit 1dcad05
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 23 deletions.
7 changes: 3 additions & 4 deletions src/structures/BaseMessageComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,10 @@ class BaseMessageComponent {
* Constructs a MessageComponent based on the type of the incoming data
* @param {MessageComponentOptions} data Data for a MessageComponent
* @param {Client|WebhookClient} [client] Client constructing this component
* @param {boolean} [skipValidation=false] Whether or not to validate the component type
* @returns {?MessageComponent}
* @private
*/
static create(data, client, skipValidation = false) {
static create(data, client) {
let component;
let type = data.type;

Expand All @@ -66,7 +65,7 @@ class BaseMessageComponent {
switch (type) {
case MessageComponentTypes.ACTION_ROW: {
const MessageActionRow = require('./MessageActionRow');
component = new MessageActionRow(data);
component = new MessageActionRow(data, client);
break;
}
case MessageComponentTypes.BUTTON: {
Expand All @@ -82,7 +81,7 @@ class BaseMessageComponent {
default:
if (client) {
client.emit(Events.DEBUG, `[BaseMessageComponent] Received component with unknown type: ${data.type}`);
} else if (!skipValidation) {
} else {
throw new TypeError('INVALID_TYPE', 'data.type', 'valid MessageComponentType');
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/structures/Message.js
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ class Message extends Base {
* @property {MessageAttachment[]} [attachments] An array of attachments to keep,
* all attachments will be kept if omitted
* @property {FileOptions[]|BufferResolvable[]|MessageAttachment[]} [files] Files to add to the message
* @property {MessageActionRow[]|MessageActionRowOptions[]|MessageActionRowComponentResolvable[][]} [components]
* @property {MessageActionRow[]|MessageActionRowOptions[]} [components]
* Action rows containing interactive components for the message (buttons, select menus)
*/

Expand Down
13 changes: 5 additions & 8 deletions src/structures/MessageActionRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,16 @@ class MessageActionRow extends BaseMessageComponent {

/**
* @param {MessageActionRow|MessageActionRowOptions} [data={}] MessageActionRow to clone or raw data
* @param {Client} [client] The client constructing this MessageActionRow, if provided
*/
constructor(data = {}) {
constructor(data = {}, client = null) {
super({ type: 'ACTION_ROW' });

/**
* The components in this action row
* @type {MessageActionRowComponent[]}
*/
this.components = data.components?.map(c => BaseMessageComponent.create(c, null, true)) ?? [];
this.components = data.components?.map(c => BaseMessageComponent.create(c, client)) ?? [];
}

/**
Expand All @@ -54,7 +55,7 @@ class MessageActionRow extends BaseMessageComponent {
* @returns {MessageActionRow}
*/
addComponents(...components) {
this.components.push(...components.flat(Infinity).map(c => BaseMessageComponent.create(c, null, true)));
this.components.push(...components.flat(Infinity).map(c => BaseMessageComponent.create(c)));
return this;
}

Expand All @@ -66,11 +67,7 @@ class MessageActionRow extends BaseMessageComponent {
* @returns {MessageActionRow}
*/
spliceComponents(index, deleteCount, ...components) {
this.components.splice(
index,
deleteCount,
...components.flat(Infinity).map(c => BaseMessageComponent.create(c, null, true)),
);
this.components.splice(index, deleteCount, ...components.flat(Infinity).map(c => BaseMessageComponent.create(c)));
return this;
}

Expand Down
7 changes: 1 addition & 6 deletions src/structures/MessagePayload.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const BaseMessageComponent = require('./BaseMessageComponent');
const MessageEmbed = require('./MessageEmbed');
const { RangeError } = require('../errors');
const { MessageComponentTypes } = require('../util/Constants');
const DataResolver = require('../util/DataResolver');
const MessageFlags = require('../util/MessageFlags');
const Util = require('../util/Util');
Expand Down Expand Up @@ -138,11 +137,7 @@ class MessagePayload {
}
}

const components = this.options.components?.map(c =>
BaseMessageComponent.create(
Array.isArray(c) ? { type: MessageComponentTypes.ACTION_ROW, components: c } : c,
).toJSON(),
);
const components = this.options.components?.map(c => BaseMessageComponent.create(c).toJSON());

let username;
let avatarURL;
Expand Down
2 changes: 1 addition & 1 deletion src/structures/Webhook.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class Webhook {
* @property {string} [content] See {@link BaseMessageOptions#content}
* @property {FileOptions[]|BufferResolvable[]|MessageAttachment[]} [files] See {@link BaseMessageOptions#files}
* @property {MessageMentionOptions} [allowedMentions] See {@link BaseMessageOptions#allowedMentions}
* @property {MessageActionRow[]|MessageActionRowOptions[]|MessageActionRowComponentResolvable[][]} [components]
* @property {MessageActionRow[]|MessageActionRowOptions[]} [components]
* Action rows containing interactive components for the message (buttons, select menus)
*/

Expand Down
2 changes: 1 addition & 1 deletion src/structures/interfaces/TextBasedChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class TextBasedChannel {
* @property {MessageMentionOptions} [allowedMentions] Which mentions should be parsed from the message content
* (see [here](https://discord.com/developers/docs/resources/channel#allowed-mentions-object) for more details)
* @property {FileOptions[]|BufferResolvable[]|MessageAttachment[]} [files] Files to send with the message
* @property {MessageActionRow[]|MessageActionRowOptions[]|MessageActionRowComponentResolvable[][]} [components]
* @property {MessageActionRow[]|MessageActionRowOptions[]} [components]
* Action rows containing interactive components for the message (buttons, select menus)
*/

Expand Down
4 changes: 2 additions & 2 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3519,7 +3519,7 @@ export interface MessageEditOptions {
files?: (FileOptions | BufferResolvable | Stream | MessageAttachment)[];
flags?: BitFieldResolvable<MessageFlagsString, number>;
allowedMentions?: MessageMentionOptions;
components?: (MessageActionRow | MessageActionRowOptions | MessageActionRowComponentResolvable[])[];
components?: (MessageActionRow | MessageActionRowOptions)[];
}

export interface MessageEmbedAuthor {
Expand Down Expand Up @@ -3618,7 +3618,7 @@ export interface MessageOptions {
nonce?: string | number;
content?: string | null;
embeds?: (MessageEmbed | MessageEmbedOptions)[];
components?: (MessageActionRow | MessageActionRowOptions | MessageActionRowComponentResolvable[])[];
components?: (MessageActionRow | MessageActionRowOptions)[];
allowedMentions?: MessageMentionOptions;
files?: (FileOptions | BufferResolvable | Stream | MessageAttachment)[];
reply?: ReplyOptions;
Expand Down
1 change: 1 addition & 0 deletions typings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ client.on('interaction', async interaction => {

await interaction.reply({ content: 'Hi!', components: [actionRow] });

// @ts-expect-error
await interaction.reply({ content: 'Hi!', components: [[button]] });

// @ts-expect-error
Expand Down

0 comments on commit 1dcad05

Please sign in to comment.