From 6c0ee43b6c868e2b8992d2f005608d3edd0b01b4 Mon Sep 17 00:00:00 2001 From: nemoola <144327005+nemoola@users.noreply.github.com> Date: Fri, 1 Mar 2024 23:14:47 +0900 Subject: [PATCH 1/6] Fix and change DisplayName --- examples/voice_receive/go.mod | 51 +++++++++++++++++++++++++++++++++++ message.go | 9 +++++++ message_test.go | 29 ++++++++++++++++++++ structs_test.go | 36 ------------------------- 4 files changed, 89 insertions(+), 36 deletions(-) delete mode 100644 structs_test.go diff --git a/examples/voice_receive/go.mod b/examples/voice_receive/go.mod index f6bbdeb80..11d104253 100644 --- a/examples/voice_receive/go.mod +++ b/examples/voice_receive/go.mod @@ -1,7 +1,58 @@ module github.com/bwmarrin/discordgo/examples/voice_receive +go 1.22.0 + require ( github.com/bwmarrin/discordgo v0.21.1 github.com/pion/rtp v1.6.0 github.com/pion/webrtc/v3 v3.0.0-20200721060053-ca3cc9d940bc ) + +require ( + github.com/cheekybits/genny v1.0.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/fsnotify/fsnotify v1.4.7 // indirect + github.com/golang/mock v1.2.0 // indirect + github.com/golang/protobuf v1.2.0 // indirect + github.com/google/uuid v1.1.1 // indirect + github.com/gorilla/websocket v1.4.0 // indirect + github.com/hpcloud/tail v1.0.0 // indirect + github.com/kr/pretty v0.1.0 // indirect + github.com/kr/pty v1.1.1 // indirect + github.com/kr/text v0.1.0 // indirect + github.com/lucas-clemente/quic-go v0.7.1-0.20190401152353-907071221cf9 // indirect + github.com/marten-seemann/qtls v0.2.3 // indirect + github.com/onsi/ginkgo v1.7.0 // indirect + github.com/onsi/gomega v1.4.3 // indirect + github.com/pion/datachannel v1.4.19 // indirect + github.com/pion/dtls/v2 v2.0.2 // indirect + github.com/pion/ice/v2 v2.0.0-rc.8 // indirect + github.com/pion/logging v0.2.2 // indirect + github.com/pion/mdns v0.0.4 // indirect + github.com/pion/quic v0.1.1 // indirect + github.com/pion/randutil v0.1.0 // indirect + github.com/pion/rtcp v1.2.3 // indirect + github.com/pion/sctp v1.7.8 // indirect + github.com/pion/sdp/v2 v2.4.0 // indirect + github.com/pion/srtp v1.5.0 // indirect + github.com/pion/stun v0.3.5 // indirect + github.com/pion/transport v0.10.1 // indirect + github.com/pion/turn/v2 v2.0.4 // indirect + github.com/pion/udp v0.1.0 // indirect + github.com/pkg/errors v0.9.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/sclevine/agouti v3.0.0+incompatible // indirect + github.com/stretchr/objx v0.1.0 // indirect + github.com/stretchr/testify v1.6.1 // indirect + golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect + golang.org/x/net v0.0.0-20200625001655-4c5254603344 // indirect + golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f // indirect + golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd // indirect + golang.org/x/text v0.3.0 // indirect + golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect + gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect + gopkg.in/fsnotify.v1 v1.4.7 // indirect + gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect + gopkg.in/yaml.v2 v2.2.2 // indirect + gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect +) diff --git a/message.go b/message.go index 3b03ed5f9..9c5e34ad0 100644 --- a/message.go +++ b/message.go @@ -567,3 +567,12 @@ 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 + } + return m.Author.GlobalName +} diff --git a/message_test.go b/message_test.go index 270c4b8c3..5c41e1e81 100644 --- a/message_test.go +++ b/message_test.go @@ -50,3 +50,32 @@ func TestGettingEmojisFromMessage(t *testing.T) { } } + +func TestMember_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) + } + }) +} diff --git a/structs_test.go b/structs_test.go deleted file mode 100644 index cde552067..000000000 --- a/structs_test.go +++ /dev/null @@ -1,36 +0,0 @@ -// Discordgo - Discord bindings for Go -// Available at https://github.com/bwmarrin/discordgo - -// Copyright 2015-2016 Bruce Marriner . 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) - } - }) -} From 4720fe47c6d5ad99de474e2921fef835f6bdb496 Mon Sep 17 00:00:00 2001 From: nemoola <144327005+nemoola@users.noreply.github.com> Date: Fri, 1 Mar 2024 23:22:42 +0900 Subject: [PATCH 2/6] Undo examples/voice_receive/go.mod --- examples/voice_receive/go.mod | 51 ----------------------------------- 1 file changed, 51 deletions(-) diff --git a/examples/voice_receive/go.mod b/examples/voice_receive/go.mod index 11d104253..f6bbdeb80 100644 --- a/examples/voice_receive/go.mod +++ b/examples/voice_receive/go.mod @@ -1,58 +1,7 @@ module github.com/bwmarrin/discordgo/examples/voice_receive -go 1.22.0 - require ( github.com/bwmarrin/discordgo v0.21.1 github.com/pion/rtp v1.6.0 github.com/pion/webrtc/v3 v3.0.0-20200721060053-ca3cc9d940bc ) - -require ( - github.com/cheekybits/genny v1.0.0 // indirect - github.com/davecgh/go-spew v1.1.1 // indirect - github.com/fsnotify/fsnotify v1.4.7 // indirect - github.com/golang/mock v1.2.0 // indirect - github.com/golang/protobuf v1.2.0 // indirect - github.com/google/uuid v1.1.1 // indirect - github.com/gorilla/websocket v1.4.0 // indirect - github.com/hpcloud/tail v1.0.0 // indirect - github.com/kr/pretty v0.1.0 // indirect - github.com/kr/pty v1.1.1 // indirect - github.com/kr/text v0.1.0 // indirect - github.com/lucas-clemente/quic-go v0.7.1-0.20190401152353-907071221cf9 // indirect - github.com/marten-seemann/qtls v0.2.3 // indirect - github.com/onsi/ginkgo v1.7.0 // indirect - github.com/onsi/gomega v1.4.3 // indirect - github.com/pion/datachannel v1.4.19 // indirect - github.com/pion/dtls/v2 v2.0.2 // indirect - github.com/pion/ice/v2 v2.0.0-rc.8 // indirect - github.com/pion/logging v0.2.2 // indirect - github.com/pion/mdns v0.0.4 // indirect - github.com/pion/quic v0.1.1 // indirect - github.com/pion/randutil v0.1.0 // indirect - github.com/pion/rtcp v1.2.3 // indirect - github.com/pion/sctp v1.7.8 // indirect - github.com/pion/sdp/v2 v2.4.0 // indirect - github.com/pion/srtp v1.5.0 // indirect - github.com/pion/stun v0.3.5 // indirect - github.com/pion/transport v0.10.1 // indirect - github.com/pion/turn/v2 v2.0.4 // indirect - github.com/pion/udp v0.1.0 // indirect - github.com/pkg/errors v0.9.1 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/sclevine/agouti v3.0.0+incompatible // indirect - github.com/stretchr/objx v0.1.0 // indirect - github.com/stretchr/testify v1.6.1 // indirect - golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect - golang.org/x/net v0.0.0-20200625001655-4c5254603344 // indirect - golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f // indirect - golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd // indirect - golang.org/x/text v0.3.0 // indirect - golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect - gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect - gopkg.in/fsnotify.v1 v1.4.7 // indirect - gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect - gopkg.in/yaml.v2 v2.2.2 // indirect - gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect -) From 4b6c3779994364fe142cee59fb93382b3dbc28eb Mon Sep 17 00:00:00 2001 From: nemoola <144327005+nemoola@users.noreply.github.com> Date: Fri, 1 Mar 2024 23:49:07 +0900 Subject: [PATCH 3/6] Delete structs.DisplayName() --- structs.go | 9 --------- 1 file changed, 9 deletions(-) diff --git a/structs.go b/structs.go index 0a0d38d15..ae653d0d7 100644 --- a/structs.go +++ b/structs.go @@ -1557,15 +1557,6 @@ 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"` From cce7c3519817ca8c8802a59983b0a98eb4fa86a1 Mon Sep 17 00:00:00 2001 From: nemoola <144327005+nemoola@users.noreply.github.com> Date: Sat, 2 Mar 2024 00:19:20 +0900 Subject: [PATCH 4/6] Undo structs.DisplayName() and structs_test.go --- structs.go | 9 +++++++++ structs_test.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 structs_test.go diff --git a/structs.go b/structs.go index ae653d0d7..0a0d38d15 100644 --- a/structs.go +++ b/structs.go @@ -1557,6 +1557,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"` diff --git a/structs_test.go b/structs_test.go new file mode 100644 index 000000000..cde552067 --- /dev/null +++ b/structs_test.go @@ -0,0 +1,36 @@ +// Discordgo - Discord bindings for Go +// Available at https://github.com/bwmarrin/discordgo + +// Copyright 2015-2016 Bruce Marriner . 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) + } + }) +} From 4c746904f9d2fbc75d8757ad2010f6af1cbe0ed9 Mon Sep 17 00:00:00 2001 From: Nemoola <144327005+nemoola@users.noreply.github.com> Date: Sat, 2 Mar 2024 08:27:34 +0900 Subject: [PATCH 5/6] Fix test name in message_test.go --- message_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/message_test.go b/message_test.go index 5c41e1e81..062739889 100644 --- a/message_test.go +++ b/message_test.go @@ -51,7 +51,7 @@ func TestGettingEmojisFromMessage(t *testing.T) { } -func TestMember_DisplayName(t *testing.T) { +func TestMessage_DisplayName(t *testing.T) { user := &User{ GlobalName: "Global", } From c6330482998dd80f9f47c9cff418cf941813c202 Mon Sep 17 00:00:00 2001 From: nemoola <144327005+nemoola@users.noreply.github.com> Date: Sat, 2 Mar 2024 13:18:37 +0900 Subject: [PATCH 6/6] Support Bot --- message.go | 2 ++ message_test.go | 29 +++++++++++++++++++++++++++++ structs.go | 2 ++ structs_test.go | 31 +++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+) diff --git a/message.go b/message.go index 9c5e34ad0..8a9335302 100644 --- a/message.go +++ b/message.go @@ -573,6 +573,8 @@ type MessageInteraction struct { 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 062739889..2955281d8 100644 --- a/message_test.go +++ b/message_test.go @@ -78,4 +78,33 @@ func TestMessage_DisplayName(t *testing.T) { 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) + } + }) }