From 7e9d77144961a2a2acbbe498cd64cbe6bd0f16d6 Mon Sep 17 00:00:00 2001 From: Rodry <38259440+ImRodry@users.noreply.github.com> Date: Mon, 25 Oct 2021 17:06:46 +0100 Subject: [PATCH 1/2] feat(MessageEmbed): add #equals --- src/structures/MessageEmbed.js | 49 ++++++++++++++++++++++++++++++++++ typings/index.d.ts | 3 +++ 2 files changed, 52 insertions(+) diff --git a/src/structures/MessageEmbed.js b/src/structures/MessageEmbed.js index f2fc9785ffdb..869c8c9d8aff 100644 --- a/src/structures/MessageEmbed.js +++ b/src/structures/MessageEmbed.js @@ -263,6 +263,55 @@ class MessageEmbed { ); } + /** + * Checks if this embed is equal to another one by comparing every single one of their properties. + * @param {MessageEmbed|APIEmbed} embed The embed to compare with + * @returns {boolean} + */ + equals(embed) { + return ( + this.type === embed.type && + this.author?.name === embed.author?.name && + this.author?.url === embed.author?.url && + this.author?.iconURL === (embed.author?.iconURL ?? embed.author?.icon_url) && + this.author?.proxyIconURL === (embed.author?.proxyIconURL ?? embed.author?.proxy_icon_url) && + this.color === embed.color && + this.title === embed.title && + this.description === embed.description && + this.url === embed.url && + this.timestamp === embed.timestamp && + this.fields.every((field, i) => this._fieldEquals(field, embed.fields[i])) && + this.footer?.text === embed.footer?.text && + this.footer?.iconURL === (embed.footer?.iconURL ?? embed.footer?.icon_url) && + this.footer?.proxyIconURL === (embed.footer?.proxyIconURL ?? embed.footer?.proxy_icon_url) && + this.image?.url === embed.image?.url && + this.image?.proxyURL === (embed.image?.proxyURL ?? embed.image?.proxy_url) && + this.image?.height === embed.image?.height && + this.image?.width === embed.image?.width && + this.thumbnail?.url === embed.thumbnail?.url && + this.thumbnail?.proxyURL === (embed.thumbnail?.proxyURL ?? embed.thumbnail?.proxy_url) && + this.thumbnail?.height === embed.thumbnail?.height && + this.thumbnail?.width === embed.thumbnail?.width && + this.video?.url === embed.video?.url && + this.video?.proxyURL === (embed.video?.proxyURL ?? embed.video?.proxy_url) && + this.video?.height === embed.video?.height && + this.video?.width === embed.video?.width && + this.provider?.name === embed.provider?.name && + this.provider?.url === embed.provider?.url + ); + } + + /** + * Compares two given embed fields to see if they are equal + * @param {EmbedFieldData} field The first field to compare + * @param {EmbedFieldData} other The second field to compare + * @returns {boolean} + * @private + */ + _fieldEquals(field, other) { + return field.name === other?.name && field.value === other?.value && field.inline === other?.inline; + } + /** * Adds a field to the embed (max 25). * @param {string} name The name of this field diff --git a/typings/index.d.ts b/typings/index.d.ts index d5199212f0ca..0ba9222ae31f 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -1477,6 +1477,8 @@ export class MessageComponentInteraction extends Interaction { } export class MessageEmbed { + private _fieldEquals(field: EmbedField, other: EmbedField): boolean; + public constructor(data?: MessageEmbed | MessageEmbedOptions | APIEmbed); public author: MessageEmbedAuthor | null; public color: number | null; @@ -1508,6 +1510,7 @@ export class MessageEmbed { public setTitle(title: string): this; public setURL(url: string): this; public spliceFields(index: number, deleteCount: number, ...fields: EmbedFieldData[] | EmbedFieldData[][]): this; + public equals(embed: MessageEmbed | APIEmbed): boolean; public toJSON(): unknown; public static normalizeField(name: string, value: string, inline?: boolean): Required; From dfb1de0941a0d0d7d86d3796491b5fbefe309add Mon Sep 17 00:00:00 2001 From: Rodry <38259440+ImRodry@users.noreply.github.com> Date: Tue, 26 Oct 2021 19:13:26 +0100 Subject: [PATCH 2/2] fix(MessageEmbed): check for field length in equals first --- src/structures/MessageEmbed.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/structures/MessageEmbed.js b/src/structures/MessageEmbed.js index 869c8c9d8aff..12317d8b34c0 100644 --- a/src/structures/MessageEmbed.js +++ b/src/structures/MessageEmbed.js @@ -280,6 +280,7 @@ class MessageEmbed { this.description === embed.description && this.url === embed.url && this.timestamp === embed.timestamp && + this.fields.length === embed.fields.length && this.fields.every((field, i) => this._fieldEquals(field, embed.fields[i])) && this.footer?.text === embed.footer?.text && this.footer?.iconURL === (embed.footer?.iconURL ?? embed.footer?.icon_url) && @@ -309,7 +310,7 @@ class MessageEmbed { * @private */ _fieldEquals(field, other) { - return field.name === other?.name && field.value === other?.value && field.inline === other?.inline; + return field.name === other.name && field.value === other.value && field.inline === other.inline; } /**