Skip to content

Commit

Permalink
feat(*): document and support embeds field in message create endpoint (
Browse files Browse the repository at this point in the history
…#5792)

Co-authored-by: ckohen <chaikohen@gmail.com>
Co-authored-by: Jan <66554238+vaporox@users.noreply.github.com>
  • Loading branch information
3 people committed Jun 11, 2021
1 parent 6df3623 commit 99ff715
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
29 changes: 19 additions & 10 deletions src/structures/APIMessage.js
Expand Up @@ -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);
Expand All @@ -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(
Expand Down Expand Up @@ -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,
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/structures/interfaces/TextBasedChannel.js
Expand Up @@ -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
*/
Expand Down
7 changes: 3 additions & 4 deletions typings/index.d.ts
Expand Up @@ -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<MessageFlagsString, number>;
Expand Down Expand Up @@ -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)[];
Expand Down Expand Up @@ -3754,10 +3754,9 @@ declare module 'discord.js' {
'content' | 'embeds' | 'files' | 'allowedMentions' | 'components'
>;

interface WebhookMessageOptions extends Omit<MessageOptions, 'embed' | 'reply'> {
interface WebhookMessageOptions extends Omit<MessageOptions, 'reply'> {
username?: string;
avatarURL?: string;
embeds?: (MessageEmbed | unknown)[];
}

type WebhookTypes = 'Incoming' | 'Channel Follower';
Expand Down
6 changes: 3 additions & 3 deletions typings/index.ts
Expand Up @@ -34,13 +34,13 @@ declare const assertIsMessageArray: (m: Promise<Message[]>) => 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 }));

Expand Down

0 comments on commit 99ff715

Please sign in to comment.