forked from botlabs-gg/discordgo
/
user.go
91 lines (75 loc) · 2.1 KB
/
user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package discordgo
import (
"fmt"
"strings"
"github.com/jonas747/gojay"
)
// A User stores all data for an individual Discord user.
type User struct {
// The ID of the user.
ID int64 `json:"id,string"`
// The user's username.
Username string `json:"username"`
// The hash of the user's avatar. Use Session.UserAvatar
// to retrieve the avatar itself.
Avatar string `json:"avatar"`
// The user's chosen language option.
Locale string `json:"locale"`
// The discriminator of the user (4 numbers after name).
Discriminator string `json:"discriminator"`
// Whether the user is a bot.
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)
}
// Mention return a string which mentions the user
func (u *User) Mention() string {
return fmt.Sprintf("<@%d>", u.ID)
}
// implement gojay.UnmarshalerJSONObject
func (u *User) UnmarshalJSONObject(dec *gojay.Decoder, key string) error {
switch key {
case "id":
return DecodeSnowflake(&u.ID, dec)
case "username":
return dec.String(&u.Username)
case "avatar":
return dec.String(&u.Avatar)
case "locale":
return dec.String(&u.Locale)
case "discriminator":
return dec.String(&u.Discriminator)
case "bot":
return dec.Bool(&u.Bot)
}
return nil
}
func (u *User) NKeys() int {
return 0
}
// AvatarURL returns a URL to the user's avatar.
// size: The size of the user's avatar as a power of two
// if size is an empty string, no size parameter will
// be added to the URL.
func (u *User) AvatarURL(size string) string {
var URL string
if u.Avatar == "" {
URL = EndpointDefaultUserAvatar(u.Discriminator)
} else if strings.HasPrefix(u.Avatar, "a_") {
URL = EndpointUserAvatarAnimated(u.ID, u.Avatar)
} else {
URL = EndpointUserAvatar(u.ID, u.Avatar)
}
if size != "" {
return URL + "?size=" + size
}
return URL
}
// A SelfUser stores user data about the token owner.
// Includes a few extra fields than a normal user struct.
type SelfUser struct {
*User
Token string `json:"token"`
}