Skip to content

Commit

Permalink
expose image hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
Starman3787 committed Oct 28, 2024
1 parent 045ae78 commit e970433
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 31 deletions.
10 changes: 5 additions & 5 deletions src/structures/Guild.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
Expand Down Expand Up @@ -585,7 +585,7 @@ class Guild {
* @public
*/
get displayIconURL() {
return Guild.getIcon(this.id, this.#_originalIconHash);
return Guild.getIcon(this.id, this._originalIconHash);
}

/**
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
16 changes: 6 additions & 10 deletions src/structures/Member.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
Expand Down Expand Up @@ -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
);
}
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/structures/Role.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
Expand Down Expand Up @@ -193,7 +193,7 @@ class Role {
* @public
*/
get displayIconURL() {
return Role.getIconUrl(this.id, this.#_originalIconHash);
return Role.getIconUrl(this.id, this._originalIconHash);
}

/**
Expand Down Expand Up @@ -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,
};
Expand All @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions src/structures/ScheduledEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
Expand Down Expand Up @@ -284,7 +284,7 @@ class ScheduledEvent {
* @public
*/
get displayImageURL() {
return ScheduledEvent.getImageUrl(this.id, this.#_originalImageHash);
return ScheduledEvent.getImageUrl(this.id, this._originalImageHash);
}

/**
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions src/structures/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
10 changes: 8 additions & 2 deletions test/structures/Guild.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ import {
TEST_ROLES,
} from "../../src/testData.js";
import {
Member,
Guild,
GuildEmojisManager,
GuildInviteManager,
GuildVoiceStatesManager,
GuildMemberManager,
GuildChannelsManager,
GuildCacheOptions,
Role,
} from "../../src/structures.js";
import { TO_JSON_TYPES_ENUM } from "../../src/constants.js";

Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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();
Expand Down
22 changes: 22 additions & 0 deletions test/structures/Member.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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,
);
});
});
});
26 changes: 26 additions & 0 deletions test/structures/Role.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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);
});
});
});
Loading

0 comments on commit e970433

Please sign in to comment.