From 99ff7151379fe03a1cfd52f252c0e6fc892d7776 Mon Sep 17 00:00:00 2001 From: Adrian Castro Date: Sat, 12 Jun 2021 00:45:17 +0200 Subject: [PATCH] feat(*): document and support embeds field in message create endpoint (#5792) Co-authored-by: ckohen Co-authored-by: Jan <66554238+vaporox@users.noreply.github.com> --- src/structures/APIMessage.js | 29 ++++++++++++------- src/structures/interfaces/TextBasedChannel.js | 2 +- typings/index.d.ts | 7 ++--- typings/index.ts | 6 ++-- 4 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/structures/APIMessage.js b/src/structures/APIMessage.js index 9b2faccda..1c26df9b9 100644 --- a/src/structures/APIMessage.js +++ b/src/structures/APIMessage.js @@ -129,7 +129,6 @@ class APIMessage { if (this.data) return this; const isInteraction = this.isInteraction; const isWebhook = this.isWebhook; - const isWebhookLike = isInteraction || isWebhook; const content = this.makeContent(); const tts = Boolean(this.options.tts); @@ -144,14 +143,9 @@ class APIMessage { } const embedLikes = []; - if (isWebhookLike) { - if (this.options.embeds) { - embedLikes.push(...this.options.embeds); - } - } else if (this.options.embed) { - embedLikes.push(this.options.embed); + if (this.options.embeds) { + embedLikes.push(...this.options.embeds); } - const embeds = embedLikes.map(e => new MessageEmbed(e).toJSON()); const components = this.options.components?.map(c => BaseMessageComponent.create( @@ -202,8 +196,7 @@ class APIMessage { content, tts, nonce, - embed: !isWebhookLike ? (this.options.embed === null ? null : embeds[0]) : undefined, - embeds: isWebhookLike ? embeds : undefined, + embeds: this.options.embeds?.map(embed => new MessageEmbed(embed).toJSON()), components, username, avatar_url: avatarURL, @@ -223,6 +216,22 @@ class APIMessage { async resolveFiles() { if (this.files) return this; + const embedLikes = []; + + if (this.options.embeds) { + embedLikes.push(...this.options.embeds); + } + + const fileLikes = []; + if (this.options.files) { + fileLikes.push(...this.options.files); + } + for (const embed of embedLikes) { + if (embed.files) { + fileLikes.push(...embed.files); + } + } + this.files = await Promise.all(this.options.files?.map(file => this.constructor.resolveFile(file)) ?? []); return this; } diff --git a/src/structures/interfaces/TextBasedChannel.js b/src/structures/interfaces/TextBasedChannel.js index af2b99c39..b76517bed 100644 --- a/src/structures/interfaces/TextBasedChannel.js +++ b/src/structures/interfaces/TextBasedChannel.js @@ -70,7 +70,7 @@ class TextBasedChannel { /** * Options provided when sending or editing a message. * @typedef {BaseMessageOptions} MessageOptions - * @property {MessageEmbed|Object} [embed] An embed for the message + * @property {MessageEmbed[]|Object[]} [embeds] The embeds for the message * (see [here](https://discord.com/developers/docs/resources/channel#embed-object) for more details) * @property {ReplyOptions} [reply] The options for replying to a message */ diff --git a/typings/index.d.ts b/typings/index.d.ts index 0a72b621a..7266cf4b0 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -3256,7 +3256,7 @@ declare module 'discord.js' { interface MessageEditOptions { attachments?: MessageAttachment[]; content?: string | null; - embed?: MessageEmbed | MessageEmbedOptions | null; + embeds?: (MessageEmbed | MessageEmbedOptions)[] | null; code?: string | boolean; files?: (FileOptions | BufferResolvable | Stream | MessageAttachment)[]; flags?: BitFieldResolvable; @@ -3352,7 +3352,7 @@ declare module 'discord.js' { tts?: boolean; nonce?: string | number; content?: string; - embed?: MessageEmbed | MessageEmbedOptions; + embeds?: (MessageEmbed | MessageEmbedOptions)[]; components?: MessageActionRow[] | MessageActionRowOptions[] | MessageActionRowComponentResolvable[][]; allowedMentions?: MessageMentionOptions; files?: (FileOptions | BufferResolvable | Stream | MessageAttachment)[]; @@ -3754,10 +3754,9 @@ declare module 'discord.js' { 'content' | 'embeds' | 'files' | 'allowedMentions' | 'components' >; - interface WebhookMessageOptions extends Omit { + interface WebhookMessageOptions extends Omit { username?: string; avatarURL?: string; - embeds?: (MessageEmbed | unknown)[]; } type WebhookTypes = 'Incoming' | 'Channel Follower'; diff --git a/typings/index.ts b/typings/index.ts index c3a4c21a7..6787e1404 100644 --- a/typings/index.ts +++ b/typings/index.ts @@ -34,13 +34,13 @@ declare const assertIsMessageArray: (m: Promise) => void; client.on('message', ({ channel }) => { assertIsMessage(channel.send('string')); assertIsMessage(channel.send({})); - assertIsMessage(channel.send({ embed: {} })); + assertIsMessage(channel.send({ embeds: [] })); const attachment = new MessageAttachment('file.png'); const embed = new MessageEmbed(); assertIsMessage(channel.send({ files: [attachment] })); - assertIsMessage(channel.send({ embed })); - assertIsMessage(channel.send({ embed, files: [attachment] })); + assertIsMessage(channel.send({ embeds: [embed] })); + assertIsMessage(channel.send({ embeds: [embed], files: [attachment] })); assertIsMessageArray(channel.send({ split: true }));