Skip to content

Commit

Permalink
feat: user avatar decorations (#8914)
Browse files Browse the repository at this point in the history
feat(User): add avatar decorations

Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com>
  • Loading branch information
almeidx and Jiralite authored Jul 13, 2023
1 parent ceab07b commit 8d97017
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
19 changes: 19 additions & 0 deletions packages/discord.js/src/structures/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ class User extends Base {
*/
this.flags = new UserFlagsBitField(data.public_flags);
}

if ('avatar_decoration' in data) {
/**
* The user avatar decoration's hash
* @type {?string}
*/
this.avatarDecoration = data.avatar_decoration;
} else {
this.avatarDecoration ??= null;
}
}

/**
Expand Down Expand Up @@ -160,6 +170,15 @@ class User extends Base {
return this.avatar && this.client.rest.cdn.avatar(this.id, this.avatar, options);
}

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

/**
* A link to the user's default avatar
* @type {string}
Expand Down
2 changes: 2 additions & 0 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3096,6 +3096,7 @@ export class User extends PartialTextBasedChannel(Base) {

public accentColor: number | null | undefined;
public avatar: string | null;
public avatarDecoration: string | null;
public banner: string | null | undefined;
public bot: boolean;
public get createdAt(): Date;
Expand All @@ -3113,6 +3114,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 bannerURL(options?: ImageURLOptions): string | null | undefined;
public createDM(force?: boolean): Promise<DMChannel>;
public deleteDM(): Promise<DMChannel>;
Expand Down
15 changes: 15 additions & 0 deletions packages/rest/src/lib/CDN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,21 @@ export class CDN {
return this.dynamicMakeURL(`/avatars/${id}/${avatarHash}`, avatarHash, options);
}

/**
* Generates a user avatar decoration URL.
*
* @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,
options?: Readonly<BaseImageURLOptions>,
): string {
return this.makeURL(`/avatar-decorations/${userId}/${userAvatarDecoration}`, options);
}

/**
* Generates a banner URL, e.g. for a user or a guild.
*
Expand Down

0 comments on commit 8d97017

Please sign in to comment.