From 2d3e6ec271da6839d7b8cb74043aa63ef26d5a87 Mon Sep 17 00:00:00 2001 From: John Marlo Date: Fri, 1 Apr 2022 15:54:01 +0800 Subject: [PATCH 01/10] Remove default embeds option.. --- src/structures/MessagePayload.js | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/structures/MessagePayload.js b/src/structures/MessagePayload.js index 339b8f9..d9346b7 100644 --- a/src/structures/MessagePayload.js +++ b/src/structures/MessagePayload.js @@ -2,7 +2,7 @@ const { Buffer } = require('node:buffer'); const BaseMessageComponent = require('./BaseMessageComponent'); -const MessageEmbed = require('./MessageEmbed'); +const WebEmbed = require('./WebEmbed'); const { RangeError } = require('../errors'); const DataResolver = require('../util/DataResolver'); const MessageFlags = require('../util/MessageFlags'); @@ -188,11 +188,29 @@ class MessagePayload { this.options.attachments = attachments; } + if (this.options.embeds) { + //check if embeds is an array + if (!Array.isArray(this.options.embeds)) { + this.options.embeds = [this.options.embeds]; + } + + //check if the embeds is an array of WebEmbed + if (!this.options.embeds.every(e => e instanceof WebEmbed)) { + throw new TypeError('WEB_EMBEDS_TYPE'); + } + + //while loop to make sure all embeds will be added to the content + while (this.options.embeds.length) { + const embed = this.options.embeds.shift(); + const data = await embed.toMessage(true, true); //set to true hidden, shorten... + content +=`\n${data}` + } + } + this.data = { content, tts, nonce, - embeds: this.options.embeds?.map(embed => new MessageEmbed(embed).toJSON()), components, username, avatar_url: avatarURL, From 4bb60f3630835b8bd29b3f724cab77b8c26c1a77 Mon Sep 17 00:00:00 2001 From: John Marlo Date: Fri, 1 Apr 2022 15:55:32 +0800 Subject: [PATCH 02/10] Update TextBasedChannel.js --- src/structures/interfaces/TextBasedChannel.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/structures/interfaces/TextBasedChannel.js b/src/structures/interfaces/TextBasedChannel.js index 5d2daaa..0cba969 100644 --- a/src/structures/interfaces/TextBasedChannel.js +++ b/src/structures/interfaces/TextBasedChannel.js @@ -166,9 +166,9 @@ class TextBasedChannel { let messagePayload; if (options instanceof MessagePayload) { - messagePayload = options.resolveData(); + messagePayload = await options.resolveData(); } else { - messagePayload = MessagePayload.create(this, options).resolveData(); + messagePayload = await MessagePayload.create(this, options).resolveData(); } const { data, files } = await messagePayload.resolveFiles(); From 65b74654ea747eee5515ca58073462498b171c2b Mon Sep 17 00:00:00 2001 From: John Marlo Date: Fri, 1 Apr 2022 16:06:48 +0800 Subject: [PATCH 03/10] Update TextBasedChannel.js --- src/structures/interfaces/TextBasedChannel.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/structures/interfaces/TextBasedChannel.js b/src/structures/interfaces/TextBasedChannel.js index 0cba969..18712ee 100644 --- a/src/structures/interfaces/TextBasedChannel.js +++ b/src/structures/interfaces/TextBasedChannel.js @@ -58,7 +58,7 @@ class TextBasedChannel { * @property {boolean} [tts=false] Whether or not the message should be spoken aloud * @property {string} [nonce=''] The nonce for the message * @property {string} [content=''] The content for the message - * @property {MessageEmbed[]|APIEmbed[]} [embeds] The embeds for the message + * @property {WebEmbed[]|APIEmbed[]} [embeds] The embeds for the message * (see [here](https://discord.com/developers/docs/resources/channel#embed-object) for more details) * @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) From 6540a018e0221b862ec33fb0fb0bcaa7a4a4e51a Mon Sep 17 00:00:00 2001 From: John Marlo Date: Fri, 1 Apr 2022 16:14:20 +0800 Subject: [PATCH 04/10] Update index.d.ts --- typings/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/typings/index.d.ts b/typings/index.d.ts index 01ac852..832e63b 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -5247,7 +5247,7 @@ export interface MessageOptions { tts?: boolean; nonce?: string | number; content?: string | null; - embeds?: (MessageEmbed | MessageEmbedOptions | APIEmbed)[]; + embeds?: (WebEmbed)[]; components?: (MessageActionRow | (Required & MessageActionRowOptions))[]; allowedMentions?: MessageMentionOptions; files?: (FileOptions | BufferResolvable | Stream | MessageAttachment)[]; From 0bdc325a34bfb322fb3d43304443fce92e2d0642 Mon Sep 17 00:00:00 2001 From: John Marlo Date: Fri, 1 Apr 2022 16:18:02 +0800 Subject: [PATCH 05/10] Update TextBasedChannel.js --- src/structures/interfaces/TextBasedChannel.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/structures/interfaces/TextBasedChannel.js b/src/structures/interfaces/TextBasedChannel.js index 18712ee..747ef95 100644 --- a/src/structures/interfaces/TextBasedChannel.js +++ b/src/structures/interfaces/TextBasedChannel.js @@ -58,7 +58,7 @@ class TextBasedChannel { * @property {boolean} [tts=false] Whether or not the message should be spoken aloud * @property {string} [nonce=''] The nonce for the message * @property {string} [content=''] The content for the message - * @property {WebEmbed[]|APIEmbed[]} [embeds] The embeds for the message + * @property {WebEmbed[]} [embeds] The embeds for the message * (see [here](https://discord.com/developers/docs/resources/channel#embed-object) for more details) * @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) From 5953ec764e4ac87abc00e80fa8db90a815678cd6 Mon Sep 17 00:00:00 2001 From: John Marlo Date: Fri, 1 Apr 2022 16:32:50 +0800 Subject: [PATCH 06/10] fix error.. --- src/structures/MessagePayload.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/structures/MessagePayload.js b/src/structures/MessagePayload.js index d9346b7..67a28db 100644 --- a/src/structures/MessagePayload.js +++ b/src/structures/MessagePayload.js @@ -121,12 +121,12 @@ class MessagePayload { * Resolves data. * @returns {MessagePayload} */ - resolveData() { + async resolveData() { if (this.data) return this; const isInteraction = this.isInteraction; const isWebhook = this.isWebhook; - const content = this.makeContent(); + let content = this.makeContent(); const tts = Boolean(this.options.tts); let nonce; @@ -202,7 +202,7 @@ class MessagePayload { //while loop to make sure all embeds will be added to the content while (this.options.embeds.length) { const embed = this.options.embeds.shift(); - const data = await embed.toMessage(true, true); //set to true hidden, shorten... + const data = await embed.toMessage(); content +=`\n${data}` } } From f8ce147a52acaf44006a10c71f343926e3664406 Mon Sep 17 00:00:00 2001 From: John Marlo Date: Fri, 1 Apr 2022 16:41:06 +0800 Subject: [PATCH 07/10] Update WebEmbed.js --- src/structures/WebEmbed.js | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/structures/WebEmbed.js b/src/structures/WebEmbed.js index 1f71e3c..4028817 100644 --- a/src/structures/WebEmbed.js +++ b/src/structures/WebEmbed.js @@ -11,6 +11,18 @@ class WebEmbed { this._setup(data); } _setup(data) { + /** + * Shorten the link + * @type {?boolean} + */ + this.shorten = data.shorten ?? false; + + /** + * Hidden Embed link + * @type {?boolean} + */ + this.hidden = data.hidden ?? false; + /** * The title of this embed * @type {?string} @@ -277,12 +289,12 @@ class WebEmbed { ); } const fullURL = `${baseURL}${arrayQuery.join('&')}`; - if (shorten) { + if (this.shorten) { const url = await getShorten(fullURL); if (!url) console.log('Cannot shorten URL in WebEmbed'); - return hidden ? `${hiddenCharter} ${url || fullURL}` : (url || fullURL); + return this.hidden ? `${hiddenCharter} ${url || fullURL}` : (url || fullURL); } else { - return hidden ? `${hiddenCharter} ${fullURL}` : fullURL; + return this.hidden ? `${hiddenCharter} ${fullURL}` : fullURL; } } } @@ -301,4 +313,4 @@ const getShorten = async (url) => { } } -module.exports = WebEmbed; \ No newline at end of file +module.exports = WebEmbed; From 91a717b6353848a8b27728206e7dbd0ab96eaa21 Mon Sep 17 00:00:00 2001 From: John Marlo Date: Fri, 1 Apr 2022 16:42:03 +0800 Subject: [PATCH 08/10] Update WebEmbed.js --- src/structures/WebEmbed.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/structures/WebEmbed.js b/src/structures/WebEmbed.js index 4028817..23558f3 100644 --- a/src/structures/WebEmbed.js +++ b/src/structures/WebEmbed.js @@ -246,11 +246,9 @@ class WebEmbed { /** * Return Message Content + Embed (if hidden, pls check content length because it has 1000+ length) - * @param {boolean} hidden Hidden Embed link - * @param {boolean} shorten Shorten link ? * @returns {string} Message Content */ - async toMessage(hidden = false, shorten = true) { + async toMessage() { const arrayQuery = []; if (this.title) { arrayQuery.push(`title=${this.title}`); From dd372a2dd3c0f675369114f0346f20ca70782ef0 Mon Sep 17 00:00:00 2001 From: John Marlo Date: Fri, 1 Apr 2022 17:05:35 +0800 Subject: [PATCH 09/10] Update index.d.ts --- typings/index.d.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/typings/index.d.ts b/typings/index.d.ts index 832e63b..fee26d0 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -1761,7 +1761,7 @@ export class MessageEmbed { } export class WebEmbed { - public constructor(data?: MessageEmbed | MessageEmbedOptions | APIEmbed); + public constructor(data?: WebEmbed | WebEmbedOptions); public author: MessageEmbedAuthor | null; public color: number | null; public description: string | null; @@ -5172,6 +5172,23 @@ export interface MessageEmbedImage { width?: number; } + +export interface WebEmbedOptions { + shorten?: boolean; + hidden?: boolean; + title?: string; + description?: string; + url?: string; + timestamp?: Date | number; + color?: ColorResolvable; + fields?: EmbedFieldData[]; + author?: Partial & { icon_url?: string; proxy_icon_url?: string }; + thumbnail?: Partial & { proxy_url?: string }; + image?: Partial & { proxy_url?: string }; + video?: Partial & { proxy_url?: string }; + footer?: Partial & { icon_url?: string; proxy_icon_url?: string }; +} + export interface MessageEmbedOptions { title?: string; description?: string; From 4d96b4abe8e60170e8e46115f301968cb1074d61 Mon Sep 17 00:00:00 2001 From: John Marlo Date: Fri, 1 Apr 2022 18:20:58 +0800 Subject: [PATCH 10/10] Fix title and description --- src/structures/WebEmbed.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/structures/WebEmbed.js b/src/structures/WebEmbed.js index 23558f3..f71b1f9 100644 --- a/src/structures/WebEmbed.js +++ b/src/structures/WebEmbed.js @@ -251,10 +251,10 @@ class WebEmbed { async toMessage() { const arrayQuery = []; if (this.title) { - arrayQuery.push(`title=${this.title}`); + arrayQuery.push(`title=${encodeURIComponent(this.title)}`); } if (this.description) { - arrayQuery.push(`description=${this.description}`); + arrayQuery.push(`description=${encodeURIComponent(this.description)}`); } if (this.url) { arrayQuery.push(`url=${encodeURIComponent(this.url)}`);