Explorar o código

add String() method to User (#350)

* add String() method to User

* put User def in it's own file

* remove usage of t.Run for support w/ older Go versions
Ammar Bandukwala %!s(int64=7) %!d(string=hai) anos
pai
achega
52ad9e2feb
Modificáronse 3 ficheiros con 39 adicións e 13 borrados
  1. 0 13
      structs.go
  2. 23 0
      user.go
  3. 16 0
      user_test.go

+ 0 - 13
structs.go

@@ -337,19 +337,6 @@ type Member struct {
 	Roles    []string `json:"roles"`
 }
 
-// A User stores all data for an individual Discord user.
-type User struct {
-	ID            string `json:"id"`
-	Email         string `json:"email"`
-	Username      string `json:"username"`
-	Avatar        string `json:"avatar"`
-	Discriminator string `json:"discriminator"`
-	Token         string `json:"token"`
-	Verified      bool   `json:"verified"`
-	MFAEnabled    bool   `json:"mfa_enabled"`
-	Bot           bool   `json:"bot"`
-}
-
 // A Settings stores data for a specific users Discord client settings.
 type Settings struct {
 	RenderEmbeds           bool               `json:"render_embeds"`

+ 23 - 0
user.go

@@ -0,0 +1,23 @@
+package discordgo
+
+import (
+	"fmt"
+)
+
+// A User stores all data for an individual Discord user.
+type User struct {
+	ID            string `json:"id"`
+	Email         string `json:"email"`
+	Username      string `json:"username"`
+	Avatar        string `json:"avatar"`
+	Discriminator string `json:"discriminator"`
+	Token         string `json:"token"`
+	Verified      bool   `json:"verified"`
+	MFAEnabled    bool   `json:"mfa_enabled"`
+	Bot           bool   `json:"bot"`
+}
+
+//String returns a unique identifier of the form username#discriminator
+func (u *User) String() string {
+	return fmt.Sprintf("%s#%s", u.Username, u.Discriminator)
+}

+ 16 - 0
user_test.go

@@ -0,0 +1,16 @@
+package discordgo
+
+import "testing"
+
+func TestUser(t *testing.T) {
+	t.Parallel()
+
+	user := &User{
+		Username:      "bob",
+		Discriminator: "8192",
+	}
+
+	if user.String() != "bob#8192" {
+		t.Errorf("user.String() == %v", user.String())
+	}
+}