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

feat(member): add helper function for display name #1426

Merged
merged 1 commit into from
Dec 29, 2023
Merged
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
9 changes: 9 additions & 0 deletions structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1394,6 +1394,15 @@ func (m *Member) AvatarURL(size string) string {

}

// DisplayName returns the member's guild nickname if they have one,
// otherwise it returns their discord display name.
func (m *Member) DisplayName() string {
if m.Nick != "" {
return m.Nick
}
return m.User.GlobalName
}

// ClientStatus stores the online, offline, idle, or dnd status of each device of a Guild member.
type ClientStatus struct {
Desktop Status `json:"desktop"`
Expand Down
36 changes: 36 additions & 0 deletions structs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Discordgo - Discord bindings for Go
// Available at https://github.com/bwmarrin/discordgo

// Copyright 2015-2016 Bruce Marriner <[email protected]>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package discordgo

import (
"testing"
)

func TestMember_DisplayName(t *testing.T) {
user := &User{
GlobalName: "Global",
}
t.Run("no server nickname set", func(t *testing.T) {
m := &Member{
Nick: "",
User: 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 := &Member{
Nick: "Server",
User: user,
}
if dn := m.DisplayName(); dn != m.Nick {
t.Errorf("Member.DisplayName() = %v, want %v", dn, m.Nick)
}
})
}