From 6d70da5b1e7b8f0e8ad9bd5120cff9e4391783f9 Mon Sep 17 00:00:00 2001 From: Lewdcario Date: Mon, 23 Jul 2018 22:41:07 -0600 Subject: [PATCH] backport: fix circular conversion in RichEmbed --- src/structures/RichEmbed.js | 34 +++++++++++++++++-- src/structures/Webhook.js | 2 ++ src/structures/interfaces/TextBasedChannel.js | 2 ++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/structures/RichEmbed.js b/src/structures/RichEmbed.js index 8842f976d..af68bde19 100644 --- a/src/structures/RichEmbed.js +++ b/src/structures/RichEmbed.js @@ -39,7 +39,7 @@ class RichEmbed { /** * Timestamp for this Embed - * @type {Date} + * @type {number} */ this.timestamp = data.timestamp; @@ -133,7 +133,7 @@ class RichEmbed { /** * Sets the timestamp of this embed. - * @param {Date} [timestamp=current date] The timestamp + * @param {Date} [timestamp=new Date()] The timestamp * @returns {RichEmbed} This embed */ setTimestamp(timestamp = new Date()) { @@ -215,6 +215,36 @@ class RichEmbed { this.file = file; return this; } + + /** + * Transforms the embed object to be processed. + * @returns {Object} The raw data of this embed + * @private + */ + _apiTransform() { + return { + title: this.title, + type: 'rich', + description: this.description, + url: this.url, + timestamp: this.timestamp ? new Date(this.timestamp) : null, + color: this.color, + fields: this.fields, + thumbnail: this.thumbnail, + image: this.image ? { + url: this.image.url, + } : null, + author: this.author ? { + name: this.author.name, + url: this.author.url, + icon_url: this.author.iconURL, + } : null, + footer: this.footer ? { + text: this.footer.text, + icon_url: this.footer.iconURL, + } : null, + }; + } } module.exports = RichEmbed; diff --git a/src/structures/Webhook.js b/src/structures/Webhook.js index f26b83522..5ce1e76d9 100644 --- a/src/structures/Webhook.js +++ b/src/structures/Webhook.js @@ -178,6 +178,8 @@ class Webhook { else options.files = files; } + if (options.embeds) options.embeds = options.embeds.map(e => new RichEmbed(e)._apiTransform()); + if (options.files) { for (let i = 0; i < options.files.length; i++) { let file = options.files[i]; diff --git a/src/structures/interfaces/TextBasedChannel.js b/src/structures/interfaces/TextBasedChannel.js index 3109d85d4..21f1c7bfa 100644 --- a/src/structures/interfaces/TextBasedChannel.js +++ b/src/structures/interfaces/TextBasedChannel.js @@ -135,6 +135,8 @@ class TextBasedChannel { else options.files = [options.file]; } + if (options.embed) options.embed = new RichEmbed(options.embed)._apiTransform(); + if (options.files) { for (let i = 0; i < options.files.length; i++) { let file = options.files[i];