diff --git a/message.go b/message.go index 3b03ed5f9..8a9335302 100644 --- a/message.go +++ b/message.go @@ -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 +} diff --git a/message_test.go b/message_test.go index 270c4b8c3..2955281d8 100644 --- a/message_test.go +++ b/message_test.go @@ -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) + } + }) +} diff --git a/structs.go b/structs.go index 0a0d38d15..4d6b9f51f 100644 --- a/structs.go +++ b/structs.go @@ -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 } diff --git a/structs_test.go b/structs_test.go index cde552067..52c9d3bf6 100644 --- a/structs_test.go +++ b/structs_test.go @@ -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: "", @@ -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", @@ -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) + } + }) }