Skip to content

Commit f75d834

Browse files
authored
feat(User): add DisplayName method (#1609)
1 parent 5571950 commit f75d834

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

structs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1635,7 +1635,7 @@ func (m *Member) DisplayName() string {
16351635
if m.Nick != "" {
16361636
return m.Nick
16371637
}
1638-
return m.User.GlobalName
1638+
return m.User.DisplayName()
16391639
}
16401640

16411641
// ClientStatus stores the online, offline, idle, or dnd status of each device of a Guild member.

structs_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ func TestMember_DisplayName(t *testing.T) {
2020
Nick: "",
2121
User: user,
2222
}
23-
if dn := m.DisplayName(); dn != user.GlobalName {
24-
t.Errorf("Member.DisplayName() = %v, want %v", dn, user.GlobalName)
23+
want := user.DisplayName()
24+
if dn := m.DisplayName(); dn != want {
25+
t.Errorf("Member.DisplayName() = %v, want %v", dn, want)
2526
}
2627
})
2728
t.Run("server nickname set", func(t *testing.T) {

user.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,11 @@ func (u *User) DefaultAvatarIndex() int {
152152
id, _ := strconv.Atoi(u.Discriminator)
153153
return id % 5
154154
}
155+
156+
// DisplayName returns the user's global name if they have one, otherwise it returns their username.
157+
func (u *User) DisplayName() string {
158+
if u.GlobalName != "" {
159+
return u.GlobalName
160+
}
161+
return u.Username
162+
}

user_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,24 @@ func TestUser_String(t *testing.T) {
3535
})
3636
}
3737
}
38+
39+
func TestUser_DisplayName(t *testing.T) {
40+
t.Run("no global name set", func(t *testing.T) {
41+
u := &User{
42+
GlobalName: "",
43+
Username: "username",
44+
}
45+
if dn := u.DisplayName(); dn != u.Username {
46+
t.Errorf("User.DisplayName() = %v, want %v", dn, u.Username)
47+
}
48+
})
49+
t.Run("global name set", func(t *testing.T) {
50+
u := &User{
51+
GlobalName: "global",
52+
Username: "username",
53+
}
54+
if dn := u.DisplayName(); dn != u.GlobalName {
55+
t.Errorf("User.DisplayName() = %v, want %v", dn, u.GlobalName)
56+
}
57+
})
58+
}

0 commit comments

Comments
 (0)