Skip to content

Commit 3b5c600

Browse files
almeidxJiralitekodiakhq[bot]
authored
feat(User): add avatarDecorationData (discordjs#9888)
* feat(User): add `avatarDecorationData` * fix: remove options * fix(User): check avatar decoration in equals() methods * docs: Add full reference --------- Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent 311aaf2 commit 3b5c600

File tree

4 files changed

+69
-3
lines changed

4 files changed

+69
-3
lines changed

packages/discord.js/src/structures/User.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,31 @@ class User extends Base {
127127
/**
128128
* The user avatar decoration's hash
129129
* @type {?string}
130+
* @deprecated Use `avatarDecorationData` instead
130131
*/
131132
this.avatarDecoration = data.avatar_decoration;
132133
} else {
133134
this.avatarDecoration ??= null;
134135
}
136+
137+
/**
138+
* @typedef {Object} AvatarDecorationData
139+
* @property {string} asset The avatar decoration hash
140+
* @property {Snowflake} skuId The id of the avatar decoration's SKU
141+
*/
142+
143+
if (data.avatar_decoration_data) {
144+
/**
145+
* The user avatar decoration's data
146+
* @type {?AvatarDecorationData}
147+
*/
148+
this.avatarDecorationData = {
149+
asset: data.avatar_decoration_data.asset,
150+
skuId: data.avatar_decoration_data.sku_id,
151+
};
152+
} else {
153+
this.avatarDecorationData = null;
154+
}
135155
}
136156

137157
/**
@@ -176,6 +196,10 @@ class User extends Base {
176196
* @returns {?string}
177197
*/
178198
avatarDecorationURL(options = {}) {
199+
if (this.avatarDecorationData) {
200+
return this.client.rest.cdn.avatarDecoration(this.avatarDecorationData.asset);
201+
}
202+
179203
return this.avatarDecoration && this.client.rest.cdn.avatarDecoration(this.id, this.avatarDecoration, options);
180204
}
181205

@@ -286,7 +310,10 @@ class User extends Base {
286310
this.avatar === user.avatar &&
287311
this.flags?.bitfield === user.flags?.bitfield &&
288312
this.banner === user.banner &&
289-
this.accentColor === user.accentColor
313+
this.accentColor === user.accentColor &&
314+
this.avatarDecoration === user.avatarDecoration &&
315+
this.avatarDecorationData?.asset === user.avatarDecorationData?.asset &&
316+
this.avatarDecorationData?.skuId === user.avatarDecorationData?.skuId
290317
);
291318
}
292319

@@ -306,7 +333,12 @@ class User extends Base {
306333
this.avatar === user.avatar &&
307334
this.flags?.bitfield === user.public_flags &&
308335
('banner' in user ? this.banner === user.banner : true) &&
309-
('accent_color' in user ? this.accentColor === user.accent_color : true)
336+
('accent_color' in user ? this.accentColor === user.accent_color : true) &&
337+
('avatar_decoration' in user ? this.avatarDecoration === user.avatar_decoration : true) &&
338+
('avatar_decoration_data' in user
339+
? this.avatarDecorationData?.asset === user.avatar_decoration_data?.asset &&
340+
this.avatarDecorationData?.skuId === user.avatar_decoration_data?.sku_id
341+
: true)
310342
);
311343
}
312344

packages/discord.js/typings/index.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3311,6 +3311,11 @@ export class Typing extends Base {
33113311
};
33123312
}
33133313

3314+
export interface AvatarDecorationData {
3315+
asset: string;
3316+
skuId: Snowflake;
3317+
}
3318+
33143319
// tslint:disable-next-line no-empty-interface
33153320
export interface User extends PartialTextBasedChannelFields<false> {}
33163321
export class User extends Base {
@@ -3319,7 +3324,9 @@ export class User extends Base {
33193324

33203325
public accentColor: number | null | undefined;
33213326
public avatar: string | null;
3327+
/** @deprecated Use {@link User.avatarDecorationData} instead */
33223328
public avatarDecoration: string | null;
3329+
public avatarDecorationData: AvatarDecorationData | null;
33233330
public banner: string | null | undefined;
33243331
public bot: boolean;
33253332
public get createdAt(): Date;

packages/rest/__tests__/CDN.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ test('avatar dynamic-not-animated', () => {
2929
expect(cdn.avatar(id, hash)).toEqual(`${base}/avatars/${id}/${hash}.webp`);
3030
});
3131

32+
test('avatar decoration default', () => {
33+
expect(cdn.avatarDecoration(id, hash)).toEqual(`${base}/avatar-decorations/${id}/${hash}.webp`);
34+
});
35+
36+
test('avatar decoration preset', () => {
37+
expect(cdn.avatarDecoration(hash)).toEqual(`${base}/avatar-decoration-presets/${hash}.png`);
38+
});
39+
3240
test('banner default', () => {
3341
expect(cdn.banner(id, hash)).toEqual(`${base}/banners/${id}/${hash}.webp`);
3442
});

packages/rest/src/lib/CDN.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,19 +97,38 @@ export class CDN {
9797
return this.dynamicMakeURL(`/avatars/${id}/${avatarHash}`, avatarHash, options);
9898
}
9999

100+
/**
101+
* Generates a user avatar decoration preset URL.
102+
*
103+
* @param asset - The avatar decoration hash
104+
*/
105+
public avatarDecoration(asset: string): string;
106+
100107
/**
101108
* Generates a user avatar decoration URL.
102109
*
110+
* @deprecated This overload is deprecated. Pass a hash instead.
103111
* @param userId - The id of the user
104112
* @param userAvatarDecoration - The hash provided by Discord for this avatar decoration
105113
* @param options - Optional options for the avatar decoration
106114
*/
107115
public avatarDecoration(
108116
userId: string,
109117
userAvatarDecoration: string,
118+
// eslint-disable-next-line @typescript-eslint/unified-signatures
119+
options?: Readonly<BaseImageURLOptions>,
120+
): string;
121+
122+
public avatarDecoration(
123+
userIdOrAsset: string,
124+
userAvatarDecoration?: string,
110125
options?: Readonly<BaseImageURLOptions>,
111126
): string {
112-
return this.makeURL(`/avatar-decorations/${userId}/${userAvatarDecoration}`, options);
127+
if (userAvatarDecoration) {
128+
return this.makeURL(`/avatar-decorations/${userIdOrAsset}/${userAvatarDecoration}`, options);
129+
}
130+
131+
return this.makeURL(`/avatar-decoration-presets/${userIdOrAsset}`, { extension: 'png' });
113132
}
114133

115134
/**

0 commit comments

Comments
 (0)