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 avatar decorations #8914

Merged
merged 2 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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