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 2 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
8 changes: 8 additions & 0 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,15 @@ 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.Discriminator == "0" || u.Discriminator == "" {
return u.Username
}

// The code below is a fallback to the special case for a pre-existing user
// before all users were migrated to the new format
// Notes: https://support-dev.discord.com/hc/en-us/articles/13667755828631
Aldiwildan77 marked this conversation as resolved.
Show resolved Hide resolved
return u.Username + "#" + u.Discriminator
Aldiwildan77 marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down
40 changes: 34 additions & 6 deletions user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,40 @@ import "testing"
func TestUser(t *testing.T) {
t.Parallel()

user := &User{
Username: "bob",
Discriminator: "8192",
tests := []struct {
name string
u *User
want string
}{
{
name: "Given a user with a username and discriminator, When String() is called, Then it should return the username and discriminator",
Aldiwildan77 marked this conversation as resolved.
Show resolved Hide resolved
u: &User{
Username: "bob",
Discriminator: "8192",
},
want: "bob#8192",
},
{
name: "Given a user with a username and no discriminator, When String() is called, Then it should return the username",
Aldiwildan77 marked this conversation as resolved.
Show resolved Hide resolved
u: &User{
Username: "aldiwildan",
},
want: "aldiwildan",
},
{
name: "Given a user with a username and a 0 discriminator, When String() is called, Then it should return the username",
Aldiwildan77 marked this conversation as resolved.
Show resolved Hide resolved
u: &User{
Username: "aldiwildan",
Discriminator: "0",
},
want: "aldiwildan",
},
}

if user.String() != "bob#8192" {
t.Errorf("user.String() == %v", user.String())
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.u.String(); got != tt.want {
t.Errorf("User.String() = %v, want %v", got, tt.want)
}
})
Aldiwildan77 marked this conversation as resolved.
Show resolved Hide resolved
}
}