From ba93e85d0da6dc8e30e7a23e1936f605c9ce21d1 Mon Sep 17 00:00:00 2001 From: Rodry <38259440+ImRodry@users.noreply.github.com> Date: Mon, 4 Oct 2021 23:16:59 +0100 Subject: [PATCH] refactor(User): set accentColor and banner to undefined when not yet received (#6721) Co-authored-by: Sugden <28943913+NotSugden@users.noreply.github.com> --- src/errors/Messages.js | 1 + src/structures/User.js | 18 ++++++++++-------- typings/index.d.ts | 6 +++--- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/errors/Messages.js b/src/errors/Messages.js index affb80fa8..2e5eb6c6b 100644 --- a/src/errors/Messages.js +++ b/src/errors/Messages.js @@ -60,6 +60,7 @@ const Messages = { FILE_NOT_FOUND: file => `File could not be found: ${file}`, + USER_BANNER_NOT_FETCHED: "You must fetch this user's banner before trying to generate its URL!", USER_NO_DMCHANNEL: 'No DM Channel exists!', VOICE_NOT_STAGE_CHANNEL: 'You are only allowed to do this in stage channels.', diff --git a/src/structures/User.js b/src/structures/User.js index 2cc7af49c..1f9070aaf 100644 --- a/src/structures/User.js +++ b/src/structures/User.js @@ -74,23 +74,23 @@ class User extends Base { if ('banner' in data) { /** * The user banner's hash - * The user must be force fetched for this property to be present + * The user must be force fetched for this property to be present or be updated * @type {?string} */ this.banner = data.banner; - } else { - this.banner ??= null; + } else if (this.banner !== null) { + this.banner ??= undefined; } if ('accent_color' in data) { /** * The base 10 accent color of the user's banner - * The user must be force fetched for this property to be present + * The user must be force fetched for this property to be present or be updated * @type {?number} */ this.accentColor = data.accent_color; - } else { - this.accentColor ??= null; + } else if (this.accentColor !== null) { + this.accentColor ??= undefined; } if ('system' in data) { @@ -175,17 +175,19 @@ class User extends Base { * @readonly */ get hexAccentColor() { - if (!this.accentColor) return null; + if (typeof this.accentColor !== 'number') return this.accentColor; return `#${this.accentColor.toString(16).padStart(6, '0')}`; } /** * A link to the user's banner. - * The user must be force fetched for this property to be present + * This method will throw an error if called before the user is force fetched. + * See {@link User#banner} for more info * @param {ImageURLOptions} [options={}] Options for the Image URL * @returns {?string} */ bannerURL({ format, size, dynamic } = {}) { + if (typeof this.banner === 'undefined') throw new Error('USER_BANNER_NOT_FETCHED'); if (!this.banner) return null; return this.client.rest.cdn.Banner(this.id, this.banner, format, size, dynamic); } diff --git a/typings/index.d.ts b/typings/index.d.ts index 6dd6498e0..d120dc559 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -2039,9 +2039,9 @@ export class User extends PartialTextBasedChannel(Base) { protected constructor(client: Client, data: RawUserData); private _equals(user: APIUser): boolean; - public accentColor: number | null; + public accentColor: number | null | undefined; public avatar: string | null; - public banner: string | null; + public banner: string | null | undefined; public bot: boolean; public readonly createdAt: Date; public readonly createdTimestamp: number; @@ -2049,7 +2049,7 @@ export class User extends PartialTextBasedChannel(Base) { public readonly defaultAvatarURL: string; public readonly dmChannel: DMChannel | null; public flags: Readonly | null; - public readonly hexAccentColor: HexColorString | null; + public readonly hexAccentColor: HexColorString | null | undefined; public id: Snowflake; public readonly partial: false; public system: boolean;