forked from andersfylling/disgord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
struct_guild.go
922 lines (781 loc) · 21.7 KB
/
struct_guild.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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
package disgord
import (
"encoding/json"
"errors"
"sort"
"sync"
)
// consts inspired by: https://github.com/bwmarrin/discordgo/blob/master/structs.go
// Constants for the different bit offsets of text channel permissions
const (
ReadMessagesPermission = 1 << (iota + 10)
SendMessagesPermission
SendTTSMessagesPermission
ManageMessagesPermission
EmbedLinksPermission
AttachFilesPermission
ReadMessageHistoryPermission
MentionEveryonePermission
UseExternalEmojisPermission
)
// Constants for the different bit offsets of voice permissions
const (
VoiceConnectPermission = 1 << (iota + 20)
VoiceSpeakPermission
VoiceMuteMembersPermission
VoiceDeafenMembersPermission
VoiceMoveMembersPermission
VoiceUseVADPermission
)
// Constants for general management.
const (
ChangeNicknamePermission = 1 << (iota + 26)
ManageNicknamesPermission
ManageRolesPermission
ManageWebhooksPermission
ManageEmojisPermission
)
// Constants for the different bit offsets of general permissions
const (
CreateInstantInvitePermission = 1 << iota
KickMembersPermission
BanMembersPermission
AdministratorPermission
ManageChannelsPermission
ManageServerPermission
AddReactionsPermission
ViewAuditLogsPermission
AllTextPermission = ReadMessagesPermission |
SendMessagesPermission |
SendTTSMessagesPermission |
ManageMessagesPermission |
EmbedLinksPermission |
AttachFilesPermission |
ReadMessageHistoryPermission |
MentionEveryonePermission
AllVoicePermission = VoiceConnectPermission |
VoiceSpeakPermission |
VoiceMuteMembersPermission |
VoiceDeafenMembersPermission |
VoiceMoveMembersPermission |
VoiceUseVADPermission
AllChannelPermission = AllTextPermission |
AllVoicePermission |
CreateInstantInvitePermission |
ManageRolesPermission |
ManageChannelsPermission |
AddReactionsPermission |
ViewAuditLogsPermission
AllPermission = AllChannelPermission |
KickMembersPermission |
BanMembersPermission |
ManageServerPermission |
AdministratorPermission
)
func NewGuild() *Guild {
return &Guild{
Roles: []*Role{},
Emojis: []*Emoji{},
Features: []string{},
VoiceStates: []*VoiceState{},
Members: []*Member{},
Channels: []*Channel{},
Presences: []*UserPresence{},
}
}
func NewGuildFromJSON(data []byte) (guild *Guild) {
guild = NewGuild()
err := json.Unmarshal(data, guild)
if err != nil {
panic(err)
}
return guild
}
func NewPartialGuild(ID Snowflake) (guild *Guild) {
guild = NewGuild()
guild.ID = ID
guild.Unavailable = true
return
}
func NewGuildFromUnavailable(gu *GuildUnavailable) *Guild {
return NewPartialGuild(gu.ID)
}
func NewGuildUnavailable(ID Snowflake) *GuildUnavailable {
gu := &GuildUnavailable{
ID: ID,
Unavailable: true,
}
return gu
}
type GuildUnavailable struct {
ID Snowflake `json:"id"`
Unavailable bool `json:"unavailable"` // ?*|
sync.RWMutex `json:"-"`
}
type GuildInterface interface {
Channel(ID Snowflake)
}
// if loading is deactivated, then check state, then do a request.
// if loading is activated, check state only.
// type Members interface {
// Member(userID snowflake.Snowflake) *Member
// MembersWithName( /*username*/ name string) map[snowflake.Snowflake]*Member
// MemberByUsername( /*username#discriminator*/ username string) *Member
// MemberByAlias(alias string) *Member
// EverythingInMemory() bool
// }
// Guild Guilds in Discord represent an isolated collection of users and channels,
// and are often referred to as "servers" in the UI.
// https://discordapp.com/developers/docs/resources/guild#guild-object
// Fields with `*` are only sent within the GUILD_CREATE event
// TODO: lazyload everything
// reviewed: 2018-08-25
type PartialGuild = Guild
type Guild struct {
sync.RWMutex `json:"-"`
ID Snowflake `json:"id"`
ApplicationID *Snowflake `json:"application_id"` // |?
Name string `json:"name"`
Icon *string `json:"icon"` // |?, icon hash
Splash *string `json:"splash"` // |?, image hash
Owner bool `json:"owner,omitempty"` // ?|
OwnerID Snowflake `json:"owner_id"`
Permissions uint64 `json:"permissions,omitempty"` // ?|, permission flags for connected user `/users/@me/guilds`
Region string `json:"region"`
AfkChannelID *Snowflake `json:"afk_channel_id"` // |?
AfkTimeout uint `json:"afk_timeout"`
EmbedEnabled bool `json:"embed_enabled,omit_empty"`
EmbedChannelID Snowflake `json:"embed_channel_id,omit_empty"`
VerificationLevel VerificationLvl `json:"verification_level"`
DefaultMessageNotifications DefaultMessageNotificationLvl `json:"default_message_notifications"`
ExplicitContentFilter ExplicitContentFilterLvl `json:"explicit_content_filter"`
Roles []*Role `json:"roles"`
Emojis []*Emoji `json:"emojis"`
Features []string `json:"features"`
MFALevel MFALvl `json:"mfa_level"`
WidgetEnabled bool `json:"widget_enabled,omit_empty"` // |
WidgetChannelID Snowflake `json:"widget_channel_id,omit_empty"` // |
SystemChannelID *Snowflake `json:"system_channel_id,omitempty"` // |?
// JoinedAt must be a pointer, as we can't hide non-nil structs
JoinedAt *Timestamp `json:"joined_at,omitempty"` // ?*|
Large bool `json:"large,omitempty"` // ?*|
Unavailable bool `json:"unavailable"` // ?*| omitempty?
MemberCount uint `json:"member_count,omitempty"` // ?*|
VoiceStates []*VoiceState `json:"voice_states,omitempty"` // ?*|
Members []*Member `json:"members,omitempty"` // ?*|
Channels []*Channel `json:"channels,omitempty"` // ?*|
Presences []*UserPresence `json:"presences,omitempty"` // ?*|
//highestSnowflakeAmoungMembers Snowflake
}
func (g *Guild) GetMemberWithHighestSnowflake() *Member {
g.RLock()
defer g.RUnlock()
if len(g.Members) == 0 {
return nil
}
highest := g.Members[0]
for _, member := range g.Members {
if member.User.ID > highest.User.ID {
highest = member
}
}
return highest
}
//func (g *Guild) EverythingInMemory() bool {
// return false
//}
// Compare two guild objects
// TODO: remove?
//func (g *Guild) Compare(other *Guild) bool {
// // TODO: this is shit..
// return (g == nil && other == nil) || (other != nil && g.ID == other.ID)
//}
// func (g *Guild) UnmarshalJSON(data []byte) (err error) {
// return json.Unmarshal(data, &g.guildJSON)
// }
// TODO: fix copying of mutex lock
func (g *Guild) MarshalJSON() ([]byte, error) {
g.Lock()
defer g.Unlock()
// TODO: check for dead locks
var jsonData []byte
var err error
if g.Unavailable {
guildUnavailable := NewGuildUnavailable(g.ID)
jsonData, err = json.Marshal(guildUnavailable)
if err != nil {
return []byte(""), nil
}
} else {
jsonData, err = json.Marshal(Guild(*g))
if err != nil {
return []byte(""), nil
}
}
return jsonData, nil
}
// sortChannels Only while in lock
func (g *Guild) sortChannels() {
sort.Slice(g.Channels, func(i, j int) bool {
return g.Channels[i].ID < g.Channels[j].ID
})
}
func (g *Guild) AddChannel(c *Channel) error {
g.Lock()
defer g.Unlock()
g.Channels = append(g.Channels, c)
g.sortChannels()
return nil
}
func (g *Guild) DeleteChannel(c *Channel) error {
return g.DeleteChannelByID(c.ID)
}
func (g *Guild) DeleteChannelByID(ID Snowflake) error {
g.Lock()
defer g.Unlock()
index := -1
for i, c := range g.Channels {
if c.ID == ID {
index = i
}
}
if index == -1 {
return errors.New("channel with Snowflake{" + ID.String() + "} does not exist in cache")
}
// delete the entry
g.Channels[index] = g.Channels[len(g.Channels)-1]
g.Channels[len(g.Channels)-1] = nil
g.Channels = g.Channels[:len(g.Channels)-1]
g.sortChannels()
return nil
}
func (g *Guild) addMember(member *Member) error {
if member == nil {
return errors.New("member was nil")
}
// TODO: implement sorting for faster searching later
g.Members = append(g.Members, member)
return nil
}
func (g *Guild) AddMembers(members []*Member) {
g.Lock()
defer g.Unlock()
for _, member := range members {
g.addMember(member)
}
}
func (g *Guild) AddMember(member *Member) error {
g.Lock()
defer g.Unlock()
return g.addMember(member)
}
func (g *Guild) LoadAllMembers(session Session) (err error) {
g.Lock()
defer g.Unlock()
// TODO-1: check cache
// TODO-2: what if members have already been loaded? use Guild.MembersCount?
var lastCount = 1000
var failsafe bool
// TODO-3: failsafe is set when the number of users returned is less
// than 1,000 two times
highestSnowflake := NewSnowflake(0)
for {
if lastCount == 0 || failsafe {
break
}
var members []*Member
members, err = session.GetGuildMembers(g.ID, highestSnowflake, 1000)
if err != nil {
return
}
for _, member := range members {
g.addMember(member)
s := member.User.ID
if s > highestSnowflake {
highestSnowflake = s
}
}
lastCount = len(members)
}
return nil
}
func (g *Guild) AddRole(role *Role) error {
g.Lock()
defer g.Unlock()
// TODO: implement sorting for faster searching later
role.guildID = g.ID
g.Roles = append(g.Roles, role)
return nil
}
// Member return a member by his/her userid
func (g *Guild) Member(id Snowflake) (*Member, error) {
g.RLock()
defer g.RUnlock()
for _, member := range g.Members {
if member.User.ID == id {
return member, nil
}
}
return nil, errors.New("member not found in guild")
}
// MembersByName retrieve a slice of members with same username or nickname
func (g *Guild) MembersByName(name string) (members []*Member) {
g.RLock()
defer g.RUnlock()
for _, member := range g.Members {
if member.Nick == name || member.User.Username == name {
members = append(members, member)
}
}
return
}
// Role retrieve a role based on role id
func (g *Guild) Role(id Snowflake) (role *Role, err error) {
g.RLock()
defer g.RUnlock()
for _, role = range g.Roles {
if role.ID == id {
return
}
}
err = errors.New("role not found in guild")
return
}
// TODO
//func (g *Guild) UpdateRole(r *Role) {
// for _, role := range g.Roles {
// if role.ID == r.ID {
// *role = *r
// break
// }
// }
//}
// DeleteRoleByID remove a role from the guild struct
func (g *Guild) DeleteRoleByID(ID Snowflake) {
g.Lock()
defer g.Unlock()
index := -1
for i, r := range g.Roles {
if r.ID == ID {
index = i
break
}
}
if index != -1 {
// delete the entry
g.Roles[index] = g.Roles[len(g.Roles)-1]
g.Roles[len(g.Roles)-1] = nil
g.Roles = g.Roles[:len(g.Roles)-1]
}
}
// RoleByTitle retrieves a slice of roles with same name
func (g *Guild) RoleByName(name string) ([]*Role, error) {
g.RLock()
defer g.RUnlock()
var roles []*Role
for _, role := range g.Roles {
if role.Name == name {
roles = append(roles, role)
}
}
if len(roles) == 0 {
return nil, errors.New("no roles were found in guild")
}
return roles, nil
}
func (g *Guild) Channel(id Snowflake) (*Channel, error) {
g.RLock()
defer g.RUnlock()
for _, channel := range g.Channels {
if channel.ID == id {
return channel, nil
}
}
return nil, errors.New("channel not found in guild")
}
// TODO
// func (g *Guild) UpdatePresence(p *UserPresence) {
// g.RLock()
// index := -1
// for i, presence := range g.Presences {
// if presence.User.ID == p.User.ID {
// index = i
// break
// }
// }
// g.RUnlock()
//
// if index != -1 {
// // update
// return
// }
//
// // otherwise add
// g.Lock()
// g.Presences = append(g.Presences, p) // TODO: update the user pointer?
// g.Unlock()
// }
// Clear all the pointers
// func (g *Guild) Clear() {
// g.Lock() // what if another process tries to read this, but awais while locked for clearing?
// defer g.Unlock()
//
// //g.Icon = nil // should this be cleared?
// //g.Splash = nil // should this be cleared?
//
// for _, r := range g.Roles {
// r.Clear()
// r = nil
// }
// g.Roles = nil
//
// for _, e := range g.Emojis {
// e.Clear()
// e = nil
// }
// g.Emojis = nil
//
// for _, vst := range g.VoiceStates {
// vst.Clear()
// vst = nil
// }
// g.VoiceStates = nil
//
// var deletedUsers []Snowflake
// for _, m := range g.Members {
// deletedUsers = append(deletedUsers, m.Clear())
// m = nil
// }
// g.Members = nil
//
// for _, c := range g.Channels {
// c.Clear()
// c = nil
// }
// g.Channels = nil
//
// for _, p := range g.Presences {
// p.Clear()
// p = nil
// }
// g.Presences = nil
//
// }
func (g *Guild) DeepCopy() (copy interface{}) {
copy = NewGuild()
g.CopyOverTo(copy)
return
}
func (g *Guild) CopyOverTo(other interface{}) (err error) {
var guild *Guild
var valid bool
if guild, valid = other.(*Guild); !valid {
err = NewErrorUnsupportedType("argument given is not a *Guild type")
return
}
g.RLock()
guild.Lock()
guild.ID = g.ID
guild.Name = g.Name
guild.Owner = g.Owner
guild.OwnerID = g.OwnerID
guild.Permissions = g.Permissions
guild.Region = g.Region
guild.AfkTimeout = g.AfkTimeout
guild.EmbedEnabled = g.EmbedEnabled
guild.EmbedChannelID = g.EmbedChannelID
guild.VerificationLevel = g.VerificationLevel
guild.DefaultMessageNotifications = g.DefaultMessageNotifications
guild.ExplicitContentFilter = g.ExplicitContentFilter
guild.Features = g.Features
guild.MFALevel = g.MFALevel
guild.WidgetEnabled = g.WidgetEnabled
guild.WidgetChannelID = g.WidgetChannelID
guild.SystemChannelID = g.SystemChannelID
guild.Large = g.Large
guild.Unavailable = g.Unavailable
guild.MemberCount = g.MemberCount
// pointers
if g.ApplicationID != nil {
id := *g.ApplicationID
guild.ApplicationID = &id
}
if g.Splash != nil {
splash := *g.Splash
guild.Splash = &splash
}
if g.Icon != nil {
icon := *g.Icon
guild.Icon = &icon
}
if g.AfkChannelID != nil {
channel := *g.AfkChannelID
guild.AfkChannelID = &channel
}
if g.SystemChannelID != nil {
channel := *g.SystemChannelID
guild.SystemChannelID = &channel
}
if g.JoinedAt != nil {
joined := *g.JoinedAt
guild.JoinedAt = &joined
}
for _, roleP := range g.Roles {
if roleP == nil {
continue
}
guild.Roles = append(guild.Roles, roleP.DeepCopy().(*Role))
}
for _, emojiP := range g.Emojis {
if emojiP == nil {
continue
}
guild.Emojis = append(guild.Emojis, emojiP.DeepCopy().(*Emoji))
}
for _, vsP := range g.VoiceStates {
if vsP == nil {
continue
}
guild.VoiceStates = append(guild.VoiceStates, vsP.DeepCopy().(*VoiceState))
}
for _, memberP := range g.Members {
if memberP == nil {
continue
}
guild.Members = append(guild.Members, memberP.DeepCopy().(*Member))
}
for _, channelP := range g.Channels {
if channelP == nil {
continue
}
guild.Channels = append(guild.Channels, channelP.DeepCopy().(*Channel))
}
for _, presenceP := range g.Presences {
if presenceP == nil {
continue
}
guild.Presences = append(guild.Presences, presenceP.DeepCopy().(*UserPresence))
}
g.RUnlock()
guild.Unlock()
return
}
// saveToDiscord creates a new Guild if ID is empty or updates an existing one
func (g *Guild) saveToDiscord(session Session) (err error) {
return errors.New("not implemented")
}
func (g *Guild) deleteFromDiscord(session Session) (err error) {
return errors.New("not implemented")
}
// --------------
// Ban https://discordapp.com/developers/docs/resources/guild#ban-object
type Ban struct {
sync.RWMutex `json:"-"`
Reason string `json:"reason"`
User *User `json:"user"`
}
func (b *Ban) DeepCopy() (copy interface{}) {
copy = &Ban{}
b.CopyOverTo(copy)
return
}
func (b *Ban) CopyOverTo(other interface{}) (err error) {
var ok bool
var ban *Ban
if ban, ok = other.(*Ban); !ok {
err = NewErrorUnsupportedType("given interface{} was not of type *Ban")
return
}
b.RLock()
ban.Lock()
ban.Reason = b.Reason
if b.User != nil {
ban.User = b.User.DeepCopy().(*User)
}
b.RUnlock()
ban.Unlock()
return
}
// ------------
// GuildEmbed https://discordapp.com/developers/docs/resources/guild#guild-embed-object
type GuildEmbed struct {
sync.RWMutex `json:"-"`
Enabled bool `json:"enabled"`
ChannelID Snowflake `json:"channel_id"`
}
func (e *GuildEmbed) DeepCopy() (copy interface{}) {
copy = &GuildEmbed{}
e.CopyOverTo(copy)
return
}
func (e *GuildEmbed) CopyOverTo(other interface{}) (err error) {
var ok bool
var embed *GuildEmbed
if embed, ok = other.(*GuildEmbed); !ok {
err = NewErrorUnsupportedType("given interface{} was not of type *GuildEmbed")
return
}
e.RLock()
embed.Lock()
embed.Enabled = e.Enabled
embed.ChannelID = e.ChannelID
e.RUnlock()
embed.Unlock()
return
}
// -------
// Integration https://discordapp.com/developers/docs/resources/guild#integration-object
type Integration struct {
sync.RWMutex `json:"-"`
ID Snowflake `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
Enabled bool `json:"enabled"`
Syncing bool `json:"syncing"`
RoleID Snowflake `json:"role_id"`
ExpireBehavior int `json:"expire_behavior"`
ExpireGracePeriod int `json:"expire_grace_period"`
User *User `json:"user"`
Account *IntegrationAccount `json:"account"`
}
func (i *Integration) DeepCopy() (copy interface{}) {
copy = &Integration{}
i.CopyOverTo(copy)
return
}
func (i *Integration) CopyOverTo(other interface{}) (err error) {
var ok bool
var integration *Integration
if integration, ok = other.(*Integration); !ok {
err = NewErrorUnsupportedType("given interface{} was not of type *Integration")
return
}
i.RLock()
integration.Lock()
integration.ID = i.ID
integration.Name = i.Name
integration.Type = i.Type
integration.Enabled = i.Enabled
integration.Syncing = i.Syncing
integration.RoleID = i.RoleID
integration.ExpireBehavior = i.ExpireBehavior
integration.ExpireGracePeriod = i.ExpireGracePeriod
if i.User != nil {
integration.User = i.User.DeepCopy().(*User)
}
if i.Account != nil {
integration.Account = i.Account.DeepCopy().(*IntegrationAccount)
}
i.RUnlock()
integration.Unlock()
return
}
// IntegrationAccount https://discordapp.com/developers/docs/resources/guild#integration-account-object
type IntegrationAccount struct {
sync.RWMutex `json:"-"`
ID string `json:"id"` // id of the account
Name string `json:"name"` // name of the account
}
func (i *IntegrationAccount) DeepCopy() (copy interface{}) {
copy = &IntegrationAccount{}
i.CopyOverTo(copy)
return
}
func (i *IntegrationAccount) CopyOverTo(other interface{}) (err error) {
var ok bool
var account *IntegrationAccount
if account, ok = other.(*IntegrationAccount); !ok {
err = NewErrorUnsupportedType("given interface{} was not of type *IntegrationAccount")
return
}
i.RLock()
account.Lock()
account.ID = i.ID
account.Name = i.Name
i.RUnlock()
account.Unlock()
return
}
// -------
// Member https://discordapp.com/developers/docs/resources/guild#guild-member-object
type Member struct {
sync.RWMutex `json:"-"`
GuildID Snowflake `json:"guild_id,omitempty"`
User *User `json:"user"`
Nick string `json:"nick,omitempty"` // ?|
Roles []Snowflake `json:"roles"`
JoinedAt Timestamp `json:"joined_at,omitempty"`
Deaf bool `json:"deaf"`
Mute bool `json:"mute"`
}
func (m *Member) String() string {
return "member{user:" + m.User.Username + ", nick:" + m.Nick + ", ID:" + m.User.ID.String() + "}"
}
func (m *Member) Mention() string {
return m.User.Mention()
}
func (m *Member) DeepCopy() (copy interface{}) {
copy = &Member{}
m.CopyOverTo(copy)
return
}
func (m *Member) CopyOverTo(other interface{}) (err error) {
var ok bool
var member *Member
if member, ok = other.(*Member); !ok {
err = NewErrorUnsupportedType("given interface{} was not of type *Member")
return
}
m.RLock()
member.Lock()
member.GuildID = m.GuildID
member.User = m.User.DeepCopy().(*User)
member.Nick = m.Nick
member.Roles = m.Roles
member.JoinedAt = m.JoinedAt
member.Deaf = m.Deaf
member.Mute = m.Mute
m.RUnlock()
member.Unlock()
return
}
// TODO
// func (m *Member) Clear() Snowflake {
// // do i want to delete user?.. what if there is a PM?
// // Check for user id in DM's
// // or.. since the user object is sent on channel_create events, the user can be reintialized when needed.
// // but should be properly removed from other arrays.
// m.User.Clear()
// id := m.User.ID
// m.User = nil
//
// // use this Snowflake to check in other places. To avoid pointing to random memory spaces
// return id
// }
// TODO
// func (m *Member) Update(new *Member) (err error) {
// if m.User.ID != new.User.ID || m.GuildID != new.GuildID {
// err = errors.New("cannot update user when the new struct has a different Snowflake")
// return
// }
// // make sure that new is not the same pointer!
// if m == new {
// err = errors.New("cannot update user when the new struct points to the same memory space")
// return
// }
//
// m.Lock()
// new.RLock()
// m.Nick = new.Nick
// m.Roles = new.Roles
// m.JoinedAt = new.JoinedAt
// m.Deaf = new.Deaf
// m.Mute = new.Mute
// new.RUnlock()
// m.Unlock()
//
// return
// }
type GuildPruneCount struct {
Pruned int `json:"pruned"`
}