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

feat: support new username system #1387

Merged
merged 19 commits into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ var (
uDiscriminatorInt, _ := strconv.Atoi(uDiscriminator)
return EndpointCDN + "embed/avatars/" + strconv.Itoa(uDiscriminatorInt%5) + ".png"
}
EndpointDefaultUserAvatarMigrated = func(userID string) string {
userIDInt, _ := strconv.Atoi(userID)
return EndpointCDN + "embed/avatars/" + strconv.Itoa((userIDInt>>22)%5) + ".png"
}
FedorLap2006 marked this conversation as resolved.
Show resolved Hide resolved
EndpointUserBanner = func(uID, cID string) string {
return EndpointCDNBanners + uID + "/" + cID + ".png"
}
Expand Down
36 changes: 34 additions & 2 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ type User struct {
// The discriminator of the user (4 numbers after name).
Discriminator string `json:"discriminator"`

// The user's display name, if it is set.
// For bots, this is the application name.
GlobalName string `json:"global_name"`

// The token of the user. This is only present for
// the user represented by the current session.
Token string `json:"token"`
Expand Down Expand Up @@ -81,7 +85,14 @@ type User struct {
}

// String returns a unique identifier of the form username#discriminator
// or username only if the discriminator is "0"
Aldiwildan77 marked this conversation as resolved.
Show resolved Hide resolved
func (u *User) String() string {
if u.isMigrated() {
return u.Username
}

// The code below handles applications and users without a migrated username.
// https://support-dev.discord.com/hc/en-us/articles/13667755828631
return u.Username + "#" + u.Discriminator
Aldiwildan77 marked this conversation as resolved.
Show resolved Hide resolved
}

Expand All @@ -94,9 +105,24 @@ func (u *User) Mention() string {
// size: The size of the user's avatar as a power of two
// if size is an empty string, no size parameter will
// be added to the URL.
//
// Update: https://discord.com/developers/docs/change-log#default-avatars
// Based on the update from new username
// Users on the legacy username will use the discriminator % 5
// Otherwise, users will use the (user_id >> 22) % 6
Aldiwildan77 marked this conversation as resolved.
Show resolved Hide resolved
func (u *User) AvatarURL(size string) string {
return avatarURL(u.Avatar, EndpointDefaultUserAvatar(u.Discriminator),
EndpointUserAvatar(u.ID, u.Avatar), EndpointUserAvatarAnimated(u.ID, u.Avatar), size)
defaultUserAvatar := EndpointDefaultUserAvatar(u.Discriminator)
if u.isMigrated() {
defaultUserAvatar = EndpointDefaultUserAvatarMigrated(u.ID)
}

FedorLap2006 marked this conversation as resolved.
Show resolved Hide resolved
return avatarURL(
u.Avatar,
defaultUserAvatar,
EndpointUserAvatar(u.ID, u.Avatar),
EndpointUserAvatarAnimated(u.ID, u.Avatar),
size,
)
}

// BannerURL returns the URL of the users's banner image.
Expand All @@ -105,3 +131,9 @@ func (u *User) AvatarURL(size string) string {
func (u *User) BannerURL(size string) string {
return bannerURL(u.Banner, EndpointUserBanner(u.ID, u.Banner), EndpointUserBannerAnimated(u.ID, u.Banner), size)
}

// isMigrated returns true if the user is migrated from the legacy username system
// https://discord.com/developers/docs/change-log#identifying-migrated-users
func (u *User) isMigrated() bool {
return u.Discriminator == "" || u.Discriminator == "0"
}
42 changes: 35 additions & 7 deletions user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,43 @@ package discordgo

import "testing"

func TestUser(t *testing.T) {
func TestUser_String(t *testing.T) {
t.Parallel()

user := &User{
Username: "bob",
Discriminator: "8192",
tests := []struct {
name string
u *User
want string
}{
{
name: "User with a discriminator",
u: &User{
Username: "bob",
Discriminator: "8192",
},
want: "bob#8192",
},
{
name: "User without a discriminator",
u: &User{
Username: "aldiwildan",
},
want: "aldiwildan",
},
{
name: "User with discriminator set to 0",
u: &User{
Username: "aldiwildan",
Discriminator: "0",
},
want: "aldiwildan",
},
}

if user.String() != "bob#8192" {
t.Errorf("user.String() == %v", user.String())
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
if got := tc.u.String(); got != tc.want {
t.Errorf("User.String() = %v, want %v", got, tc.want)
}
})
}
}