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(User): add avatarDecorationData #9888

Merged
merged 6 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions packages/discord.js/src/structures/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,31 @@ class User extends Base {
/**
* The user avatar decoration's hash
* @type {?string}
* @deprecated Use `avatarDecorationData` instead
*/
this.avatarDecoration = data.avatar_decoration;
} else {
this.avatarDecoration ??= null;
}

/**
* @typedef {Object} AvatarDecorationData
* @property {string} asset The avatar decoration hash
* @property {Snowflake} skuId The id of the avatar decoration's SKU
*/

if (data.avatar_decoration_data) {
/**
* The user avatar decoration's data
* @type {?AvatarDecorationData}
*/
this.avatarDecorationData = {
asset: data.avatar_decoration_data.asset,
skuId: data.avatar_decoration_data.sku_id,
};
} else {
this.avatarDecorationData = null;
}
}

/**
Expand Down Expand Up @@ -176,6 +196,10 @@ class User extends Base {
* @returns {?string}
*/
avatarDecorationURL(options = {}) {
if (this.avatarDecorationData) {
return this.client.rest.cdn.avatarDecoration(this.avatarDecorationData.asset);
}

return this.avatarDecoration && this.client.rest.cdn.avatarDecoration(this.id, this.avatarDecoration, options);
}

Expand Down Expand Up @@ -286,7 +310,10 @@ class User extends Base {
this.avatar === user.avatar &&
this.flags?.bitfield === user.flags?.bitfield &&
this.banner === user.banner &&
this.accentColor === user.accentColor
this.accentColor === user.accentColor &&
this.avatarDecoration === user.avatarDecoration &&
this.avatarDecorationData?.asset === user.avatarDecorationData?.asset &&
this.avatarDecorationData?.skuId === user.avatarDecorationData?.skuId
);
}

Expand All @@ -306,7 +333,12 @@ class User extends Base {
this.avatar === user.avatar &&
this.flags?.bitfield === user.public_flags &&
('banner' in user ? this.banner === user.banner : true) &&
('accent_color' in user ? this.accentColor === user.accent_color : true)
('accent_color' in user ? this.accentColor === user.accent_color : true) &&
('avatar_decoration' in user ? this.avatarDecoration === user.avatar_decoration : true) &&
('avatar_decoration_data' in user
? this.avatarDecorationData?.asset === user.avatar_decoration_data?.asset &&
this.avatarDecorationData?.skuId === user.avatar_decoration_data?.sku_id
: true)
);
}

Expand Down
7 changes: 7 additions & 0 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3131,13 +3131,20 @@ export class Typing extends Base {
};
}

export interface AvatarDecorationData {
asset: string;
skuId: Snowflake;
}

export class User extends PartialTextBasedChannel(Base) {
protected constructor(client: Client<true>, data: RawUserData);
private _equals(user: APIUser): boolean;

public accentColor: number | null | undefined;
public avatar: string | null;
/** @deprecated Use {@link avatarDecorationData} instead */
Jiralite marked this conversation as resolved.
Show resolved Hide resolved
public avatarDecoration: string | null;
public avatarDecorationData: AvatarDecorationData | null;
public banner: string | null | undefined;
public bot: boolean;
public get createdAt(): Date;
Expand Down
8 changes: 8 additions & 0 deletions packages/rest/__tests__/CDN.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ test('avatar dynamic-not-animated', () => {
expect(cdn.avatar(id, hash)).toEqual(`${base}/avatars/${id}/${hash}.webp`);
});

test('avatar decoration default', () => {
expect(cdn.avatarDecoration(id, hash)).toEqual(`${base}/avatar-decorations/${id}/${hash}.webp`);
});

test('avatar decoration preset', () => {
expect(cdn.avatarDecoration(hash)).toEqual(`${base}/avatar-decoration-presets/${hash}.png`);
});

test('banner default', () => {
expect(cdn.banner(id, hash)).toEqual(`${base}/banners/${id}/${hash}.webp`);
});
Expand Down
21 changes: 20 additions & 1 deletion packages/rest/src/lib/CDN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,38 @@ export class CDN {
return this.dynamicMakeURL(`/avatars/${id}/${avatarHash}`, avatarHash, options);
}

/**
* Generates a user avatar decoration preset URL.
*
* @param asset - The avatar decoration hash
*/
public avatarDecoration(asset: string): string;

/**
* Generates a user avatar decoration URL.
*
* @deprecated This overload is deprecated. Pass a hash instead.
* @param userId - The id of the user
* @param userAvatarDecoration - The hash provided by Discord for this avatar decoration
* @param options - Optional options for the avatar decoration
*/
public avatarDecoration(
userId: string,
userAvatarDecoration: string,
// eslint-disable-next-line @typescript-eslint/unified-signatures
options?: Readonly<BaseImageURLOptions>,
): string;

public avatarDecoration(
userIdOrAsset: string,
userAvatarDecoration?: string,
options?: Readonly<BaseImageURLOptions>,
ckohen marked this conversation as resolved.
Show resolved Hide resolved
): string {
return this.makeURL(`/avatar-decorations/${userId}/${userAvatarDecoration}`, options);
if (userAvatarDecoration) {
return this.makeURL(`/avatar-decorations/${userIdOrAsset}/${userAvatarDecoration}`, options);
}

return this.makeURL(`/avatar-decoration-presets/${userIdOrAsset}`, { extension: 'png' });
}

/**
Expand Down