/
user.go
268 lines (217 loc) · 6.45 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package discord
import (
"strconv"
"time"
)
type User struct {
ID UserID `json:"id"`
Username string `json:"username"`
Discriminator string `json:"discriminator"` // This is "0" if the user has migrated to the new username system.
Avatar Hash `json:"avatar"`
DisplayName string `json:"global_name"`
// These fields may be omitted
Bot bool `json:"bot,omitempty"`
MFA bool `json:"mfa_enabled,omitempty"`
Nitro UserNitro `json:"premium_type,omitempty"`
Flags UserFlags `json:"flags,omitempty"`
PublicFlags UserFlags `json:"public_flags,omitempty"`
DiscordSystem bool `json:"system,omitempty"`
EmailVerified bool `json:"verified,omitempty"`
Locale string `json:"locale,omitempty"`
Email string `json:"email,omitempty"`
Banner Hash `json:"banner,omitempty"`
Accent Color `json:"accent_color,omitempty"`
}
// CreatedAt returns a time object representing when the user was created.
func (u User) CreatedAt() time.Time {
return u.ID.Time()
}
// Mention returns a mention of the user.
func (u User) Mention() string {
return u.ID.Mention()
}
// Tag returns a tag of the user.
func (u User) Tag() string {
if u.Discriminator == "0" || u.Discriminator == "0000" {
return u.Username
}
return u.Username + "#" + u.Discriminator
}
// DisplayOrUsername returns the DisplayName if it is set, otherwise the
// Username.
func (u User) DisplayOrUsername() string {
if u.DisplayName != "" {
return u.DisplayName
}
return u.Username
}
// DisplayOrTag returns the DisplayName if it is set, otherwise the Tag.
func (u User) DisplayOrTag() string {
if u.DisplayName != "" {
return u.DisplayName
}
return u.Tag()
}
// AvatarURL returns the URL of the Avatar Image. It automatically detects a
// suitable type.
func (u User) AvatarURL() string {
return u.AvatarURLWithType(AutoImage)
}
// AvatarURLWithType returns the URL of the Avatar Image using the passed type.
// If the user has no Avatar, his default avatar will be returned. This
// requires ImageType Auto or PNG
//
// Supported Image Types: PNG, JPEG, WebP, GIF (read above for caveat)
func (u User) AvatarURLWithType(t ImageType) string {
if u.Avatar == "" {
if t != PNGImage && t != AutoImage {
return ""
}
var picNo string
if u.Discriminator != "0" {
disc, err := strconv.Atoi(u.Discriminator)
if err != nil { // this should never happen
return ""
}
picNo = strconv.Itoa(disc % 5)
} else {
picNo = strconv.FormatUint(uint64(u.ID>>22)%6, 10)
}
return "https://cdn.discordapp.com/embed/avatars/" + picNo + ".png"
}
return "https://cdn.discordapp.com/avatars/" + u.ID.String() + "/" + t.format(u.Avatar)
}
// BannerURL returns the URL of the Banner Image. It automatically detects a
// suitable type.
func (u User) BannerURL() string {
return u.BannerURLWithType(AutoImage)
}
// BannerURLWithType returns the URL of the Banner Image using the passed type.
// If the user has no Banner, an empty string will be returned.
func (u User) BannerURLWithType(t ImageType) string {
if u.Banner == "" {
return ""
}
return "https://cdn.discordapp.com/banners/" + u.ID.String() + "/" + t.format(u.Banner)
}
type UserFlags uint32
const NoFlag UserFlags = 0
const (
Employee UserFlags = 1 << iota
Partner
HypeSquadEvents
BugHunterLvl1
_
_
HouseBravery
HouseBrilliance
HouseBalance
EarlySupporter
TeamUser
_
System
_
BugHunterLvl2
_
VerifiedBot
VerifiedBotDeveloper
CertifiedModerator
LikelySpammer // undocumented
)
type UserNitro uint8
const (
NoUserNitro UserNitro = iota
NitroClassic
NitroFull
NitroBasic
)
type Connection struct {
ID string `json:"id"`
Name string `json:"name"`
Type Service `json:"type"`
Revoked bool `json:"revoked"`
Verified bool `json:"verified"`
FriendSync bool `json:"friend_sync"`
ShowActivity bool `json:"show_activity"`
Visibility ConnectionVisibility `json:"visibility"`
// Only partial
Integrations []Integration `json:"integrations"`
}
type ConnectionVisibility uint8
const (
ConnectionNotVisible ConnectionVisibility = iota
ConnectionVisibleEveryone
)
type Activity struct {
Name string `json:"name"`
URL URL `json:"url,omitempty"`
Type ActivityType `json:"type"`
// User only
Instance bool `json:"instance,omitempty"`
Flags ActivityFlags `json:"flags,omitempty"`
CreatedAt UnixTimestamp `json:"created_at,omitempty"`
Timestamps *ActivityTimestamps `json:"timestamps,omitempty"`
AppID AppID `json:"application_id,omitempty"`
Details string `json:"details,omitempty"`
State string `json:"state,omitempty"` // party status
Emoji *Emoji `json:"emoji,omitempty"`
Party *ActivityParty `json:"party,omitempty"`
Assets *ActivityAssets `json:"assets,omitempty"`
Secrets *ActivitySecrets `json:"secrets,omitempty"`
// Undocumented fields
SyncID string `json:"sync_id,omitempty"`
SessionID string `json:"session_id,omitempty"`
}
type ActivityType uint8
const (
GameActivity ActivityType = iota // Playing $name
StreamingActivity // Streaming $details
ListeningActivity // Listening to $name
WatchingActivity // Watching $name
CustomActivity // $emoji $state
CompetingActivity // Competing in $name
)
type ActivityFlags uint32
const (
InstanceActivity ActivityFlags = 1 << iota
JoinActivity
SpectateActivity
JoinRequestActivity
SyncActivity
PlayActivity
)
type ActivityTimestamps struct {
Start UnixMsTimestamp `json:"start,omitempty"`
End UnixMsTimestamp `json:"end,omitempty"`
}
type ActivityParty struct {
ID string `json:"id,omitempty"`
Size [2]int `json:"size,omitempty"` // [ current, max ]
}
type ActivityAssets struct {
LargeImage string `json:"large_image,omitempty"` // id
LargeText string `json:"large_text,omitempty"`
SmallImage string `json:"small_image,omitempty"` // id
SmallText string `json:"small_text,omitempty"`
}
type ActivitySecrets struct {
Join string `json:"join,omitempty"`
Spectate string `json:"spectate,omitempty"`
Match string `json:"match,omitempty"`
}
// A Relationship between the logged in user and the user in the struct. This
// struct is undocumented.
type Relationship struct {
UserID UserID `json:"id"`
User User `json:"user"`
Type RelationshipType `json:"type"`
}
// RelationshipType is an enum for a relationship.
type RelationshipType uint8
const (
_ RelationshipType = iota
FriendRelationship
BlockedRelationship
IncomingFriendRequest
SentFriendRequest
)