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): banners and accent colors #6117

Merged
merged 17 commits into from Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from 13 commits
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
53 changes: 51 additions & 2 deletions src/structures/User.js
Expand Up @@ -75,6 +75,28 @@ class User extends Base {
this.avatar = null;
}

if ('banner' in data) {
/**
* The user banner's hash
* <info>The user must be force fetched</info>
iCrawl marked this conversation as resolved.
Show resolved Hide resolved
* @type {?string}
*/
this.banner = data.banner;
} else if (typeof this.banner !== 'string') {
this.banner = null;
}

if ('accent_color' in data) {
/**
* The base 10 accent color of the user
* <info>The user must be force fetched</info>
* @type {?number}
*/
this.accentColor = data.accent_color;
} else if (typeof this.accentColor === 'undefined') {
this.accentColor = null;
}
iCrawl marked this conversation as resolved.
Show resolved Hide resolved

if ('system' in data) {
/**
* Whether the user is an Official Discord System user (part of the urgent message system)
Expand Down Expand Up @@ -150,6 +172,28 @@ class User extends Base {
return this.avatarURL(options) ?? this.defaultAvatarURL;
}

/**
* The hexadecimal version of the user accent color, with a leading hash
* <info>The user must be force fetched</info>
iCrawl marked this conversation as resolved.
Show resolved Hide resolved
* @type {?string}
* @readonly
*/
get hexAccentColor() {
if (!this.accentColor) return null;
return `#${this.accentColor.toString(16).padStart(6, '0')}`;
}

/**
* A link to the user's banner.
* <info>The user must be force fetched</info>
iCrawl marked this conversation as resolved.
Show resolved Hide resolved
* @param {ImageURLOptions} [options={}] Options for the Image URL
* @returns {?string}
*/
bannerURL({ format, size, dynamic } = {}) {
if (!this.banner) return null;
return this.client.rest.cdn.Banner(this.id, this.banner, format, size, dynamic);
}

/**
* The Discord "tag" (e.g. `hydrabolt#0001`) for this user
* @type {?string}
Expand Down Expand Up @@ -200,7 +244,8 @@ class User extends Base {
}

/**
* Checks if the user is equal to another. It compares id, username, discriminator, avatar, and bot flags.
* Checks if the user is equal to another.
* It compares id, username, discriminator, avatar, banner, accent color, and bot flags.
* It is recommended to compare equality by using `user.id === user2.id` unless you want to compare all properties.
* @param {User} user User to compare with
* @returns {boolean}
Expand All @@ -211,7 +256,9 @@ class User extends Base {
this.id === user.id &&
this.username === user.username &&
this.discriminator === user.discriminator &&
this.avatar === user.avatar;
this.avatar === user.avatar &&
this.banner === user.banner &&
this.accentColor === user.accentColor;

return equal;
}
Expand Down Expand Up @@ -253,12 +300,14 @@ class User extends Base {
{
createdTimestamp: true,
defaultAvatarURL: true,
hexAccentColor: true,
tag: true,
},
...props,
);
json.avatarURL = this.avatarURL();
json.displayAvatarURL = this.displayAvatarURL();
json.bannerURL = this.bannerURL();
return json;
}

Expand Down
6 changes: 4 additions & 2 deletions src/util/Constants.js
Expand Up @@ -49,8 +49,10 @@ exports.Endpoints = {
if (dynamic) format = hash.startsWith('a_') ? 'gif' : format;
return makeImageUrl(`${root}/avatars/${userId}/${hash}`, { format, size });
},
Banner: (guildId, hash, format = 'webp', size) =>
makeImageUrl(`${root}/banners/${guildId}/${hash}`, { format, size }),
Banner: (id, hash, format = 'webp', size, dynamic = false) => {
if (dynamic) format = hash.startsWith('a_') ? 'gif' : format;
return makeImageUrl(`${root}/banners/${id}/${hash}`, { format, size });
},
Icon: (guildId, hash, format = 'webp', size, dynamic = false) => {
if (dynamic) format = hash.startsWith('a_') ? 'gif' : format;
return makeImageUrl(`${root}/icons/${guildId}/${hash}`, { format, size });
Expand Down
12 changes: 11 additions & 1 deletion typings/index.d.ts
Expand Up @@ -1822,20 +1822,24 @@ export class Typing extends Base {

export class User extends PartialTextBasedChannel(Base) {
public constructor(client: Client, data: RawUserData);
public accentColor: number | null;
public avatar: string | null;
public banner: string | null;
public bot: boolean;
public readonly createdAt: Date;
public readonly createdTimestamp: number;
public discriminator: string;
public readonly defaultAvatarURL: string;
public readonly dmChannel: DMChannel | null;
public flags: Readonly<UserFlags> | null;
public readonly hexAccentColor: string | null;
iCrawl marked this conversation as resolved.
Show resolved Hide resolved
public id: Snowflake;
public readonly partial: false;
public system: boolean;
public readonly tag: string;
public username: string;
public avatarURL(options?: ImageURLOptions): string | null;
public bannerURL(options?: ImageURLOptions): string | null;
public createDM(): Promise<DMChannel>;
public deleteDM(): Promise<DMChannel>;
public displayAvatarURL(options?: ImageURLOptions): string;
Expand Down Expand Up @@ -2144,7 +2148,13 @@ export const Constants: {
DefaultAvatar: (id: Snowflake | number) => string;
Emoji: (emojiId: Snowflake, format: DynamicImageFormat) => string;
Avatar: (userId: Snowflake, hash: string, format: DynamicImageFormat, size: AllowedImageSize) => string;
Banner: (guildId: Snowflake | number, hash: string, format: AllowedImageFormat, size: AllowedImageSize) => string;
Banner: (
id: Snowflake,
hash: string,
format: DynamicImageFormat,
size: AllowedImageSize,
dynamic: boolean,
) => string;
Icon: (userId: Snowflake | number, hash: string, format: DynamicImageFormat, size: AllowedImageSize) => string;
AppIcon: (userId: Snowflake | number, hash: string, format: AllowedImageFormat, size: AllowedImageSize) => string;
AppAsset: (
Expand Down