Skip to content

Commit

Permalink
feat: change default avatar id to type
Browse files Browse the repository at this point in the history
  • Loading branch information
Syjalo committed May 6, 2023
1 parent 35d68d3 commit 9b3861e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
6 changes: 3 additions & 3 deletions packages/discord.js/src/structures/User.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/rest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 7 additions & 4 deletions packages/rest/src/lib/CDN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
}

/**
Expand Down
6 changes: 3 additions & 3 deletions packages/rest/src/lib/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 9b3861e

Please sign in to comment.