Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(MessageEmbed): add #equals #6885

Merged
merged 2 commits into from Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/structures/MessageEmbed.js
Expand Up @@ -263,6 +263,56 @@ 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.length === embed.fields.length &&
this.fields.every((field, i) => this._fieldEquals(field, embed.fields[i])) &&
ImRodry marked this conversation as resolved.
Show resolved Hide resolved
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
ImRodry marked this conversation as resolved.
Show resolved Hide resolved
* @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
Expand Down
3 changes: 3 additions & 0 deletions typings/index.d.ts
Expand Up @@ -1477,6 +1477,8 @@ export class MessageComponentInteraction extends Interaction {
}

export class MessageEmbed {
private _fieldEquals(field: EmbedField, other: EmbedField): boolean;
ImRodry marked this conversation as resolved.
Show resolved Hide resolved

public constructor(data?: MessageEmbed | MessageEmbedOptions | APIEmbed);
public author: MessageEmbedAuthor | null;
public color: number | null;
Expand Down Expand Up @@ -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<EmbedFieldData>;
Expand Down