From e6653a6cb0732fc718b2696bc1adcf67148d1cc1 Mon Sep 17 00:00:00 2001 From: Synbulat Biishev Date: Fri, 5 May 2023 16:04:10 +0300 Subject: [PATCH] feat: change default avatar id to type --- packages/discord.js/src/structures/User.js | 6 +++--- packages/rest/src/index.ts | 2 +- packages/rest/src/lib/CDN.ts | 11 +++++++---- packages/rest/src/lib/utils/utils.ts | 6 +++--- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/packages/discord.js/src/structures/User.js b/packages/discord.js/src/structures/User.js index cd6da3329cfc1..5f8e5e3a80fa5 100644 --- a/packages/discord.js/src/structures/User.js +++ b/packages/discord.js/src/structures/User.js @@ -1,7 +1,7 @@ 'use strict'; const { userMention } = require('@discordjs/builders'); -const { calculateUserDefaultAvatarId } = require('@discordjs/rest'); +const { calculateUserDefaultAvatarType } = require('@discordjs/rest'); const { DiscordSnowflake } = require('@sapphire/snowflake'); const Base = require('./Base'); const TextBasedChannel = require('./interfaces/TextBasedChannel'); @@ -166,8 +166,8 @@ class User extends Base { * @readonly */ get defaultAvatarURL() { - const remainder = this.discriminator === '0' ? calculateUserDefaultAvatarId(this.id) : this.discriminator % 5; - return this.client.rest.cdn.defaultAvatar(remainder); + const type = this.discriminator === '0' ? calculateUserDefaultAvatarType(this.id) : this.discriminator % 5; + return this.client.rest.cdn.defaultAvatar(type); } /** diff --git a/packages/rest/src/index.ts b/packages/rest/src/index.ts index 4c128ebead602..fc88068fa6422 100644 --- a/packages/rest/src/index.ts +++ b/packages/rest/src/index.ts @@ -5,7 +5,7 @@ export * from './lib/errors/RateLimitError.js'; export * from './lib/RequestManager.js'; export * from './lib/REST.js'; export * from './lib/utils/constants.js'; -export { calculateUserDefaultAvatarId, makeURLSearchParams, parseResponse } from './lib/utils/utils.js'; +export { calculateUserDefaultAvatarType, makeURLSearchParams, parseResponse } from './lib/utils/utils.js'; /** * The {@link https://github.com/discordjs/discord.js/blob/main/packages/rest/#readme | @discordjs/rest} version diff --git a/packages/rest/src/lib/CDN.ts b/packages/rest/src/lib/CDN.ts index f091bf5c5cf86..4ccc3c7884b45 100644 --- a/packages/rest/src/lib/CDN.ts +++ b/packages/rest/src/lib/CDN.ts @@ -119,12 +119,15 @@ export class CDN { } /** - * Generates the default avatar URL for an user id. + * Generates a default avatar URL * - * @param userId - The user id modulo 5 + * @param type - The default avatar type + * @remarks + * To calculate the type for a user do `(userId >> 22) % 5`, + * or `discriminator % 5` if they're using the legacy username system. */ - public defaultAvatar(userId: number): string { - return this.makeURL(`/embed/avatars/${userId}`, { extension: 'png' }); + public defaultAvatar(type: number): string { + return this.makeURL(`/embed/avatars/${type}`, { extension: 'png' }); } /** diff --git a/packages/rest/src/lib/utils/utils.ts b/packages/rest/src/lib/utils/utils.ts index 40b795e99dcd3..cc76a664a085a 100644 --- a/packages/rest/src/lib/utils/utils.ts +++ b/packages/rest/src/lib/utils/utils.ts @@ -169,10 +169,10 @@ export async function onRateLimit(manager: RequestManager, rateLimitData: RateLi } /** - * Calculates the default avatar id for a given user id. + * Calculates the default avatar type for a given user id. * - * @param userId - The user id to calculate the default avatar id for + * @param userId - The user id to calculate the default avatar type for */ -export function calculateUserDefaultAvatarId(userId: string) { +export function calculateUserDefaultAvatarType(userId: string) { return Number(BigInt(userId) >> 22n) % 5; }