feat(User): add DisplayName method (#1609)
This commit is contained in:
parent
5571950c90
commit
f75d834aa5
4 changed files with 33 additions and 3 deletions
|
@ -1635,7 +1635,7 @@ func (m *Member) DisplayName() string {
|
||||||
if m.Nick != "" {
|
if m.Nick != "" {
|
||||||
return m.Nick
|
return m.Nick
|
||||||
}
|
}
|
||||||
return m.User.GlobalName
|
return m.User.DisplayName()
|
||||||
}
|
}
|
||||||
|
|
||||||
// ClientStatus stores the online, offline, idle, or dnd status of each device of a Guild member.
|
// ClientStatus stores the online, offline, idle, or dnd status of each device of a Guild member.
|
||||||
|
|
|
@ -20,8 +20,9 @@ func TestMember_DisplayName(t *testing.T) {
|
||||||
Nick: "",
|
Nick: "",
|
||||||
User: user,
|
User: user,
|
||||||
}
|
}
|
||||||
if dn := m.DisplayName(); dn != user.GlobalName {
|
want := user.DisplayName()
|
||||||
t.Errorf("Member.DisplayName() = %v, want %v", dn, user.GlobalName)
|
if dn := m.DisplayName(); dn != want {
|
||||||
|
t.Errorf("Member.DisplayName() = %v, want %v", dn, want)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
t.Run("server nickname set", func(t *testing.T) {
|
t.Run("server nickname set", func(t *testing.T) {
|
||||||
|
|
8
user.go
8
user.go
|
@ -152,3 +152,11 @@ func (u *User) DefaultAvatarIndex() int {
|
||||||
id, _ := strconv.Atoi(u.Discriminator)
|
id, _ := strconv.Atoi(u.Discriminator)
|
||||||
return id % 5
|
return id % 5
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DisplayName returns the user's global name if they have one, otherwise it returns their username.
|
||||||
|
func (u *User) DisplayName() string {
|
||||||
|
if u.GlobalName != "" {
|
||||||
|
return u.GlobalName
|
||||||
|
}
|
||||||
|
return u.Username
|
||||||
|
}
|
||||||
|
|
21
user_test.go
21
user_test.go
|
@ -35,3 +35,24 @@ func TestUser_String(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUser_DisplayName(t *testing.T) {
|
||||||
|
t.Run("no global name set", func(t *testing.T) {
|
||||||
|
u := &User{
|
||||||
|
GlobalName: "",
|
||||||
|
Username: "username",
|
||||||
|
}
|
||||||
|
if dn := u.DisplayName(); dn != u.Username {
|
||||||
|
t.Errorf("User.DisplayName() = %v, want %v", dn, u.Username)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
t.Run("global name set", func(t *testing.T) {
|
||||||
|
u := &User{
|
||||||
|
GlobalName: "global",
|
||||||
|
Username: "username",
|
||||||
|
}
|
||||||
|
if dn := u.DisplayName(); dn != u.GlobalName {
|
||||||
|
t.Errorf("User.DisplayName() = %v, want %v", dn, u.GlobalName)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue