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

Fix and change DisplayName #1503

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,3 +567,14 @@ type MessageInteraction struct {
// Member is only present when the interaction is from a guild.
Member *Member `json:"member"`
}

// DisplayName returns the member's guild nickname if they have one,
// otherwise it returns their discord display name.
func (m *Message) DisplayName() string {
if m.Member.Nick != "" {
return m.Member.Nick
} else if m.Author.Bot {
return m.Author.Username
}
return m.Author.GlobalName
}
58 changes: 58 additions & 0 deletions message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,61 @@ func TestGettingEmojisFromMessage(t *testing.T) {
}

}

func TestMessage_DisplayName(t *testing.T) {
user := &User{
GlobalName: "Global",
}
t.Run("no server nickname set", func(t *testing.T) {
m := &Message{
Member: &Member{
Nick: "",
},
Author: user,
}
if dn := m.DisplayName(); dn != user.GlobalName {
t.Errorf("Member.DisplayName() = %v, want %v", dn, user.GlobalName)
}
})

t.Run("server nickname set", func(t *testing.T) {
m := &Message{
Member: &Member{
Nick: "Server",
},
Author: user,
}
if dn := m.DisplayName(); dn != m.Member.Nick {
t.Errorf("Member.DisplayName() = %v, want %v", dn, m.Member.Nick)
}
})

bot := &User{
Username: "Bot",
Bot: true,
}

t.Run("bot no server nickname set", func(t *testing.T) {
m := &Message{
Member: &Member{
Nick: "",
},
Author: bot,
}
if dn := m.DisplayName(); dn != m.Author.Username {
t.Errorf("Member.DisplayName() = %v, want %v", dn, m.Author.Username)
}
})

t.Run("bot server nickname set", func(t *testing.T) {
m := &Message{
Member: &Member{
Nick: "Server",
},
Author: bot,
}
if dn := m.DisplayName(); dn != m.Member.Nick {
t.Errorf("Member.DisplayName() = %v, want %v", dn, m.Member.Nick)
}
})
}
2 changes: 2 additions & 0 deletions structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1562,6 +1562,8 @@ func (m *Member) AvatarURL(size string) string {
func (m *Member) DisplayName() string {
if m.Nick != "" {
return m.Nick
} else if m.User.Bot {
return m.User.Username
}
return m.User.GlobalName
}
Expand Down
31 changes: 31 additions & 0 deletions structs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func TestMember_DisplayName(t *testing.T) {
user := &User{
GlobalName: "Global",
}

t.Run("no server nickname set", func(t *testing.T) {
m := &Member{
Nick: "",
Expand All @@ -24,6 +25,7 @@ func TestMember_DisplayName(t *testing.T) {
t.Errorf("Member.DisplayName() = %v, want %v", dn, user.GlobalName)
}
})

t.Run("server nickname set", func(t *testing.T) {
m := &Member{
Nick: "Server",
Expand All @@ -33,4 +35,33 @@ func TestMember_DisplayName(t *testing.T) {
t.Errorf("Member.DisplayName() = %v, want %v", dn, m.Nick)
}
})

bot := &User{
Username: "Bot",
Bot: true,
}

t.Run("bot no server nickname set", func(t *testing.T) {
m := &Message{
Member: &Member{
Nick: "",
},
Author: bot,
}
if dn := m.DisplayName(); dn != m.Author.Username {
t.Errorf("Member.DisplayName() = %v, want %v", dn, m.Author.Username)
}
})

t.Run("bot server nickname set", func(t *testing.T) {
m := &Message{
Member: &Member{
Nick: "Server",
},
Author: bot,
}
if dn := m.DisplayName(); dn != m.Member.Nick {
t.Errorf("Member.DisplayName() = %v, want %v", dn, m.Member.Nick)
}
})
}