Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to new username system #66

Merged
merged 6 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions lib/structures/Member.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,14 @@ export default class Member extends Base {
get bot(): boolean {
return this.user.bot;
}

/** The 4 digits after the username of the user associated with this member. */
/** The Discord-tag of the user associated with this member. */
get discriminator(): string {
return this.user.discriminator;
}

/** The nick of this member if set, or the username of this member's user. */
/** The nick of this member if set, the display name of this member's user if set, or their username. */
get displayName(): string {
return this.nick ?? this.username;
return this.nick ?? this.user.globalName ?? this.username;
}

/** The guild this member is for. This will throw an error if the guild is not cached. */
Expand Down Expand Up @@ -172,7 +171,7 @@ export default class Member extends Base {
return this.user.system;
}

/** A combination of the user associated with this member's username and discriminator. */
/** The 4 digits after this user's username, if they have not been migrated. If migrated, this will be a single "0". */
get tag(): string {
return this.user.tag;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/structures/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ export default class Message<T extends AnyTextableChannel | Uncached = AnyTextab
this.type = data.type;
this.webhookID = data.webhook_id;
this.update(data);
this.author = data.author.discriminator === "0000" ? new User(data.author, client) : client.users.update(data.author);
// don't add webhook users to the cache
this.author = data.webhook_id === undefined ? client.users.update(data.author) : new User(data.author, client);
if (data.application_id === undefined) {
this.applicationID = null;
} else {
Expand Down
23 changes: 20 additions & 3 deletions lib/structures/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ export default class User extends Base {
banner?: string | null;
/** If this user is a bot. */
bot: boolean;
/** The 4 digits after the user's username. */
/** The 4 digits after this user's username, if they have not been migrated. If migrated, this will be a single "0". */
discriminator: string;
/** The user's display name, if set. */
globalName: string | null;
/** The user's public [flags](https://discord.com/developers/docs/resources/user#user-object-user-flags). */
publicFlags: number;
/** If this user is an official discord system user. */
Expand All @@ -32,6 +34,7 @@ export default class User extends Base {
this.avatar = null;
this.bot = !!data.bot;
this.discriminator = data.discriminator;
this.globalName = data.global_name;
this.publicFlags = 0;
this.system = !!data.system;
this.username = data.username;
Expand All @@ -54,6 +57,9 @@ export default class User extends Base {
if (data.discriminator !== undefined) {
this.discriminator = data.discriminator;
}
if (data.global_name !== undefined) {
this.globalName = data.global_name;
}
if (data.public_flags !== undefined) {
this.publicFlags = data.public_flags;
}
Expand All @@ -62,8 +68,15 @@ export default class User extends Base {
}
}

/** The default avatar value of this user (discriminator modulo 5). */
private get isMigrated(): boolean {
return this.globalName !== null && (this.discriminator === undefined || this.discriminator === "0");
}

/** The default avatar value of this user. */
get defaultAvatar(): number {
if (this.isMigrated) {
return Number(BigInt(this.id) >> 22n) % 6;
}
return Number(this.discriminator) % 5;
}

Expand All @@ -72,8 +85,11 @@ export default class User extends Base {
return `<@${this.id}>`;
}

/** a combination of this user's username and discriminator. */
/** This user's display name, if migrated, else a combination of the user's username and discriminator. */
get tag(): string {
if (this.isMigrated) {
return this.globalName!;
}
return `${this.username}#${this.discriminator}`;
}

Expand Down Expand Up @@ -118,6 +134,7 @@ export default class User extends Base {
banner: this.banner,
bot: this.bot,
discriminator: this.discriminator,
globalName: this.globalName,
publicFlags: this.publicFlags,
system: this.system,
username: this.username
Expand Down
1 change: 1 addition & 0 deletions lib/types/json.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,7 @@ export interface JSONUser extends JSONBase {
banner?: string | null;
bot: boolean;
discriminator: string;
globalName: string | null;
publicFlags: number;
system: boolean;
username: string;
Expand Down
7 changes: 4 additions & 3 deletions lib/types/users.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface RESTUser {
discriminator: string;
email?: string | null;
flags?: number;
global_name: string | null;
id: string;
locale?: string;
member?: RawMember;
Expand All @@ -22,10 +23,10 @@ export interface RESTUser {
username: string;
verified?: boolean;
}
export interface RawUser extends Pick<RESTUser, "id" | "username" | "discriminator" | "avatar" | "avatar_decoration" | "bot" | "system" | "banner" | "accent_color">, Required<Pick<RESTUser, "public_flags">> {}
export interface RawUser extends Pick<RESTUser, "id" | "username" | "discriminator" | "avatar" | "avatar_decoration" | "bot" | "system" | "banner" | "accent_color">, Required<Pick<RESTUser, "public_flags" | "global_name">> {}
export interface RawUserWithMember extends RawUser, Pick<RESTUser, "member"> {}
export interface RawOAuthUser extends Pick<RESTUser, "id" | "username" | "discriminator" | "avatar" | "avatar_decoration" | "bot" | "system">, Required<Pick<RESTUser, "banner" | "accent_color" | "locale" | "mfa_enabled" | "email" | "verified" | "flags" | "public_flags">> {}
export interface RawExtendedUser extends Pick<RawOAuthUser, "avatar" | "avatar_decoration" | "bot" | "discriminator" | "email" | "flags" | "id" | "mfa_enabled" | "username" | "verified"> {}
export interface RawOAuthUser extends Pick<RESTUser, "id" | "username" | "discriminator" | "avatar" | "avatar_decoration" | "bot" | "system" | "global_name">, Required<Pick<RESTUser, "banner" | "accent_color" | "locale" | "mfa_enabled" | "email" | "verified" | "flags" | "public_flags">> {}
export interface RawExtendedUser extends Pick<RawOAuthUser, "avatar" | "avatar_decoration" | "bot" | "discriminator" | "email" | "flags" | "id" | "mfa_enabled" | "username" | "verified" | "global_name"> {}

export interface EditSelfUserOptions {
/** The new avatar (buffer, or full data url). `null` to reset. */
Expand Down