From 983816b0c71e0f2c1e2c0080c6ef12961ddd8dec Mon Sep 17 00:00:00 2001 From: Elysia <71698422+aiko-chan-ai@users.noreply.github.com> Date: Wed, 14 Jun 2023 14:00:48 +0700 Subject: [PATCH] #9634 djs --- src/structures/GuildMember.js | 4 +-- src/structures/User.js | 46 ++++++++++++++++++++++++++++++++--- src/util/Constants.js | 2 +- src/util/Util.js | 13 ++++++++-- typings/index.d.ts | 6 ++++- 5 files changed, 61 insertions(+), 10 deletions(-) diff --git a/src/structures/GuildMember.js b/src/structures/GuildMember.js index 0e9aa30..ab2cc34 100644 --- a/src/structures/GuildMember.js +++ b/src/structures/GuildMember.js @@ -321,12 +321,12 @@ class GuildMember extends Base { } /** - * The nickname of this member, or their username if they don't have one + * The nickname of this member, or their user display name if they don't have one * @type {?string} * @readonly */ get displayName() { - return this.nickname ?? this.user.username; + return this.nickname ?? this.user.displayName; } /** diff --git a/src/structures/User.js b/src/structures/User.js index 14f9141..a2de2ea 100644 --- a/src/structures/User.js +++ b/src/structures/User.js @@ -1,5 +1,6 @@ 'use strict'; +const process = require('node:process'); const { Collection } = require('@discordjs/collection'); const Base = require('./Base'); const ClientApplication = require('./ClientApplication'); @@ -9,6 +10,9 @@ const { Error } = require('../errors'); const { RelationshipTypes, NitroType } = require('../util/Constants'); const SnowflakeUtil = require('../util/SnowflakeUtil'); const UserFlags = require('../util/UserFlags'); +const Util = require('../util/Util'); + +let tagDeprecationEmitted = false; /** * Represents a user on Discord. @@ -94,6 +98,16 @@ class User extends Base { this.username ??= null; } + if ('global_name' in data) { + /** + * The global name of this user + * @type {?string} + */ + this.globalName = data.global_name; + } else { + this.globalName ??= null; + } + if ('bot' in data) { /** * Whether or not the user is a bot @@ -110,7 +124,8 @@ class User extends Base { if ('discriminator' in data) { /** - * A discriminator based on username for the user + * The discriminator of this user + * `'0'`, or a 4-digit stringified number if they're using the legacy username system * @type {?string} */ this.discriminator = data.discriminator; @@ -453,7 +468,8 @@ class User extends Base { * @readonly */ get defaultAvatarURL() { - return this.client.rest.cdn.DefaultAvatar(this.discriminator % 5); + const index = this.discriminator === '0' ? Util.calculateUserDefaultAvatarIndex(this.id) : this.discriminator % 5; + return this.client.rest.cdn.DefaultAvatar(index); } /** @@ -527,12 +543,32 @@ class User extends Base { } /** - * The Discord "tag" (e.g. `hydrabolt#0001`) for this user + * The tag of this user + * This user's username, or their legacy tag (e.g. `hydrabolt#0001`) + * if they're using the legacy username system * @type {?string} + * @deprecated Use {@link User#username} instead. * @readonly */ get tag() { - return typeof this.username === 'string' ? `${this.username}#${this.discriminator}` : null; + if (!tagDeprecationEmitted) { + process.emitWarning('User#tag is deprecated. Use User#username instead.', 'DeprecationWarning'); + tagDeprecationEmitted = true; + } + return typeof this.username === 'string' + ? this.discriminator === '0' + ? this.username + : `${this.username}#${this.discriminator}` + : null; + } + + /** + * The global name of this user, or their username if they don't have one + * @type {?string} + * @readonly + */ + get displayName() { + return this.globalName ?? this.username; } /** @@ -574,6 +610,7 @@ class User extends Base { this.id === user.id && this.username === user.username && this.discriminator === user.discriminator && + this.globalName === user.globalName && this.avatar === user.avatar && this.flags?.bitfield === user.flags?.bitfield && this.banner === user.banner && @@ -594,6 +631,7 @@ class User extends Base { this.id === user.id && this.username === user.username && this.discriminator === user.discriminator && + this.globalName === user.global_name && this.avatar === user.avatar && this.flags?.bitfield === user.public_flags && ('banner' in user ? this.banner === user.banner : true) && diff --git a/src/util/Constants.js b/src/util/Constants.js index cc01053..f944065 100644 --- a/src/util/Constants.js +++ b/src/util/Constants.js @@ -186,7 +186,7 @@ exports.Endpoints = { return { Emoji: (emojiId, format = 'webp') => `${root}/emojis/${emojiId}.${format}`, Asset: name => `${root}/assets/${name}`, - DefaultAvatar: discriminator => `${root}/embed/avatars/${discriminator}.png`, + DefaultAvatar: index => `${root}/embed/avatars/${index}.png`, Avatar: (userId, hash, format, size, dynamic = false) => { if (dynamic && hash.startsWith('a_')) format = 'gif'; return makeImageUrl(`${root}/avatars/${userId}/${hash}`, { format, size }); diff --git a/src/util/Util.js b/src/util/Util.js index e14f333..52d6b3b 100644 --- a/src/util/Util.js +++ b/src/util/Util.js @@ -709,8 +709,8 @@ class Util extends null { if (!files.length) return []; files = files.map((file, i) => ({ filename: file.name ?? file.attachment?.name ?? file.attachment?.filename ?? 'file.jpg', - // 8MB = 8388608 bytes - file_size: Math.floor((8_388_608 / 10) * Math.random()), + // 25MB = 26_214_400bytes + file_size: Math.floor((26_214_400 / 10) * Math.random()), id: `${i}`, })); const { attachments } = await client.api.channels[channelId].attachments.post({ @@ -733,6 +733,15 @@ class Util extends null { return false; } } + + /** + * Calculates the default avatar index for a given user id. + * @param {Snowflake} userId - The user id to calculate the default avatar index for + * @returns {number} + */ + static calculateUserDefaultAvatarIndex(userId) { + return Number(BigInt(userId) >> 22n) % 6; + } } module.exports = Util; diff --git a/typings/index.d.ts b/typings/index.d.ts index 9734d10..f8cd148 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -3260,14 +3260,17 @@ export class User extends PartialTextBasedChannel(Base) { public readonly relationships: RelationshipTypes; public readonly createdTimestamp: number; public discriminator: string; + public readonly displayName: string; public readonly defaultAvatarURL: string; public readonly dmChannel: DMChannel | null; public flags: Readonly | null; + public globalName: string | null; public botInGuildsCount: number | null | undefined; public readonly hexAccentColor: HexColorString | null | undefined; public id: Snowflake; public readonly partial: false; public system: boolean; + /** @deprecated Use {@link User#username} instead. */ public readonly tag: string; public username: string; public readonly note: string | null; @@ -3363,6 +3366,7 @@ export class Util extends null { public static splitMessage(text: string, options?: SplitOptions): string[]; /** @deprecated This will be removed in the next major version. */ public static resolveAutoArchiveMaxLimit(guild: Guild): Exclude; + public static calculateUserDefaultAvatarIndex(userId: Snowflake): number; } export class Formatters extends null { @@ -3658,7 +3662,7 @@ export const Constants: { dynamic: boolean, ): string; Banner(id: Snowflake, hash: string, format: DynamicImageFormat, size: AllowedImageSize, dynamic: boolean): string; - DefaultAvatar(discriminator: number): string; + DefaultAvatar(index: number): string; DiscoverySplash(guildId: Snowflake, hash: string, format: AllowedImageFormat, size: AllowedImageSize): string; Emoji(emojiId: Snowflake, format: DynamicImageFormat): string; GDMIcon(channelId: Snowflake, hash: string, format: AllowedImageFormat, size: AllowedImageSize): string;