Skip to content

Commit

Permalink
feat(User): add avatarDecorationData
Browse files Browse the repository at this point in the history
  • Loading branch information
almeidx committed Oct 15, 2023
1 parent 19ea0ba commit 88e85b6
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
26 changes: 25 additions & 1 deletion 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 @@ -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
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 */
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
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 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
Original file line number Diff line number Diff line change
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.
* @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>,
): 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

0 comments on commit 88e85b6

Please sign in to comment.