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 <spaceeec@yahoo.com>
This commit is contained in:
Matt (IPv4) Cowley
2020-03-08 16:24:18 +00:00
committed by GitHub
parent 3c653aafe8
commit 3e169cb4d3
2 changed files with 9 additions and 6 deletions

View File

@@ -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) {

View File

@@ -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