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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 25 additions & 1 deletion packages/discord.js/src/structures/User.js
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 @@ -172,10 +192,14 @@ class User extends Base {

/**
* A link to the user's avatar decoration.
* @param {BaseImageURLOptions} [options={}] Options for the image URL
* @param {ImageURLOptions} [options={}] Options for the image URL
* @returns {?string}
*/
avatarDecorationURL(options = {}) {
if (this.avatarDecorationData) {
return this.client.rest.cdn.avatarDecoration(this.avatarDecorationData.asset, options);
}

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

Expand Down
9 changes: 8 additions & 1 deletion packages/discord.js/typings/index.d.ts
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 */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/** @deprecated Use {@link avatarDecorationData} instead */
/** @deprecated Use {@link User.avatarDecorationData} instead */

public avatarDecoration: string | null;
public avatarDecorationData: AvatarDecorationData | null;
public banner: string | null | undefined;
public bot: boolean;
public get createdAt(): Date;
Expand All @@ -3155,7 +3162,7 @@ export class User extends PartialTextBasedChannel(Base) {
public get tag(): string;
public username: string;
public avatarURL(options?: ImageURLOptions): string | null;
public avatarDecorationURL(options?: BaseImageURLOptions): string | null;
public avatarDecorationURL(options?: ImageURLOptions): string | null;
public bannerURL(options?: ImageURLOptions): string | null | undefined;
public createDM(force?: boolean): Promise<DMChannel>;
public deleteDM(): Promise<DMChannel>;
Expand Down
8 changes: 8 additions & 0 deletions packages/rest/__tests__/CDN.test.ts
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 presets default', () => {
expect(cdn.avatarDecoration(hash)).toEqual(`${base}/avatar-decoration-presets/${hash}.webp`);
});

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
Expand Up @@ -97,9 +97,18 @@ export class CDN {
return this.dynamicMakeURL(`/avatars/${id}/${avatarHash}`, avatarHash, options);
}

/**
* Generates a user avatar decoration preset URL.
*
* @param asset - The avatar decoration hash
* @param options - Optional options for the avatar decoration preset
*/
public avatarDecoration(asset: string, options?: Readonly<ImageURLOptions>): string;

/**
* Generates a user avatar decoration URL.
*
* @deprecated This overload is deprecated. Pass an hash instead.
almeidx marked this conversation as resolved.
Show resolved Hide resolved
* @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
Expand All @@ -108,8 +117,18 @@ export class CDN {
userId: string,
userAvatarDecoration: string,
options?: Readonly<BaseImageURLOptions>,
): string;

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

return this.dynamicMakeURL(`/avatar-decoration-presets/${userIdOrAsset}`, userIdOrAsset, assetOrOptions);
}

/**
Expand Down