forked from bwmarrin/discordgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
197 lines (165 loc) · 5.62 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package discordgo
import (
"strconv"
"strings"
)
// A User stores all data for an individual Discord user.
type User struct {
// The ID of the user.
ID string `json:"id"`
// The user's username.
Username string `json:"username"`
// The discriminator of the user (4 numbers after name).
Discriminator string `json:"discriminator"`
// The display name of the user, if it is set. For bots, this is the application name.
GlobalName *string `json:"global_name"`
// The hash of the user's avatar. Use Session.UserAvatar
// to retrieve the avatar itself.
Avatar string `json:"avatar"`
// Whether the user is a bot.
Bot bool `json:"bot"`
// Whether the user is a system user.
System bool `json:"system"`
// Whether the user has multi-factor authentication enabled.
MFAEnabled bool `json:"mfa_enabled"`
// The user's banner hash.
Banner *string `json:"banner"`
// The user's banner color encoded as an integer representation of hexadecimal color code.
AccentColor *int `json:"accent_color"`
// The user's chosen language option.
Locale string `json:"locale"`
// Whether the user's email is verified.
Verified bool `json:"verified"`
// The email of the user. This is only present when
// the application possesses the email scope for the user.
Email string `json:"email"`
// User Account Flags
Flags int `json:"flags"`
// Type of Nitro Subscriptions
PremiumType PremiumType `json:"premium_type"`
// Public User Account Flags
PublicFlags UserPublicFlags `json:"public_flags"`
// The token of the user. This is only present for
// the user represented by the current session.
Token string `json:"token"`
}
type PremiumType int
const (
PremiumTypeNone PremiumType = iota
PremiumTypeNitroClassic
PremiumTypeNitro
PremiumTypeNitroBasic
)
type UserPublicFlags int
const (
UserPublicFlagsStaff UserPublicFlags = 1 << iota
UserPublicFlagsPartner
UserPublicFlagsHypeSquad
UserPublicFlagsBugHunterLevel1
UserPublicFlagsHypeSquadBraveryHouse = 1 << (iota + 2) // HYPE_SQUAD_ONLINE_HOUSE_1 // 1 << 6
UserPublicFlagsHypeSquadBrillianceHouse // HYPE_SQUAD_ONLINE_HOUSE_2
UserPublicFlagsHypeSquadBalanceHouse // HYPE_SQUAD_ONLINE_HOUSE_3
UserPublicFlagsPremiumEarlySupporter
UserPublicFlagsTeamPseudoUser
UserPublicFlagsBugHunterLevel2 = 1 << (iota + 5) // 1 << 14
UserPublicFlagsVerifiedBot = 1 << (iota + 6) // 1 << 16
UserPublicFlagsVerifiedDeveloper
UserPublicFlagsCertifiedModerator
UserPublicFlagsBotHTTPInteractions
UserPublicFlagsActiveDeveloper = 1 << (iota + 8) // 1 << 22
)
// String returns a unique identifier of the form username#discriminator
func (u *User) String() string {
if u.Discriminator != "0" {
return u.Username + "#" + u.Discriminator
}
return u.Username
}
func (u *User) GetGlobalName() string {
if u.GlobalName != nil {
return *u.GlobalName
}
return u.String()
}
// Mention return a string which mentions the user
func (u *User) Mention() string {
return "<@" + u.ID + ">"
}
// DefaultAvatarURL returns a URL to the user's default avatar
func (u *User) DefaultAvatarURL() string {
if u.Discriminator == "0" {
id, _ := strconv.Atoi(u.ID)
return EndpointDefaultUserAvatar((id >> 22) % 6)
}
discriminator, _ := strconv.Atoi(u.Discriminator)
return EndpointDefaultUserAvatar(discriminator & 5)
}
// 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) (URL string) {
if u.Avatar == "" {
URL = u.DefaultAvatarURL()
} 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
}
// BannerURL returns a URL to user's banner. If user does not have a banner, empty string will be returned
// size: The size of the user banner as a power of two
// if size is an empty string, no size parameter will
// be added to the URL.
func (u *User) BannerURL(size string) (URL string) {
if u.Banner == nil || *u.Banner == "" {
return ""
}
banner := *u.Banner
if strings.HasPrefix(banner, "a_") {
URL = EndpointUserBannerAnimated(u.ID, banner)
} else {
URL = EndpointUserBanner(u.ID, banner)
}
if size != "" {
return URL + "?size=" + size
}
return
}
var PublicFlagNames = map[UserPublicFlags]string{
UserPublicFlagsStaff: "staff",
UserPublicFlagsPartner: "partner",
UserPublicFlagsHypeSquad: "hype_squad",
UserPublicFlagsBugHunterLevel1: "bug_hunter_level_1",
UserPublicFlagsHypeSquadBraveryHouse: "hype_squad_bravery",
UserPublicFlagsHypeSquadBrillianceHouse: "hype_squad_brilliance",
UserPublicFlagsHypeSquadBalanceHouse: "hype_squad_balance",
UserPublicFlagsPremiumEarlySupporter: "premium_early_supporter",
UserPublicFlagsTeamPseudoUser: "team_pseudo_user",
UserPublicFlagsBugHunterLevel2: "bug_hunter_level_2",
UserPublicFlagsVerifiedBot: "verified_bot",
UserPublicFlagsVerifiedDeveloper: "verified_developer",
UserPublicFlagsCertifiedModerator: "certified_moderator",
UserPublicFlagsBotHTTPInteractions: "bot_http_interactions",
UserPublicFlagsActiveDeveloper: "active_developer",
}
func (u *User) PublicFlagSlice() (res []string) {
if u.PublicFlags == 0 {
return
}
flagCopy := u.PublicFlags
for flag, name := range PublicFlagNames {
if flagCopy&flag == flag {
res = append(res, name)
flagCopy -= flag
if flagCopy == 0 {
break
}
}
}
return
}