Skip to content

Commit 3e169cb

Browse files
MattIPv4SpaceEEC
andauthored
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>
1 parent 3c653aa commit 3e169cb

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

src/structures/Message.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class Message extends Base {
9898
* A list of embeds in the message - e.g. YouTube Player
9999
* @type {MessageEmbed[]}
100100
*/
101-
this.embeds = (data.embeds || []).map(e => new Embed(e));
101+
this.embeds = (data.embeds || []).map(e => new Embed(e, true));
102102

103103
/**
104104
* A collection of attachments in the message - e.g. Pictures - mapped by their ID
@@ -225,7 +225,7 @@ class Message extends Base {
225225
if ('content' in data) this.content = data.content;
226226
if ('pinned' in data) this.pinned = data.pinned;
227227
if ('tts' in data) this.tts = data.tts;
228-
if ('embeds' in data) this.embeds = data.embeds.map(e => new Embed(e));
228+
if ('embeds' in data) this.embeds = data.embeds.map(e => new Embed(e, true));
229229
else this.embeds = this.embeds.slice();
230230

231231
if ('attachments' in data) {

src/structures/MessageEmbed.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ const Util = require('../util/Util');
77
* Represents an embed in a message (image/video preview, rich embed, etc.)
88
*/
99
class MessageEmbed {
10-
constructor(data = {}) {
11-
this.setup(data);
10+
constructor(data = {}, skipValidation = false) {
11+
this.setup(data, skipValidation);
1212
}
1313

14-
setup(data) {
14+
setup(data, skipValidation) {
1515
/**
1616
* The type of this embed, either:
1717
* * `rich` - a rich embed
@@ -65,7 +65,10 @@ class MessageEmbed {
6565
* The fields of this embed
6666
* @type {EmbedField[]}
6767
*/
68-
this.fields = data.fields ? this.constructor.normalizeFields(data.fields) : [];
68+
this.fields = [];
69+
if (data.fields) {
70+
this.fields = skipValidation ? data.fields.map(Util.cloneObject) : this.constructor.normalizeFields(data.fields);
71+
}
6972

7073
/**
7174
* @typedef {Object} MessageEmbedThumbnail

0 commit comments

Comments
 (0)