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
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
|
// Discordgo - Discord bindings for Go
// Available at https://github.com/bwmarrin/discordgo
// Copyright 2015-2016 Bruce Marriner <bruce@sqls.net>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This file contains code related to state tracking. If enabled, state
// tracking will capture the initial READY packet and many other websocket
// events and maintain an in-memory state of guilds, channels, users, and
// so forth. This information can be accessed through the Session.State struct.
package discordgo
import (
"errors"
"sort"
"sync"
)
// ErrNilState is returned when the state is nil.
var ErrNilState = errors.New("state not instantiated, please use discordgo.New() or assign Session.State")
// ErrStateNotFound is returned when the state cache
// requested is not found
var ErrStateNotFound = errors.New("state cache not found")
// ErrMessageIncompletePermissions is returned when the message
// requested for permissions does not contain enough data to
// generate the permissions.
var ErrMessageIncompletePermissions = errors.New("message incomplete, unable to determine permissions")
// A State contains the current known state.
// As discord sends this in a READY blob, it seems reasonable to simply
// use that struct as the data store.
type State struct {
sync.RWMutex
Ready
// MaxMessageCount represents how many messages per channel the state will store.
MaxMessageCount int
TrackChannels bool
TrackThreads bool
TrackEmojis bool
TrackMembers bool
TrackThreadMembers bool
TrackRoles bool
TrackVoice bool
TrackPresences bool
guildMap map[string]*Guild
channelMap map[string]*Channel
memberMap map[string]map[string]*Member
}
// NewState creates an empty state.
func NewState() *State {
return &State{
Ready: Ready{
PrivateChannels: []*Channel{},
Guilds: []*Guild{},
},
TrackChannels: true,
TrackThreads: true,
TrackEmojis: true,
TrackMembers: true,
TrackThreadMembers: true,
TrackRoles: true,
TrackVoice: true,
TrackPresences: true,
guildMap: make(map[string]*Guild),
channelMap: make(map[string]*Channel),
memberMap: make(map[string]map[string]*Member),
}
}
func (s *State) createMemberMap(guild *Guild) {
members := make(map[string]*Member)
for _, m := range guild.Members {
members[m.User.ID] = m
}
s.memberMap[guild.ID] = members
}
// GuildAdd adds a guild to the current world state, or
// updates it if it already exists.
func (s *State) GuildAdd(guild *Guild) error {
if s == nil {
return ErrNilState
}
s.Lock()
defer s.Unlock()
// Update the channels to point to the right guild, adding them to the channelMap as we go
for _, c := range guild.Channels {
s.channelMap[c.ID] = c
}
// Add all the threads to the state in case of thread sync list.
for _, t := range guild.Threads {
s.channelMap[t.ID] = t
}
// If this guild contains a new member slice, we must regenerate the member map so the pointers stay valid
if guild.Members != nil {
s.createMemberMap(guild)
} else if _, ok := s.memberMap[guild.ID]; !ok {
// Even if we have no new member slice, we still initialize the member map for this guild if it doesn't exist
s.memberMap[guild.ID] = make(map[string]*Member)
}
if g, ok := s.guildMap[guild.ID]; ok {
// We are about to replace `g` in the state with `guild`, but first we need to
// make sure we preserve any fields that the `guild` doesn't contain from `g`.
if guild.MemberCount == 0 {
guild.MemberCount = g.MemberCount
}
if guild.Roles == nil {
guild.Roles = g.Roles
}
if guild.Emojis == nil {
guild.Emojis = g.Emojis
}
if guild.Members == nil {
guild.Members = g.Members
}
if guild.Presences == nil {
guild.Presences = g.Presences
}
if guild.Channels == nil {
guild.Channels = g.Channels
}
if guild.Threads == nil {
guild.Threads = g.Threads
}
if guild.VoiceStates == nil {
guild.VoiceStates = g.VoiceStates
}
*g = *guild
return nil
}
s.Guilds = append(s.Guilds, guild)
s.guildMap[guild.ID] = guild
return nil
}
// GuildRemove removes a guild from current world state.
func (s *State) GuildRemove(guild *Guild) error {
if s == nil {
return ErrNilState
}
_, err := s.Guild(guild.ID)
if err != nil {
return err
}
s.Lock()
defer s.Unlock()
delete(s.guildMap, guild.ID)
for i, g := range s.Guilds {
if g.ID == guild.ID {
s.Guilds = append(s.Guilds[:i], s.Guilds[i+1:]...)
return nil
}
}
return nil
}
// Guild gets a guild by ID.
// Useful for querying if @me is in a guild:
// _, err := discordgo.Session.State.Guild(guildID)
// isInGuild := err == nil
func (s *State) Guild(guildID string) (*Guild, error) {
if s == nil {
return nil, ErrNilState
}
s.RLock()
defer s.RUnlock()
if g, ok := s.guildMap[guildID]; ok {
return g, nil
}
return nil, ErrStateNotFound
}
func (s *State) presenceAdd(guildID string, presence *Presence) error {
guild, ok := s.guildMap[guildID]
if !ok {
return ErrStateNotFound
}
for i, p := range guild.Presences {
if p.User.ID == presence.User.ID {
//guild.Presences[i] = presence
//Update status
guild.Presences[i].Activities = presence.Activities
if presence.Status != "" {
guild.Presences[i].Status = presence.Status
}
if presence.ClientStatus.Desktop != "" {
guild.Presences[i].ClientStatus.Desktop = presence.ClientStatus.Desktop
}
if presence.ClientStatus.Mobile != "" {
guild.Presences[i].ClientStatus.Mobile = presence.ClientStatus.Mobile
}
if presence.ClientStatus.Web != "" {
guild.Presences[i].ClientStatus.Web = presence.ClientStatus.Web
}
//Update the optionally sent user information
//ID Is a mandatory field so you should not need to check if it is empty
guild.Presences[i].User.ID = presence.User.ID
if presence.User.Avatar != "" {
guild.Presences[i].User.Avatar = presence.User.Avatar
}
if presence.User.Discriminator != "" {
guild.Presences[i].User.Discriminator = presence.User.Discriminator
}
if presence.User.Email != "" {
guild.Presences[i].User.Email = presence.User.Email
}
if presence.User.Token != "" {
guild.Presences[i].User.Token = presence.User.Token
}
if presence.User.Username != "" {
guild.Presences[i].User.Username = presence.User.Username
}
return nil
}
}
guild.Presences = append(guild.Presences, presence)
return nil
}
// PresenceAdd adds a presence to the current world state, or
// updates it if it already exists.
func (s *State) PresenceAdd(guildID string, presence *Presence) error {
if s == nil {
return ErrNilState
}
s.Lock()
defer s.Unlock()
return s.presenceAdd(guildID, presence)
}
// PresenceRemove removes a presence from the current world state.
func (s *State) PresenceRemove(guildID string, presence *Presence) error {
if s == nil {
return ErrNilState
}
guild, err := s.Guild(guildID)
if err != nil {
return err
}
s.Lock()
defer s.Unlock()
for i, p := range guild.Presences {
if p.User.ID == presence.User.ID {
guild.Presences = append(guild.Presences[:i], guild.Presences[i+1:]...)
return nil
}
}
return ErrStateNotFound
}
// Presence gets a presence by ID from a guild.
func (s *State) Presence(guildID, userID string) (*Presence, error) {
if s == nil {
return nil, ErrNilState
}
guild, err := s.Guild(guildID)
if err != nil {
return nil, err
}
for _, p := range guild.Presences {
if p.User.ID == userID {
return p, nil
}
}
return nil, ErrStateNotFound
}
// TODO: Consider moving Guild state update methods onto *Guild.
func (s *State) memberAdd(member *Member) error {
guild, ok := s.guildMap[member.GuildID]
if !ok {
return ErrStateNotFound
}
members, ok := s.memberMap[member.GuildID]
if !ok {
return ErrStateNotFound
}
m, ok := members[member.User.ID]
if !ok {
members[member.User.ID] = member
guild.Members = append(guild.Members, member)
} else {
// We are about to replace `m` in the state with `member`, but first we need to
// make sure we preserve any fields that the `member` doesn't contain from `m`.
if member.JoinedAt.IsZero() {
member.JoinedAt = m.JoinedAt
}
*m = *member
}
return nil
}
// MemberAdd adds a member to the current world state, or
// updates it if it already exists.
func (s *State) MemberAdd(member *Member) error {
if s == nil {
return ErrNilState
}
s.Lock()
defer s.Unlock()
return s.memberAdd(member)
}
// MemberRemove removes a member from current world state.
func (s *State) MemberRemove(member *Member) error {
if s == nil {
return ErrNilState
}
guild, err := s.Guild(member.GuildID)
if err != nil {
return err
}
s.Lock()
defer s.Unlock()
members, ok := s.memberMap[member.GuildID]
if !ok {
return ErrStateNotFound
}
_, ok = members[member.User.ID]
if !ok {
return ErrStateNotFound
}
delete(members, member.User.ID)
for i, m := range guild.Members {
if m.User.ID == member.User.ID {
guild.Members = append(guild.Members[:i], guild.Members[i+1:]...)
return nil
}
}
return ErrStateNotFound
}
// Member gets a member by ID from a guild.
func (s *State) Member(guildID, userID string) (*Member, error) {
if s == nil {
return nil, ErrNilState
}
s.RLock()
defer s.RUnlock()
members, ok := s.memberMap[guildID]
if !ok {
return nil, ErrStateNotFound
}
m, ok := members[userID]
if ok {
return m, nil
}
return nil, ErrStateNotFound
}
// RoleAdd adds a role to the current world state, or
// updates it if it already exists.
func (s *State) RoleAdd(guildID string, role *Role) error {
if s == nil {
return ErrNilState
}
guild, err := s.Guild(guildID)
if err != nil {
return err
}
s.Lock()
defer s.Unlock()
for i, r := range guild.Roles {
if r.ID == role.ID {
guild.Roles[i] = role
return nil
}
}
guild.Roles = append(guild.Roles, role)
return nil
}
// RoleRemove removes a role from current world state by ID.
func (s *State) RoleRemove(guildID, roleID string) error {
if s == nil {
return ErrNilState
}
guild, err := s.Guild(guildID)
if err != nil {
return err
}
s.Lock()
defer s.Unlock()
for i, r := range guild.Roles {
if r.ID == roleID {
guild.Roles = append(guild.Roles[:i], guild.Roles[i+1:]...)
return nil
}
}
return ErrStateNotFound
}
// Role gets a role by ID from a guild.
func (s *State) Role(guildID, roleID string) (*Role, error) {
if s == nil {
return nil, ErrNilState
}
guild, err := s.Guild(guildID)
if err != nil {
return nil, err
}
s.RLock()
defer s.RUnlock()
for _, r := range guild.Roles {
if r.ID == roleID {
return r, nil
}
}
return nil, ErrStateNotFound
}
// ChannelAdd adds a channel to the current world state, or
// updates it if it already exists.
// Channels may exist either as PrivateChannels or inside
// a guild.
func (s *State) ChannelAdd(channel *Channel) error {
if s == nil {
return ErrNilState
}
s.Lock()
defer s.Unlock()
// If the channel exists, replace it
if c, ok := s.channelMap[channel.ID]; ok {
if channel.Messages == nil {
channel.Messages = c.Messages
}
if channel.PermissionOverwrites == nil {
channel.PermissionOverwrites = c.PermissionOverwrites
}
if channel.ThreadMetadata == nil {
channel.ThreadMetadata = c.ThreadMetadata
}
*c = *channel
return nil
}
if channel.Type == ChannelTypeDM || channel.Type == ChannelTypeGroupDM {
s.PrivateChannels = append(s.PrivateChannels, channel)
s.channelMap[channel.ID] = channel
return nil
}
guild, ok := s.guildMap[channel.GuildID]
if !ok {
return ErrStateNotFound
}
if channel.IsThread() {
guild.Threads = append(guild.Threads, channel)
} else {
guild.Channels = append(guild.Channels, channel)
}
s.channelMap[channel.ID] = channel
return nil
}
// ChannelRemove removes a channel from current world state.
func (s *State) ChannelRemove(channel *Channel) error {
if s == nil {
return ErrNilState
}
_, err := s.Channel(channel.ID)
if err != nil {
return err
}
if channel.Type == ChannelTypeDM || channel.Type == ChannelTypeGroupDM {
s.Lock()
defer s.Unlock()
for i, c := range s.PrivateChannels {
if c.ID == channel.ID {
s.PrivateChannels = append(s.PrivateChannels[:i], s.PrivateChannels[i+1:]...)
break
}
}
delete(s.channelMap, channel.ID)
return nil
}
guild, err := s.Guild(channel.GuildID)
if err != nil {
return err
}
s.Lock()
defer s.Unlock()
if channel.IsThread() {
for i, t := range guild.Threads {
if t.ID == channel.ID {
guild.Threads = append(guild.Threads[:i], guild.Threads[i+1:]...)
break
}
}
} else {
for i, c := range guild.Channels {
if c.ID == channel.ID {
guild.Channels = append(guild.Channels[:i], guild.Channels[i+1:]...)
break
}
}
}
delete(s.channelMap, channel.ID)
return nil
}
// ThreadListSync syncs guild threads with provided ones.
func (s *State) ThreadListSync(tls *ThreadListSync) error {
guild, err := s.Guild(tls.GuildID)
if err != nil {
return err
}
s.Lock()
defer s.Unlock()
// This algorithm filters out archived or
// threads which are children of channels in channelIDs
// and then it adds all synced threads to guild threads and cache
index := 0
outer:
for _, t := range guild.Threads {
if !t.ThreadMetadata.Archived && tls.ChannelIDs != nil {
for _, v := range tls.ChannelIDs {
if t.ParentID == v {
delete(s.channelMap, t.ID)
continue outer
}
}
guild.Threads[index] = t
index++
} else {
delete(s.channelMap, t.ID)
}
}
guild.Threads = guild.Threads[:index]
for _, t := range tls.Threads {
s.channelMap[t.ID] = t
guild.Threads = append(guild.Threads, t)
}
for _, m := range tls.Members {
if c, ok := s.channelMap[m.ID]; ok {
c.Member = m
}
}
return nil
}
// ThreadMembersUpdate updates thread members list
func (s *State) ThreadMembersUpdate(tmu *ThreadMembersUpdate) error {
thread, err := s.Channel(tmu.ID)
if err != nil {
return err
}
s.Lock()
defer s.Unlock()
for idx, member := range thread.Members {
for _, removedMember := range tmu.RemovedMembers {
if member.ID == removedMember {
thread.Members = append(thread.Members[:idx], thread.Members[idx+1:]...)
break
}
}
}
for _, addedMember := range tmu.AddedMembers {
thread.Members = append(thread.Members, addedMember.ThreadMember)
if addedMember.Member != nil {
err = s.memberAdd(addedMember.Member)
if err != nil {
return err
}
}
if addedMember.Presence != nil {
err = s.presenceAdd(tmu.GuildID, addedMember.Presence)
if err != nil {
return err
}
}
}
thread.MemberCount = tmu.MemberCount
return nil
}
// ThreadMemberUpdate sets or updates member data for the current user.
func (s *State) ThreadMemberUpdate(mu *ThreadMemberUpdate) error {
thread, err := s.Channel(mu.ID)
if err != nil {
return err
}
thread.Member = mu.ThreadMember
return nil
}
// Channel gets a channel by ID, it will look in all guilds and private channels.
func (s *State) Channel(channelID string) (*Channel, error) {
if s == nil {
return nil, ErrNilState
}
s.RLock()
defer s.RUnlock()
if c, ok := s.channelMap[channelID]; ok {
return c, nil
}
return nil, ErrStateNotFound
}
// Emoji returns an emoji for a guild and emoji id.
func (s *State) Emoji(guildID, emojiID string) (*Emoji, error) {
if s == nil {
return nil, ErrNilState
}
guild, err := s.Guild(guildID)
if err != nil {
return nil, err
}
s.RLock()
defer s.RUnlock()
for _, e := range guild.Emojis {
if e.ID == emojiID {
return e, nil
}
}
return nil, ErrStateNotFound
}
// EmojiAdd adds an emoji to the current world state.
func (s *State) EmojiAdd(guildID string, emoji *Emoji) error {
if s == nil {
return ErrNilState
}
guild, err := s.Guild(guildID)
if err != nil {
return err
}
s.Lock()
defer s.Unlock()
for i, e := range guild.Emojis {
if e.ID == emoji.ID {
guild.Emojis[i] = emoji
return nil
}
}
guild.Emojis = append(guild.Emojis, emoji)
return nil
}
// EmojisAdd adds multiple emojis to the world state.
func (s *State) EmojisAdd(guildID string, emojis []*Emoji) error {
for _, e := range emojis {
if err := s.EmojiAdd(guildID, e); err != nil {
return err
}
}
return nil
}
// MessageAdd adds a message to the current world state, or updates it if it exists.
// If the channel cannot be found, the message is discarded.
// Messages are kept in state up to s.MaxMessageCount per channel.
func (s *State) MessageAdd(message *Message) error {
if s == nil {
return ErrNilState
}
c, err := s.Channel(message.ChannelID)
if err != nil {
return err
}
s.Lock()
defer s.Unlock()
// If the message exists, merge in the new message contents.
for _, m := range c.Messages {
if m.ID == message.ID {
if message.Content != "" {
m.Content = message.Content
}
if message.EditedTimestamp != nil {
m.EditedTimestamp = message.EditedTimestamp
}
if message.Mentions != nil {
m.Mentions = message.Mentions
}
if message.Embeds != nil {
m.Embeds = message.Embeds
}
if message.Attachments != nil {
m.Attachments = message.Attachments
}
if !message.Timestamp.IsZero() {
m.Timestamp = message.Timestamp
}
if message.Author != nil {
m.Author = message.Author
}
if message.Components != nil {
m.Components = message.Components
}
return nil
}
}
c.Messages = append(c.Messages, message)
if len(c.Messages) > s.MaxMessageCount {
c.Messages = c.Messages[len(c.Messages)-s.MaxMessageCount:]
}
return nil
}
// MessageRemove removes a message from the world state.
func (s *State) MessageRemove(message *Message) error {
if s == nil {
return ErrNilState
}
return s.messageRemoveByID(message.ChannelID, message.ID)
}
// messageRemoveByID removes a message by channelID and messageID from the world state.
func (s *State) messageRemoveByID(channelID, messageID string) error {
c, err := s.Channel(channelID)
if err != nil {
return err
}
s.Lock()
defer s.Unlock()
for i, m := range c.Messages {
if m.ID == messageID {
c.Messages = append(c.Messages[:i], c.Messages[i+1:]...)
return nil
}
}
return ErrStateNotFound
}
func (s *State) voiceStateUpdate(update *VoiceStateUpdate) error {
guild, err := s.Guild(update.GuildID)
if err != nil {
return err
}
s.Lock()
defer s.Unlock()
// Handle Leaving Channel
if update.ChannelID == "" {
for i, state := range guild.VoiceStates {
if state.UserID == update.UserID {
guild.VoiceStates = append(guild.VoiceStates[:i], guild.VoiceStates[i+1:]...)
return nil
}
}
} else {
for i, state := range guild.VoiceStates {
if state.UserID == update.UserID {
guild.VoiceStates[i] = update.VoiceState
return nil
}
}
guild.VoiceStates = append(guild.VoiceStates, update.VoiceState)
}
return nil
}
// VoiceState gets a VoiceState by guild and user ID.
func (s *State) VoiceState(guildID, userID string) (*VoiceState, error) {
if s == nil {
return nil, ErrNilState
}
guild, err := s.Guild(guildID)
if err != nil {
return nil, err
}
for _, state := range guild.VoiceStates {
if state.UserID == userID {
return state, nil
}
}
return nil, ErrStateNotFound
}
// Message gets a message by channel and message ID.
func (s *State) Message(channelID, messageID string) (*Message, error) {
if s == nil {
return nil, ErrNilState
}
c, err := s.Channel(channelID)
if err != nil {
return nil, err
}
s.RLock()
defer s.RUnlock()
for _, m := range c.Messages {
if m.ID == messageID {
return m, nil
}
}
return nil, ErrStateNotFound
}
// OnReady takes a Ready event and updates all internal state.
func (s *State) onReady(se *Session, r *Ready) (err error) {
if s == nil {
return ErrNilState
}
s.Lock()
defer s.Unlock()
// We must track at least the current user for Voice, even
// if state is disabled, store the bare essentials.
if !se.StateEnabled {
ready := Ready{
Version: r.Version,
SessionID: r.SessionID,
User: r.User,
Shard: r.Shard,
Application: r.Application,
}
s.Ready = ready
return nil
}
s.Ready = *r
for _, g := range s.Guilds {
s.guildMap[g.ID] = g
s.createMemberMap(g)
for _, c := range g.Channels {
s.channelMap[c.ID] = c
}
}
for _, c := range s.PrivateChannels {
s.channelMap[c.ID] = c
}
return nil
}
// OnInterface handles all events related to states.
func (s *State) OnInterface(se *Session, i interface{}) (err error) {
if s == nil {
return ErrNilState
}
r, ok := i.(*Ready)
if ok {
return s.onReady(se, r)
}
if !se.StateEnabled {
return nil
}
switch t := i.(type) {
case *GuildCreate:
err = s.GuildAdd(t.Guild)
case *GuildUpdate:
err = s.GuildAdd(t.Guild)
case *GuildDelete:
var old *Guild
old, err = s.Guild(t.ID)
if err == nil {
oldCopy := *old
t.BeforeDelete = &oldCopy
}
err = s.GuildRemove(t.Guild)
case *GuildMemberAdd:
var guild *Guild
// Updates the MemberCount of the guild.
guild, err = s.Guild(t.Member.GuildID)
if err != nil {
return err
}
guild.MemberCount++
// Caches member if tracking is enabled.
if s.TrackMembers {
err = s.MemberAdd(t.Member)
}
case *GuildMemberUpdate:
if s.TrackMembers {
var old *Member
old, err = s.Member(t.GuildID, t.User.ID)
if err == nil {
oldCopy := *old
t.BeforeUpdate = &oldCopy
}
err = s.MemberAdd(t.Member)
}
case *GuildMemberRemove:
var guild *Guild
// Updates the MemberCount of the guild.
guild, err = s.Guild(t.Member.GuildID)
if err != nil {
return err
}
guild.MemberCount--
// Removes member from the cache if tracking is enabled.
if s.TrackMembers {
err = s.MemberRemove(t.Member)
}
case *GuildMembersChunk:
if s.TrackMembers {
for i := range t.Members {
t.Members[i].GuildID = t.GuildID
err = s.MemberAdd(t.Members[i])
}
}
if s.TrackPresences {
for _, p := range t.Presences {
err = s.PresenceAdd(t.GuildID, p)
}
}
case *GuildRoleCreate:
if s.TrackRoles {
err = s.RoleAdd(t.GuildID, t.Role)
}
case *GuildRoleUpdate:
if s.TrackRoles {
err = s.RoleAdd(t.GuildID, t.Role)
}
case *GuildRoleDelete:
if s.TrackRoles {
err = s.RoleRemove(t.GuildID, t.RoleID)
}
case *GuildEmojisUpdate:
if s.TrackEmojis {
var guild *Guild
guild, err = s.Guild(t.GuildID)
if err != nil {
return err
}
s.Lock()
defer s.Unlock()
guild.Emojis = t.Emojis
}
case *ChannelCreate:
if s.TrackChannels {
err = s.ChannelAdd(t.Channel)
}
case *ChannelUpdate:
if s.TrackChannels {
err = s.ChannelAdd(t.Channel)
}
case *ChannelDelete:
if s.TrackChannels {
err = s.ChannelRemove(t.Channel)
}
case *ThreadCreate:
if s.TrackThreads {
err = s.ChannelAdd(t.Channel)
}
case *ThreadUpdate:
if s.TrackThreads {
old, err := s.Channel(t.ID)
if err == nil {
oldCopy := *old
t.BeforeUpdate = &oldCopy
}
err = s.ChannelAdd(t.Channel)
}
case *ThreadDelete:
if s.TrackThreads {
err = s.ChannelRemove(t.Channel)
}
case *ThreadMemberUpdate:
if s.TrackThreads {
err = s.ThreadMemberUpdate(t)
}
case *ThreadMembersUpdate:
if s.TrackThreadMembers {
err = s.ThreadMembersUpdate(t)
}
case *ThreadListSync:
if s.TrackThreads {
err = s.ThreadListSync(t)
}
case *MessageCreate:
if s.MaxMessageCount != 0 {
err = s.MessageAdd(t.Message)
}
case *MessageUpdate:
if s.MaxMessageCount != 0 {
var old *Message
old, err = s.Message(t.ChannelID, t.ID)
if err == nil {
oldCopy := *old
t.BeforeUpdate = &oldCopy
}
err = s.MessageAdd(t.Message)
}
case *MessageDelete:
if s.MaxMessageCount != 0 {
var old *Message
old, err = s.Message(t.ChannelID, t.ID)
if err == nil {
oldCopy := *old
t.BeforeDelete = &oldCopy
}
err = s.MessageRemove(t.Message)
}
case *MessageDeleteBulk:
if s.MaxMessageCount != 0 {
for _, mID := range t.Messages {
s.messageRemoveByID(t.ChannelID, mID)
}
}
case *VoiceStateUpdate:
if s.TrackVoice {
var old *VoiceState
old, err = s.VoiceState(t.GuildID, t.UserID)
if err == nil {
oldCopy := *old
t.BeforeUpdate = &oldCopy
}
err = s.voiceStateUpdate(t)
}
case *PresenceUpdate:
if s.TrackPresences {
s.PresenceAdd(t.GuildID, &t.Presence)
}
if s.TrackMembers {
if t.Status == StatusOffline {
return
}
var m *Member
m, err = s.Member(t.GuildID, t.User.ID)
if err != nil {
// Member not found; this is a user coming online
m = &Member{
GuildID: t.GuildID,
User: t.User,
}
} else {
if t.User.Username != "" {
m.User.Username = t.User.Username
}
}
err = s.MemberAdd(m)
}
}
return
}
// UserChannelPermissions returns the permission of a user in a channel.
// userID : The ID of the user to calculate permissions for.
// channelID : The ID of the channel to calculate permission for.
func (s *State) UserChannelPermissions(userID, channelID string) (apermissions int64, err error) {
if s == nil {
return 0, ErrNilState
}
channel, err := s.Channel(channelID)
if err != nil {
return
}
guild, err := s.Guild(channel.GuildID)
if err != nil {
return
}
member, err := s.Member(guild.ID, userID)
if err != nil {
return
}
return memberPermissions(guild, channel, userID, member.Roles), nil
}
// MessagePermissions returns the permissions of the author of the message
// in the channel in which it was sent.
func (s *State) MessagePermissions(message *Message) (apermissions int64, err error) {
if s == nil {
return 0, ErrNilState
}
if message.Author == nil || message.Member == nil {
return 0, ErrMessageIncompletePermissions
}
channel, err := s.Channel(message.ChannelID)
if err != nil {
return
}
guild, err := s.Guild(channel.GuildID)
if err != nil {
return
}
return memberPermissions(guild, channel, message.Author.ID, message.Member.Roles), nil
}
// UserColor returns the color of a user in a channel.
// While colors are defined at a Guild level, determining for a channel is more useful in message handlers.
// 0 is returned in cases of error, which is the color of @everyone.
// userID : The ID of the user to calculate the color for.
// channelID : The ID of the channel to calculate the color for.
func (s *State) UserColor(userID, channelID string) int {
if s == nil {
return 0
}
channel, err := s.Channel(channelID)
if err != nil {
return 0
}
guild, err := s.Guild(channel.GuildID)
if err != nil {
return 0
}
member, err := s.Member(guild.ID, userID)
if err != nil {
return 0
}
return firstRoleColorColor(guild, member.Roles)
}
// MessageColor returns the color of the author's name as displayed
// in the client associated with this message.
func (s *State) MessageColor(message *Message) int {
if s == nil {
return 0
}
if message.Member == nil || message.Member.Roles == nil {
return 0
}
channel, err := s.Channel(message.ChannelID)
if err != nil {
return 0
}
guild, err := s.Guild(channel.GuildID)
if err != nil {
return 0
}
return firstRoleColorColor(guild, message.Member.Roles)
}
func firstRoleColorColor(guild *Guild, memberRoles []string) int {
roles := Roles(guild.Roles)
sort.Sort(roles)
for _, role := range roles {
for _, roleID := range memberRoles {
if role.ID == roleID {
if role.Color != 0 {
return role.Color
}
}
}
}
for _, role := range roles {
if role.ID == guild.ID {
return role.Color
}
}
return 0
}
|