feat(User): add DisplayName method (#1609)

This commit is contained in:
ozraru 2025-04-13 22:55:15 +09:00 committed by GitHub
parent 5571950c90
commit f75d834aa5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 33 additions and 3 deletions

View file

@ -1635,7 +1635,7 @@ func (m *Member) DisplayName() string {
if 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.

View file

@ -20,8 +20,9 @@ func TestMember_DisplayName(t *testing.T) {
Nick: "",
User: user,
}
if dn := m.DisplayName(); dn != user.GlobalName {
t.Errorf("Member.DisplayName() = %v, want %v", dn, user.GlobalName)
want := user.DisplayName()
if dn := m.DisplayName(); dn != want {
t.Errorf("Member.DisplayName() = %v, want %v", dn, want)
}
})
t.Run("server nickname set", func(t *testing.T) {

View file

@ -152,3 +152,11 @@ func (u *User) DefaultAvatarIndex() int {
id, _ := strconv.Atoi(u.Discriminator)
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
}

View file

@ -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)
}
})
}