From e970433fbcd3752f5554c9bcbf6f476a4ab8c8fe Mon Sep 17 00:00:00 2001 From: Starman <30315137+Starman3787@users.noreply.github.com> Date: Mon, 28 Oct 2024 19:22:15 +0000 Subject: [PATCH] expose image hashes --- src/structures/Guild.js | 10 +++++----- src/structures/Member.js | 16 ++++++---------- src/structures/Role.js | 10 +++++----- src/structures/ScheduledEvent.js | 8 ++++---- src/structures/User.js | 10 +++++----- test/structures/Guild.js | 10 ++++++++-- test/structures/Member.js | 22 ++++++++++++++++++++++ test/structures/Role.js | 26 ++++++++++++++++++++++++++ test/structures/ScheduledEvent.js | 25 +++++++++++++++++++++++++ test/structures/User.js | 17 +++++++++++++++++ 10 files changed, 123 insertions(+), 31 deletions(-) diff --git a/src/structures/Guild.js b/src/structures/Guild.js index 2d142262..d7153b16 100644 --- a/src/structures/Guild.js +++ b/src/structures/Guild.js @@ -551,9 +551,9 @@ class Guild { * The hash of the guild's icon, as it was received from Discord. * @readonly * @type {String?} - * @private + * @public */ - get #_originalIconHash() { + get _originalIconHash() { return this.#_icon ? // eslint-disable-next-line quotes `${this.#_formattedIconHash}` @@ -585,7 +585,7 @@ class Guild { * @public */ get displayIconURL() { - return Guild.getIcon(this.id, this.#_originalIconHash); + return Guild.getIcon(this.id, this._originalIconHash); } /** @@ -1601,7 +1601,7 @@ class Guild { return { id: this.id, name: this.name, - icon: this.#_originalIconHash, + icon: this._originalIconHash, owner_id: this.ownerId, joined_at: this.joinedAt * 1000, unavailable: this.unavailable, @@ -1625,7 +1625,7 @@ class Guild { return { id: this.id, name: this.name, - icon: this.#_originalIconHash, + icon: this._originalIconHash, owner_id: this.ownerId, joined_at: new Date(this.joinedAt * 1000).toISOString(), premium_tier: this.premiumTier, diff --git a/src/structures/Member.js b/src/structures/Member.js index 9a39cd44..733b6798 100644 --- a/src/structures/Member.js +++ b/src/structures/Member.js @@ -345,9 +345,9 @@ class Member { * The hash of the member's avatar, as it was received from Discord. * @readonly * @type {String?} - * @private + * @public */ - get #_originalAvatarHash() { + get _originalAvatarHash() { return this.#_avatar ? // eslint-disable-next-line quotes `${this.avatarIsAnimated ? "a_" : ""}${this.#_formattedAvatarHash}` @@ -380,7 +380,7 @@ class Member { */ get displayAvatarURL() { return ( - Member.getAvatarUrl(this.id, this.guildId, this.#_originalAvatarHash) ?? + Member.getAvatarUrl(this.id, this.guildId, this._originalAvatarHash) ?? this.user.displayAvatarURL ); } @@ -392,11 +392,7 @@ class Member { * @public */ get displayAvatarURLNoFallback() { - return Member.getAvatarUrl( - this.id, - this.guildId, - this.#_originalAvatarHash, - ); + return Member.getAvatarUrl(this.id, this.guildId, this._originalAvatarHash); } /** @@ -813,7 +809,7 @@ class Member { user: this.user.toJSON(format), nick: this.nick, joined_at: this.joinedAt ? this.joinedAt * 1000 : undefined, - avatar: this.#_originalAvatarHash, + avatar: this._originalAvatarHash, permissions: String(this.permissions), roles: Array.isArray(this.#_roles) ? this.#_roles @@ -835,7 +831,7 @@ class Member { joined_at: this.joinedAt ? new Date(this.joinedAt * 1000).toISOString() : undefined, - avatar: this.#_originalAvatarHash, + avatar: this._originalAvatarHash, permissions: String(this.permissions), roles: Array.isArray(this.#_roles) ? this.#_roles diff --git a/src/structures/Role.js b/src/structures/Role.js index 0e03b897..7b5294db 100644 --- a/src/structures/Role.js +++ b/src/structures/Role.js @@ -159,9 +159,9 @@ class Role { * The hash of the role's avatar, as it was received from Discord. * @readonly * @type {String?} - * @private + * @public */ - get #_originalIconHash() { + get _originalIconHash() { return this.#_icon ? // eslint-disable-next-line quotes `${this.#_formattedIconHash}` @@ -193,7 +193,7 @@ class Role { * @public */ get displayIconURL() { - return Role.getIconUrl(this.id, this.#_originalIconHash); + return Role.getIconUrl(this.id, this._originalIconHash); } /** @@ -367,7 +367,7 @@ class Role { color: this.color, position: this.position, permissions: this.permissions, - icon: this.#_originalIconHash, + icon: this._originalIconHash, _attributes: this.#_attributes, tags: this.tags, }; @@ -380,7 +380,7 @@ class Role { color: this.color, position: this.position, permissions: this.permissions, - icon: this.#_originalIconHash, + icon: this._originalIconHash, tags: this.tags, hoist: this.hoist, managed: this.managed, diff --git a/src/structures/ScheduledEvent.js b/src/structures/ScheduledEvent.js index c9c25d76..62559d3c 100644 --- a/src/structures/ScheduledEvent.js +++ b/src/structures/ScheduledEvent.js @@ -252,9 +252,9 @@ class ScheduledEvent { * The hash of the event's image, as it was received from Discord. * @readonly * @type {String?} - * @private + * @public */ - get #_originalImageHash() { + get _originalImageHash() { return this.#_image ? // eslint-disable-next-line quotes `${this.#_formattedImageHash}` @@ -284,7 +284,7 @@ class ScheduledEvent { * @public */ get displayImageURL() { - return ScheduledEvent.getImageUrl(this.id, this.#_originalImageHash); + return ScheduledEvent.getImageUrl(this.id, this._originalImageHash); } /** @@ -463,7 +463,7 @@ class ScheduledEvent { scheduled_end_time: this.scheduledEndTime ? this.scheduledEndTime * 1000 : undefined, - image: this.#_originalImageHash, + image: this._originalImageHash, user_count: this.userCount, entity_type: this.#rawEntityType, status: this.#rawStatus, diff --git a/src/structures/User.js b/src/structures/User.js index 0a2e11de..0f63a655 100644 --- a/src/structures/User.js +++ b/src/structures/User.js @@ -190,9 +190,9 @@ class User { * The hash of the users's avatar, as it was received from Discord. * @readonly * @type {String?} - * @private + * @public */ - get #_originalAvatarHash() { + get _originalAvatarHash() { return this.#_avatar ? // eslint-disable-next-line quotes `${this.avatarIsAnimated ? "a_" : ""}${this.#_formattedAvatarHash}` @@ -226,7 +226,7 @@ class User { get displayAvatarURL() { if (this.#overrideAvatar) return this.#overrideAvatar; - return User.getAvatarUrl(this.id, this.#_originalAvatarHash); + return User.getAvatarUrl(this.id, this._originalAvatarHash); } /** @@ -363,7 +363,7 @@ class User { case TO_JSON_TYPES_ENUM.CACHE_FORMAT: { return { id: this.id, - avatar: this.#_originalAvatarHash, + avatar: this._originalAvatarHash, _cached: this._cached, bot: this.bot, username: this.username, @@ -376,7 +376,7 @@ class User { default: { return { id: this.id, - avatar: this.#_originalAvatarHash, + avatar: this._originalAvatarHash, bot: this.bot, username: this.username, global_name: this.globalName, diff --git a/test/structures/Guild.js b/test/structures/Guild.js index b9de946d..38c6cccf 100644 --- a/test/structures/Guild.js +++ b/test/structures/Guild.js @@ -9,7 +9,6 @@ import { TEST_ROLES, } from "../../src/testData.js"; import { - Member, Guild, GuildEmojisManager, GuildInviteManager, @@ -17,7 +16,6 @@ import { GuildMemberManager, GuildChannelsManager, GuildCacheOptions, - Role, } from "../../src/structures.js"; import { TO_JSON_TYPES_ENUM } from "../../src/constants.js"; @@ -62,6 +60,7 @@ describe("Guild", function () { expect(guild).to.have.property("nsfwLevel"); expect(guild).to.have.property("emojis"); expect(guild).to.have.property("invites"); + expect(guild).to.have.property("_originalIconHash"); expect(guild).to.have.property("_cacheOptions"); expect(guild).to.have.property("toString"); expect(guild).to.have.property("toJSON"); @@ -274,6 +273,13 @@ describe("Guild", function () { expect(guild.channels).to.be.an.instanceOf(GuildChannelsManager); }); }); + context("check _originalIconHash", function () { + it("should have the correct _originalIconHash", function () { + const client = TEST_CLIENTS.ALL_CACHES_ENABLED(); + const guild = TEST_GUILDS.ALL_CACHES_ENABLED(client); + expect(guild._originalIconHash).to.equal(TEST_DATA.GUILD.icon); + }); + }); context("check _cacheOptions", function () { it("should have the correct _cacheOptions", function () { const client = TEST_CLIENTS.ALL_CACHES_ENABLED(); diff --git a/test/structures/Member.js b/test/structures/Member.js index 8496f93e..17f1796d 100644 --- a/test/structures/Member.js +++ b/test/structures/Member.js @@ -33,6 +33,7 @@ describe("Member", function () { expect(member).to.have.property("rejoined"); expect(member).to.have.property("displayAvatarURL"); expect(member).to.have.property("displayAvatarURLNoFallback"); + expect(member).to.have.property("_originalAvatarHash"); expect(member).to.have.property("permissions"); expect(member).to.have.property("avatarIsAnimated"); expect(member).to.have.property("mention"); @@ -649,6 +650,15 @@ describe("Member", function () { }); }); + context("check _originalAvatarHash", function () { + it("should have the correct _originalAvatarHash", function () { + const client = TEST_CLIENTS.ALL_CACHES_ENABLED(); + TEST_GUILDS.ALL_CACHES_ENABLED(client); + const member = TEST_MEMBERS.GENERIC_MEMBER(client); + expect(member._originalAvatarHash).to.be.null; + }); + }); + context("check permissions", function () { it("should have the correct permissions", function () { const client = TEST_CLIENTS.ALL_CACHES_ENABLED(); @@ -847,6 +857,9 @@ describe("Member", function () { expect(rebundled.pending).to.equal(member.pending); expect(rebundled.avatarIsAnimated).to.equal(member.avatarIsAnimated); expect(rebundled.mention).to.equal(member.mention); + expect(rebundled._originalAvatarHash).to.equal( + member._originalAvatarHash, + ); }); it("should rebundle correctly with custom toJSON", function () { const client = TEST_CLIENTS.ALL_CACHES_ENABLED(); @@ -881,6 +894,9 @@ describe("Member", function () { expect(rebundled.pending).to.equal(member.pending); expect(rebundled.avatarIsAnimated).to.equal(member.avatarIsAnimated); expect(rebundled.mention).to.equal(member.mention); + expect(rebundled._originalAvatarHash).to.equal( + member._originalAvatarHash, + ); }); it("should rebundle correctly with custom toJSON", function () { const client = TEST_CLIENTS.ALL_CACHES_ENABLED(); @@ -915,6 +931,9 @@ describe("Member", function () { expect(rebundled.pending).to.equal(member.pending); expect(rebundled.avatarIsAnimated).to.equal(member.avatarIsAnimated); expect(rebundled.mention).to.equal(member.mention); + expect(rebundled._originalAvatarHash).to.equal( + member._originalAvatarHash, + ); }); it("should rebundle correctly with custom toJSON", function () { const client = TEST_CLIENTS.ALL_CACHES_ENABLED(); @@ -949,6 +968,9 @@ describe("Member", function () { expect(rebundled.pending).to.equal(member.pending); expect(rebundled.avatarIsAnimated).to.equal(member.avatarIsAnimated); expect(rebundled.mention).to.equal(member.mention); + expect(rebundled._originalAvatarHash).to.equal( + member._originalAvatarHash, + ); }); }); }); diff --git a/test/structures/Role.js b/test/structures/Role.js index 1d6b4deb..56035972 100644 --- a/test/structures/Role.js +++ b/test/structures/Role.js @@ -33,6 +33,7 @@ describe("Role", function () { expect(role).to.have.property("position"); expect(role).to.have.property("permissions"); expect(role).to.have.property("tags"); + expect(role).to.have.property("_originalIconHash"); expect(role).to.have.property("toString"); expect(role).to.have.property("toJSON"); }); @@ -152,6 +153,27 @@ describe("Role", function () { }); }); + context("check _originalIconHash", function () { + it("should have the correct _originalIconHash", function () { + const client = TEST_CLIENTS.ALL_CACHES_ENABLED(); + TEST_GUILDS.ALL_CACHES_ENABLED(client); + const role = new Role(client, { + id: TEST_DATA.ROLE_ID, + name: "role", + color: 123456, + position: 1, + permissions: 8, + tags: { + bot_id: "123456789012345678", + integration_id: null, + premium_subscriber: null, + }, + icon: "hash", + }); + expect(role._originalIconHash).to.equal("hash"); + }); + }); + context("check getIconUrl", function () { it("should throw an error if no role id is provided", function () { expect(() => Role.getIconUrl(undefined, "hash")).to.throw( @@ -298,6 +320,7 @@ describe("Role", function () { expect(rebundled.displayIconURL).to.equal(role.displayIconURL); expect(rebundled.guild.id).to.equal(role.guild.id); expect(rebundled.guildId).to.equal(role.guildId); + expect(rebundled._originalIconHash).to.equal(role._originalIconHash); }); it("should return the correct bundle with a custom toJSON", function () { const client = TEST_CLIENTS.ALL_CACHES_ENABLED(); @@ -323,6 +346,7 @@ describe("Role", function () { expect(rebundled.displayIconURL).to.equal(role.displayIconURL); expect(rebundled.guild.id).to.equal(role.guild.id); expect(rebundled.guildId).to.equal(role.guildId); + expect(rebundled._originalIconHash).to.equal(role._originalIconHash); }); it("should return the correct bundle with a custom toJSON", function () { const client = TEST_CLIENTS.ALL_CACHES_ENABLED(); @@ -348,6 +372,7 @@ describe("Role", function () { expect(rebundled.displayIconURL).to.equal(role.displayIconURL); expect(rebundled.guild.id).to.equal(role.guild.id); expect(rebundled.guildId).to.equal(role.guildId); + expect(rebundled._originalIconHash).to.equal(role._originalIconHash); }); it("should return the correct bundle with a custom toJSON", function () { const client = TEST_CLIENTS.ALL_CACHES_ENABLED(); @@ -373,6 +398,7 @@ describe("Role", function () { expect(rebundled.displayIconURL).to.equal(role.displayIconURL); expect(rebundled.guild.id).to.equal(role.guild.id); expect(rebundled.guildId).to.equal(role.guildId); + expect(rebundled._originalIconHash).to.equal(role._originalIconHash); }); }); }); diff --git a/test/structures/ScheduledEvent.js b/test/structures/ScheduledEvent.js index bd4a004b..a512730f 100644 --- a/test/structures/ScheduledEvent.js +++ b/test/structures/ScheduledEvent.js @@ -27,6 +27,7 @@ describe("ScheduledEvent", function () { expect(scheduledEvent).to.have.property("scheduledEndTime"); expect(scheduledEvent).to.have.property("userCount"); expect(scheduledEvent).to.have.property("location"); + expect(scheduledEvent).to.have.property("_originalImageHash"); expect(scheduledEvent).to.have.property("toString"); }); }); @@ -173,6 +174,18 @@ describe("ScheduledEvent", function () { }); }); + context("check _originalImageHash", function () { + it("should have the correct _originalImageHash", function () { + const client = TEST_CLIENTS.ALL_CACHES_ENABLED(); + TEST_GUILDS.ALL_CACHES_ENABLED(client); + const scheduledEvent = + TEST_SCHEDULED_EVENTS.GENERIC_SCHEDULED_EVENT(client); + expect(scheduledEvent._originalImageHash).to.equal( + TEST_DATA.SCHEDULED_EVENT.image, + ); + }); + }); + context("check userCount", function () { it("should have the correct userCount", function () { const client = TEST_CLIENTS.ALL_CACHES_ENABLED(); @@ -427,6 +440,9 @@ describe("ScheduledEvent", function () { expect(scheduledEvent.creatorId).to.equal(rebundled.creatorId); expect(scheduledEvent.name).to.equal(rebundled.name); expect(scheduledEvent.location).to.equal(rebundled.location); + expect(scheduledEvent._originalImageHash).to.equal( + rebundled._originalImageHash, + ); expect(rebundled.guild).to.be.an.instanceOf(Guild); expect(rebundled.guild.toJSON()).to.deep.equal( scheduledEvent.guild.toJSON(), @@ -462,6 +478,9 @@ describe("ScheduledEvent", function () { expect(scheduledEvent.creatorId).to.equal(rebundled.creatorId); expect(scheduledEvent.name).to.equal(rebundled.name); expect(scheduledEvent.location).to.equal(rebundled.location); + expect(scheduledEvent._originalImageHash).to.equal( + rebundled._originalImageHash, + ); expect(rebundled.guild).to.be.an.instanceOf(Guild); expect(rebundled.guild.toJSON()).to.deep.equal( scheduledEvent.guild.toJSON(), @@ -494,6 +513,9 @@ describe("ScheduledEvent", function () { expect(scheduledEvent.creatorId).to.equal(rebundled.creatorId); expect(scheduledEvent.name).to.equal(rebundled.name); expect(scheduledEvent.location).to.equal(rebundled.location); + expect(scheduledEvent._originalImageHash).to.equal( + rebundled._originalImageHash, + ); expect(rebundled.guild).to.be.an.instanceOf(Guild); expect(rebundled.guild.toJSON()).to.deep.equal( scheduledEvent.guild.toJSON(), @@ -525,6 +547,9 @@ describe("ScheduledEvent", function () { expect(scheduledEvent.creatorId).to.equal(rebundled.creatorId); expect(scheduledEvent.name).to.equal(rebundled.name); expect(scheduledEvent.location).to.equal(rebundled.location); + expect(scheduledEvent._originalImageHash).to.equal( + rebundled._originalImageHash, + ); expect(rebundled.guild).to.be.an.instanceOf(Guild); expect(rebundled.guild.toJSON()).to.deep.equal(scheduledEvent.guild.toJSON()); expect(scheduledEvent.toString()).to.equal(rebundled.toString()); diff --git a/test/structures/User.js b/test/structures/User.js index 1ae2f99b..6096118f 100644 --- a/test/structures/User.js +++ b/test/structures/User.js @@ -27,6 +27,7 @@ describe("User", function () { expect(user).to.have.property("createdTimestamp"); expect(user).to.have.property("bot"); expect(user).to.have.property("avatarIsAnimated"); + expect(user).to.have.property("_originalAvatarHash"); expect(user).to.have.property("toString"); expect(user).to.have.property("toJSON"); }); @@ -88,6 +89,18 @@ describe("User", function () { }); }); + context("check _originalAvatarHash", function () { + it("should have the correct _originalAvatarHash", function () { + const client = TEST_CLIENTS.ALL_CACHES_ENABLED(); + TEST_GUILDS.ALL_CACHES_ENABLED(client); + const user = new User(client, { + ...TEST_DATA.USER, + avatar: "deadbeef", + }); + expect(user._originalAvatarHash).to.equal("deadbeef"); + }); + }); + context("check tag", function () { it("should have the correct tag", function () { const client = TEST_CLIENTS.ALL_CACHES_ENABLED(); @@ -262,6 +275,7 @@ describe("User", function () { expect(rebundled.tag).to.equal(user.tag); expect(rebundled.createdTimestamp).to.equal(user.createdTimestamp); expect(rebundled.avatarIsAnimated).to.equal(user.avatarIsAnimated); + expect(rebundled._originalAvatarHash).to.equal(user._originalAvatarHash); expect(rebundled.toString()).to.equal(user.toString()); expect(rebundled.toJSON()).to.deep.equal(user.toJSON()); }); @@ -284,6 +298,7 @@ describe("User", function () { expect(rebundled.tag).to.equal(user.tag); expect(rebundled.createdTimestamp).to.equal(user.createdTimestamp); expect(rebundled.avatarIsAnimated).to.equal(user.avatarIsAnimated); + expect(rebundled._originalAvatarHash).to.equal(user._originalAvatarHash); expect(rebundled.toString()).to.equal(user.toString()); }); it("should bundle correctly with a custom toJSON", function () { @@ -305,6 +320,7 @@ describe("User", function () { expect(rebundled.tag).to.equal(user.tag); expect(rebundled.createdTimestamp).to.equal(user.createdTimestamp); expect(rebundled.avatarIsAnimated).to.equal(user.avatarIsAnimated); + expect(rebundled._originalAvatarHash).to.equal(user._originalAvatarHash); expect(rebundled.toString()).to.equal(user.toString()); }); it("should bundle correctly with a custom toJSON", function () { @@ -326,6 +342,7 @@ describe("User", function () { expect(rebundled.tag).to.equal(user.tag); expect(rebundled.createdTimestamp).to.equal(user.createdTimestamp); expect(rebundled.avatarIsAnimated).to.equal(user.avatarIsAnimated); + expect(rebundled._originalAvatarHash).to.equal(user._originalAvatarHash); expect(rebundled.toString()).to.equal(user.toString()); }); });