Skip to content

Commit 20fade2

Browse files
authored
fix: allowedMentions, container, media item toJSON() for components v2 (#10852)
* fix: allowedMentions for components v2 * refactor: passing allowed_mentions * Update packages/discord.js/src/structures/MessagePayload.js * fix: missing UnfurledMediaItem#toJSON() * fix: find interactive component in container * fix: recursive flatMap * fix: lint * refactor: top-level function * fix: jsdoc * fix: jsdoc
1 parent e827644 commit 20fade2

File tree

4 files changed

+37
-12
lines changed

4 files changed

+37
-12
lines changed

packages/discord.js/src/structures/MessagePayload.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,8 @@ class MessagePayload {
248248
components,
249249
username,
250250
avatar_url: avatarURL,
251-
allowed_mentions: content === undefined && message_reference === undefined ? undefined : allowedMentions,
251+
allowed_mentions:
252+
this.isMessage && this.target.author.id !== this.target.client.user.id ? undefined : allowedMentions,
252253
flags,
253254
message_reference,
254255
attachments: this.options.attachments,

packages/discord.js/src/structures/UnfurledMediaItem.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ class UnfurledMediaItem {
2020
get url() {
2121
return this.data.url;
2222
}
23+
24+
/**
25+
* Returns the API-compatible JSON for this media item
26+
* @returns {APIUnfurledMediaItem}
27+
*/
28+
toJSON() {
29+
return { ...this.data };
30+
}
2331
}
2432

2533
module.exports = UnfurledMediaItem;

packages/discord.js/src/util/Components.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -214,25 +214,36 @@ function createComponentBuilder(data) {
214214
}
215215
}
216216

217+
/**
218+
* Extracts all interactive components from the component tree
219+
* @param {Component|APIMessageComponent} component The component to find all interactive components in
220+
* @returns {Array<Component|APIMessageComponent>}
221+
* @ignore
222+
*/
223+
function extractInteractiveComponents(component) {
224+
switch (component.type) {
225+
case ComponentType.ActionRow:
226+
return component.components;
227+
case ComponentType.Section:
228+
return [...component.components, component.accessory];
229+
case ComponentType.Container:
230+
return component.components.flatMap(extractInteractiveComponents);
231+
default:
232+
return [component];
233+
}
234+
}
235+
217236
/**
218237
* Finds a component by customId in nested components
219238
* @param {Array<Component|APIMessageComponent>} components The components to search in
220239
* @param {string} customId The customId to search for
221240
* @returns {Component|APIMessageComponent}
241+
* @ignore
222242
*/
223243
function findComponentByCustomId(components, customId) {
224244
return (
225245
components
226-
.flatMap(component => {
227-
switch (component.type) {
228-
case ComponentType.ActionRow:
229-
return component.components;
230-
case ComponentType.Section:
231-
return [...component.components, component.accessory];
232-
default:
233-
return [component];
234-
}
235-
})
246+
.flatMap(extractInteractiveComponents)
236247
.find(component => (component.customId ?? component.custom_id) === customId) ?? null
237248
);
238249
}

packages/discord.js/typings/index.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7043,7 +7043,12 @@ export interface MessageEditAttachmentData {
70437043
export interface MessageEditOptions extends Omit<BaseMessageOptions, 'content'> {
70447044
content?: string | null;
70457045
attachments?: readonly (Attachment | MessageEditAttachmentData)[];
7046-
flags?: BitFieldResolvable<Extract<MessageFlagsString, 'SuppressEmbeds'>, MessageFlags.SuppressEmbeds> | undefined;
7046+
flags?:
7047+
| BitFieldResolvable<
7048+
Extract<MessageFlagsString, 'SuppressEmbeds' | 'IsComponentsV2'>,
7049+
MessageFlags.SuppressEmbeds | MessageFlags.IsComponentsV2
7050+
>
7051+
| undefined;
70477052
}
70487053

70497054
export type MessageReactionResolvable = MessageReaction | Snowflake | string;

0 commit comments

Comments
 (0)