From 3e169cb4d3e0bb53c1990dd6be4ce9ba67b4badb Mon Sep 17 00:00:00 2001 From: "Matt (IPv4) Cowley" Date: Sun, 8 Mar 2020 16:24:18 +0000 Subject: [PATCH] fix(MessageEmbed): skip validation of fields when inside a message (#3894) * fix(MessageEmbed): Add skipValidation flag to MessageEmbed * fix(MessageEmbed): Use skipValidation flag in Message * fix(MessageEmbed): Restore static normalizeField(s) methods * fix(MessageEmbed): Update typings for constructor * fix(MessageEmbed): Remove private docstrings/typings * fix(MessageEmbed): Use skipValidation without storing in instance * fix(MessageEmbed): skipValidation without modifying normalizeFields * fix(MessageEmbed): Revert indentation change in typings * fix(MessageEmbed): Clone logic from normalizeFields (duplicated code ftw) * revert(MessageEmbed): remove dead code / breaking change - dead code discord.js does not use those methods interally and won't in the future, as Discord does not emit any partial embed updates and doing so in the future seems unlikely. - a breaking change (an incompatible api change) Although it's not recommended to do, users can modify received embeds without cloning them, e.g.: const embed = message.embeds[0].addField('some title', ''); (replace '' with some function call; this is just an example) This would no longer throw a synchronous error (breaking change), but at a later point when actually sending it. (poorer to debug) Co-authored-by: SpaceEEC --- src/structures/Message.js | 4 ++-- src/structures/MessageEmbed.js | 11 +++++++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/structures/Message.js b/src/structures/Message.js index 630d21e7e..d76da0596 100644 --- a/src/structures/Message.js +++ b/src/structures/Message.js @@ -98,7 +98,7 @@ class Message extends Base { * A list of embeds in the message - e.g. YouTube Player * @type {MessageEmbed[]} */ - this.embeds = (data.embeds || []).map(e => new Embed(e)); + this.embeds = (data.embeds || []).map(e => new Embed(e, true)); /** * A collection of attachments in the message - e.g. Pictures - mapped by their ID @@ -225,7 +225,7 @@ class Message extends Base { if ('content' in data) this.content = data.content; if ('pinned' in data) this.pinned = data.pinned; if ('tts' in data) this.tts = data.tts; - if ('embeds' in data) this.embeds = data.embeds.map(e => new Embed(e)); + if ('embeds' in data) this.embeds = data.embeds.map(e => new Embed(e, true)); else this.embeds = this.embeds.slice(); if ('attachments' in data) { diff --git a/src/structures/MessageEmbed.js b/src/structures/MessageEmbed.js index eb67b952c..437fd9f76 100644 --- a/src/structures/MessageEmbed.js +++ b/src/structures/MessageEmbed.js @@ -7,11 +7,11 @@ const Util = require('../util/Util'); * Represents an embed in a message (image/video preview, rich embed, etc.) */ class MessageEmbed { - constructor(data = {}) { - this.setup(data); + constructor(data = {}, skipValidation = false) { + this.setup(data, skipValidation); } - setup(data) { + setup(data, skipValidation) { /** * The type of this embed, either: * * `rich` - a rich embed @@ -65,7 +65,10 @@ class MessageEmbed { * The fields of this embed * @type {EmbedField[]} */ - this.fields = data.fields ? this.constructor.normalizeFields(data.fields) : []; + this.fields = []; + if (data.fields) { + this.fields = skipValidation ? data.fields.map(Util.cloneObject) : this.constructor.normalizeFields(data.fields); + } /** * @typedef {Object} MessageEmbedThumbnail