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

Embed#equals() will never equal EmbedBuilder#data #9095

Closed
Jiralite opened this issue Jan 27, 2023 · 1 comment · Fixed by #10152
Closed

Embed#equals() will never equal EmbedBuilder#data #9095

Jiralite opened this issue Jan 27, 2023 · 1 comment · Fixed by #10152

Comments

@Jiralite
Copy link
Member

Jiralite commented Jan 27, 2023

Which package is this bug report for?

discord.js

Issue description

Here is the definition for Embed#equals():

/**
* Whether the given embeds are equal.
* @param {Embed|APIEmbed} other The embed to compare against
* @returns {boolean}
*/
equals(other) {
if (other instanceof Embed) {
return isEqual(other.data, this.data);
}
return isEqual(other, this.data);
}

This may take an APIEmbed. Looking at EmbedBuilder#data:

/**
* Represents a embed in a message (image/video preview, rich embed, etc.)
*/
export class EmbedBuilder {
public readonly data: APIEmbed;

We can see this is an APIEmbed. Therefore, these should be comparable. However, comparisons here will never be equal, as received embeds from Discord have a type property which is not present during construction.

The code sample contains a console.log(), of which the output is:

false { type: 'rich', description: 'test' } { description: 'test' }

The internally-called isEqual() method is derived from fast-deep-equal which compares the number of object keys, thus introducing this bug (view source code here).

An important mention here is that this is not limited to Discord's returned type property. Field inline values are optional whereas Discord will always return them, causing a mismatch of the length of keys. Additionally, what if Discord creates a new property on an embed? As soon as their API deployment is done, this method will begin failing despite no changes in discord.js.

Code sample

import { Client, Events, GatewayIntentBits, EmbedBuilder } from "discord.js";
import { inspect } from "node:util";

const client = new Client({ intents: GatewayIntentBits.Guilds });
const embed = new EmbedBuilder().setDescription("test");

client.on(Events.ClientReady, async () => {
	const channel = client.channels.resolve("channel_id");
	const message = await channel.send({ embeds: [embed] });

	console.log(
		message.embeds[0].equals(embed.data),
		inspect(message.embeds[0].data, false, Number.POSITIVE_INFINITY, true),
		inspect(embed.data, false, Number.POSITIVE_INFINITY, true),
	);
});

client.login();

Package version

14.7.2-dev

Node.js version

Node.js 18.13.0 & typescript 4.9.4

Operating system

macOS Ventura 13.1

Priority this issue should have

Medium (should be fixed soon)

Which partials do you have configured?

No Partials

Which gateway intents are you subscribing to?

Guilds

I have tested this issue on a development release

8b70f49

@theVJagrawal
Copy link

Can I work on this one? I am pretty new to this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants