forked from andersfylling/disgord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
struct_user.go
496 lines (395 loc) · 12.2 KB
/
struct_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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
package disgord
import (
"encoding/json"
"errors"
"sync"
)
const (
// StatusIdle presence status for idle
StatusIdle = "idle"
// StatusDnd presence status for dnd
StatusDnd = "dnd"
// StatusOnline presence status for online
StatusOnline = "online"
// StatusOffline presence status for offline
StatusOffline = "offline"
)
const (
ActivityFlagInstance = 1 << 0
ActivityFlagJoin = 1 << 1
ActivityFlagSpectate = 1 << 2
ActivityFlagJoinRequest = 1 << 3
ActivityFlagSync = 1 << 4
ActivityFlagPlay = 1 << 5
)
type UserInterface interface {
Mention() string
MentionNickname() string
String() string
}
type ActivityParty struct {
sync.RWMutex `json:"-"`
ID string `json:"id,omitempty"` // the id of the party
Size []int `json:"size,omitempty"` // used to show the party's current and maximum size
}
// Limit shows the maximum number of guests/people allowed
func (ap *ActivityParty) Limit() int {
if len(ap.Size) != 2 {
return 0
}
return ap.Size[1]
}
// NumberOfPeople shows the current number of people attending the Party
func (ap *ActivityParty) NumberOfPeople() int {
if len(ap.Size) != 1 {
return 0
}
return ap.Size[0]
}
func (a *ActivityParty) DeepCopy() (copy interface{}) {
copy = &ActivityParty{}
a.CopyOverTo(copy)
return
}
func (a *ActivityParty) CopyOverTo(other interface{}) (err error) {
var ok bool
var activity *ActivityParty
if activity, ok = other.(*ActivityParty); !ok {
err = NewErrorUnsupportedType("given interface{} was not of type *ActivityParty")
return
}
a.RLock()
activity.Lock()
activity.ID = a.ID
activity.Size = a.Size
a.RUnlock()
activity.Unlock()
return
}
type ActivityAssets struct {
sync.RWMutex `json:"-"`
LargeImage string `json:"large_image,omitempty"` // the id for a large asset of the activity, usually a snowflake
LargeText string `json:"large_text,omitempty"` //text displayed when hovering over the large image of the activity
SmallImage string `json:"small_image,omitempty"` // the id for a small asset of the activity, usually a snowflake
SmallText string `json:"small_text,omitempty"` // text displayed when hovering over the small image of the activity
}
func (a *ActivityAssets) DeepCopy() (copy interface{}) {
copy = &ActivityAssets{}
a.CopyOverTo(copy)
return
}
func (a *ActivityAssets) CopyOverTo(other interface{}) (err error) {
var ok bool
var activity *ActivityAssets
if activity, ok = other.(*ActivityAssets); !ok {
err = NewErrorUnsupportedType("given interface{} was not of type *ActivityAssets")
return
}
a.RLock()
activity.Lock()
activity.LargeImage = a.LargeImage
activity.LargeText = a.LargeText
activity.SmallImage = a.SmallImage
activity.SmallText = a.SmallText
a.RUnlock()
activity.Unlock()
return
}
type ActivitySecrets struct {
sync.RWMutex `json:"-"`
Join string `json:"join,omitempty"` // the secret for joining a party
Spectate string `json:"spectate,omitempty"` // the secret for spectating a game
Match string `json:"match,omitempty"` // the secret for a specific instanced match
}
func (a *ActivitySecrets) DeepCopy() (copy interface{}) {
copy = &ActivitySecrets{}
a.CopyOverTo(copy)
return
}
func (a *ActivitySecrets) CopyOverTo(other interface{}) (err error) {
var ok bool
var activity *ActivitySecrets
if activity, ok = other.(*ActivitySecrets); !ok {
err = NewErrorUnsupportedType("given interface{} was not of type *ActivitySecrets")
return
}
a.RLock()
activity.Lock()
activity.Join = a.Join
activity.Spectate = a.Spectate
activity.Match = a.Match
a.RUnlock()
activity.Unlock()
return
}
type ActivityTimestamp struct {
sync.RWMutex `json:"-"`
Start int `json:"start,omitempty"` // unix time (in milliseconds) of when the activity started
End int `json:"end,omitempty"` // unix time (in milliseconds) of when the activity ends
}
func (a *ActivityTimestamp) DeepCopy() (copy interface{}) {
copy = &ActivityTimestamp{}
a.CopyOverTo(copy)
return
}
func (a *ActivityTimestamp) CopyOverTo(other interface{}) (err error) {
var ok bool
var activity *ActivityTimestamp
if activity, ok = other.(*ActivityTimestamp); !ok {
err = NewErrorUnsupportedType("given interface{} was not of type *ActivityTimestamp")
return
}
a.RLock()
activity.Lock()
activity.Start = a.Start
activity.End = a.End
a.RUnlock()
activity.Unlock()
return
}
func NewActivity() (activity *Activity) {
return &Activity{
Timestamps: []*ActivityTimestamp{},
}
}
// Activity https://discordapp.com/developers/docs/topics/gateway#activity-object-activity-structure
type Activity struct {
sync.RWMutex `json:"-"`
Name string `json:"name"` // the activity's name
Type int `json:"type"` // activity type
URL *string `json:"url,omitempty"` //stream url, is validated when type is 1
Timestamps []*ActivityTimestamp `json:"timestamps,omitempty"` // timestamps object unix timestamps for start and/or end of the game
ApplicationID Snowflake `json:"application_id,omitempty"` //? snowflake application id for the game
Details *string `json:"details,omitempty"` //? ?string what the player is currently doing
State *string `json:"state,omitempty"` //state? ?string the user's current party status
Party *ActivityParty `json:"party"` //party? party object information for the current party of the player
Assets *ActivityAssets `json:"assets,omitempty"` // assets? assets object images for the presence and their hover texts
Secrets *ActivitySecrets `json:"secrets,omitempty"` // secrets? secrets object secrets for Rich Presence joining and spectating
Instance bool `json:"instance,omitempty"` // instance? boolean whether or not the activity is an instanced game session
Flags int `json:"flags,omitempty"` // flags? int activity flags ORd together, describes what the payload includes
}
func (a *Activity) DeepCopy() (copy interface{}) {
copy = &Activity{}
a.CopyOverTo(copy)
return
}
func (a *Activity) CopyOverTo(other interface{}) (err error) {
var ok bool
var activity *Activity
if activity, ok = other.(*Activity); !ok {
err = NewErrorUnsupportedType("given interface{} was not of type *Activity")
return
}
a.RLock()
activity.Lock()
activity.Name = a.Name
activity.Type = a.Type
activity.ApplicationID = a.ApplicationID
activity.Instance = a.Instance
activity.Flags = a.Flags
if a.URL != nil {
url := *a.URL
activity.URL = &url
}
if a.Timestamps != nil {
if activity.Timestamps == nil {
activity.Timestamps = make([]*ActivityTimestamp, len(a.Timestamps))
}
for i, timestampP := range a.Timestamps {
if timestampP == nil {
continue
}
activity.Timestamps[i] = timestampP.DeepCopy().(*ActivityTimestamp)
}
}
if a.Details != nil {
details := *a.Details
activity.Details = &details
}
if a.State != nil {
state := *a.State
activity.State = &state
}
if a.Party != nil {
activity.Party = a.Party.DeepCopy().(*ActivityParty)
}
if a.Assets != nil {
activity.Assets = a.Assets.DeepCopy().(*ActivityAssets)
}
if a.Secrets != nil {
activity.Secrets = a.Secrets.DeepCopy().(*ActivitySecrets)
}
return
}
// ---------
func NewUser() *User {
return &User{}
}
type User struct {
sync.RWMutex `json:"-"`
ID Snowflake `json:"id,omitempty"`
Username string `json:"username,omitempty"`
Discriminator string `json:"discriminator,omitempty"`
Email string `json:"email,omitempty"`
Avatar *string `json:"avatar"` // data:image/jpeg;base64,BASE64_ENCODED_JPEG_IMAGE_DATA //TODO: pointer?
Token string `json:"token,omitempty"`
Verified bool `json:"verified,omitempty"`
MFAEnabled bool `json:"mfa_enabled,omitempty"`
Bot bool `json:"bot,omitempty"`
}
func (u *User) Mention() string {
return "<@" + u.ID.String() + ">"
}
func (u *User) MentionNickname() string {
return "<@!" + u.ID.String() + ">"
}
func (u *User) String() string {
return u.Username + "#" + u.Discriminator + "{" + u.ID.String() + "}"
}
// Partial check if this is not a complete user object
// Assumption: has a snowflake.
func (u *User) Partial() bool {
return (u.Username + u.Discriminator) == ""
}
func (u *User) MarshalJSON() ([]byte, error) {
if u.ID.Empty() {
return []byte("{}"), nil
}
return json.Marshal(User(*u))
}
// func (u *User) UnmarshalJSON(data []byte) error {
// return json.Unmarshal(data, &u.userJSON)
// }
//func (u *User) Clear() {
// //u.d.Avatar = nil
//}
func (u *User) SendMsg(session Session, message *Message) (channel *Channel, msg *Message, err error) {
channel, err = session.CreateDM(u.ID)
if err != nil {
return
}
msg, err = session.SendMsg(channel.ID, message)
return
}
func (u *User) SendMsgString(session Session, content string) (channel *Channel, msg *Message, err error) {
channel, msg, err = u.SendMsg(session, &Message{
Content: content,
})
return
}
func (u *User) DeepCopy() (copy interface{}) {
copy = NewUser()
u.CopyOverTo(copy)
return
}
func (u *User) CopyOverTo(other interface{}) (err error) {
var user *User
var valid bool
if user, valid = other.(*User); !valid {
err = NewErrorUnsupportedType("argument given is not a *User type")
return
}
u.RLock()
user.Lock()
user.ID = u.ID
user.Username = u.Username
user.Discriminator = u.Discriminator
user.Email = u.Email
user.Token = u.Token
user.Verified = u.Verified
user.MFAEnabled = u.MFAEnabled
user.Bot = u.Bot
if u.Avatar != nil {
avatar := *u.Avatar
user.Avatar = &avatar
}
u.RUnlock()
user.Unlock()
return
}
func (u *User) saveToDiscord(session Session) (err error) {
// TODO: check snowflake if ID is current user
// call both modify methods
return errors.New("not implemented")
}
func (u *User) Valid() bool {
return u.ID > 0
}
// -------
func NewUserPresence() *UserPresence {
return &UserPresence{
Roles: []Snowflake{},
}
}
// UserPresence presence info for a guild member or friend/user in a DM
type UserPresence struct {
sync.RWMutex `json:"-"`
User *User `json:"user"`
Roles []Snowflake `json:"roles"`
Game *Activity `json:"activity"`
GuildID Snowflake `json:"guild_id"`
Nick string `json:"nick"`
Status string `json:"status"`
}
func (p *UserPresence) String() string {
return p.Status
}
func (p *UserPresence) DeepCopy() (copy interface{}) {
copy = NewUserPresence()
p.CopyOverTo(copy)
return
}
func (p *UserPresence) CopyOverTo(other interface{}) (err error) {
var ok bool
var presence *UserPresence
if presence, ok = other.(*UserPresence); !ok {
err = NewErrorUnsupportedType("given interface{} was not of type *UserPresence")
return
}
p.RLock()
presence.Lock()
presence.User = p.User.DeepCopy().(*User)
presence.Roles = p.Roles
presence.Game = p.Game.DeepCopy().(*Activity)
presence.GuildID = p.GuildID
presence.Nick = p.Nick
presence.Status = p.Status
p.RUnlock()
presence.Unlock()
return
}
// UserConnection ...
type UserConnection struct {
sync.RWMutex `json:"-"`
ID string `json:"id"` // id of the connection account
Name string `json:"name"` // the username of the connection account
Type string `json:"type"` // the service of the connection (twitch, youtube)
Revoked bool `json:"revoked"` // whether the connection is revoked
Integrations []*IntegrationAccount `json:"integrations"` // an array of partial server integrations
}
func (c *UserConnection) DeepCopy() (copy interface{}) {
copy = &UserConnection{}
c.CopyOverTo(copy)
return
}
func (c *UserConnection) CopyOverTo(other interface{}) (err error) {
var ok bool
var con *UserConnection
if con, ok = other.(*UserConnection); !ok {
err = NewErrorUnsupportedType("given interface{} was not of type *UserConnection")
return
}
c.RLock()
con.Lock()
con.ID = c.ID
con.Name = c.Name
con.Type = c.Type
con.Revoked = c.Revoked
con.Integrations = make([]*IntegrationAccount, len(c.Integrations))
for i, account := range c.Integrations {
con.Integrations[i] = account.DeepCopy().(*IntegrationAccount)
}
c.RUnlock()
con.Unlock()
return
}