-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support new username system (#9512)
* feat: support new username system * docs(GuildMember#displayName): update * fix(User#hasNewUsername): use User#discriminator * feat(User#tag): add new username * docs(User#tag): update * docs(User#hasNewUsername): update * docs: update * feat(User#defaultAvatarURL): update * feat: remove some newly added properties * docs(User#discriminator): update * docs(User#globalName): bad Copilot * feat(User#displayName): return display name * types(User#displayName): not a partial structure * feat: add `calculateUserDefaultAvatarId` function * docs(CND#defaultAvatar): update * docs(CDN#defaultAvatar): update * feat: change default avatar id to type * feat: deprecate `User#tag` * docs(CDN#defaultAvatar): update Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com> * docs(User): update Co-authored-by: Rodrigo Leitão <38259440+ImRodry@users.noreply.github.com> * feat: update * typing: update Co-authored-by: Jan <66554238+vaporoxx@users.noreply.github.com> * feat: change the param type to `Snowflake` --------- Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com> Co-authored-by: Rodrigo Leitão <38259440+ImRodry@users.noreply.github.com> Co-authored-by: Jan <66554238+vaporoxx@users.noreply.github.com>
- Loading branch information
1 parent
df8b6e9
commit 1ab60f9
Showing
6 changed files
with
64 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,15 @@ | ||
'use strict'; | ||
|
||
const process = require('node:process'); | ||
const { userMention } = require('@discordjs/builders'); | ||
const { calculateUserDefaultAvatarIndex } = require('@discordjs/rest'); | ||
const { DiscordSnowflake } = require('@sapphire/snowflake'); | ||
const Base = require('./Base'); | ||
const TextBasedChannel = require('./interfaces/TextBasedChannel'); | ||
const UserFlagsBitField = require('../util/UserFlagsBitField'); | ||
|
||
let tagDeprecationEmitted = false; | ||
|
||
/** | ||
* Represents a user on Discord. | ||
* @implements {TextBasedChannel} | ||
|
@@ -41,6 +45,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 | ||
|
@@ -53,7 +67,8 @@ class User extends Base { | |
|
||
if ('discriminator' in data) { | ||
/** | ||
* A discriminator based on username for the user | ||
* The discriminator of this user | ||
* <info>`'0'`, or a 4-digit stringified number if they're using the legacy username system</info> | ||
* @type {?string} | ||
*/ | ||
this.discriminator = data.discriminator; | ||
|
@@ -154,7 +169,8 @@ class User extends Base { | |
* @readonly | ||
*/ | ||
get defaultAvatarURL() { | ||
return this.client.rest.cdn.defaultAvatar(this.discriminator % 5); | ||
const index = this.discriminator === '0' ? calculateUserDefaultAvatarIndex(this.id) : this.discriminator % 5; | ||
return this.client.rest.cdn.defaultAvatar(index); | ||
} | ||
|
||
/** | ||
|
@@ -188,12 +204,33 @@ class User extends Base { | |
} | ||
|
||
/** | ||
* The Discord "tag" (e.g. `hydrabolt#0001`) for this user | ||
* The tag of this user | ||
* <info>This user's username, or their legacy tag (e.g. `hydrabolt#0001`) | ||
* if they're using the legacy username system</info> | ||
* @type {?string} | ||
* @readonly | ||
* @deprecated Use {@link User#username} instead. | ||
*/ | ||
get tag() { | ||
return typeof this.username === 'string' ? `${this.username}#${this.discriminator}` : null; | ||
if (!tagDeprecationEmitted) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
jaw0r3k
Contributor
|
||
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; | ||
} | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
why was
tag
deprecated but notdiscriminator
?