-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathstate_events.go
499 lines (413 loc) · 13.2 KB
/
state_events.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
package state
import (
"fmt"
"github.com/diamondburned/arikawa/v3/discord"
"github.com/diamondburned/arikawa/v3/gateway"
"github.com/diamondburned/arikawa/v3/state/store"
)
func (s *State) hookSession() {
s.Session.AddSyncHandler(func(event interface{}) {
// Call the pre-handler before the state handler.
if s.PreHandler != nil {
s.PreHandler.Call(event)
}
// Run the state handler.
s.onEvent(event)
switch event := event.(type) {
case *gateway.ReadyEvent:
s.Handler.Call(event)
s.handleReady(event)
case *gateway.GuildCreateEvent:
s.Handler.Call(event)
s.handleGuildCreate(event)
case *gateway.GuildDeleteEvent:
s.Handler.Call(event)
s.handleGuildDelete(event)
// https://github.com/discord/discord-api-docs/commit/01665c4
case *gateway.MessageCreateEvent:
if event.Member != nil {
event.Member.User = event.Author
}
s.Handler.Call(event)
case *gateway.MessageUpdateEvent:
if event.Member != nil {
event.Member.User = event.Author
}
s.Handler.Call(event)
default:
s.Handler.Call(event)
}
})
}
func (s *State) onEvent(iface interface{}) {
switch ev := iface.(type) {
case *gateway.ReadyEvent:
// Acquire the ready mutex, but since we're only writing the value and
// not anything in it, we should be fine.
s.readyMu.Lock()
s.ready = *ev
s.readyMu.Unlock()
// Reset the store before proceeding.
if err := s.Cabinet.Reset(); err != nil {
s.stateErr(err, "failed to reset state in Ready")
}
// Handle guilds
for i := range ev.Guilds {
s.batchLog(storeGuildCreate(s.Cabinet, &ev.Guilds[i]))
}
// Handle guild presences
for i, presence := range ev.Presences {
if err := s.Cabinet.PresenceSet(presence.GuildID, &ev.Presences[i], false); err != nil {
s.stateErr(err, "failed to set presence in Ready")
}
}
// Handle private channels
for i := range ev.PrivateChannels {
if err := s.Cabinet.ChannelSet(&ev.PrivateChannels[i], false); err != nil {
s.stateErr(err, "failed to set channel in Ready")
}
}
// Handle user
if err := s.Cabinet.MyselfSet(ev.User, false); err != nil {
s.stateErr(err, "failed to set self in Ready")
}
case *gateway.ReadySupplementalEvent:
// Handle guilds
for _, guild := range ev.Guilds {
// Handle guild voice states
for i := range guild.VoiceStates {
v := &guild.VoiceStates[i]
if err := s.Cabinet.VoiceStateSet(guild.ID, v, false); err != nil {
s.stateErr(err, "failed to set guild voice state in Ready Supplemental")
}
}
}
friendPresences := gateway.ConvertSupplementalPresences(ev.MergedPresences.Friends)
for i := range friendPresences {
if err := s.Cabinet.PresenceSet(0, &friendPresences[i], false); err != nil {
s.stateErr(err, "failed to set friend presence in Ready Supplemental")
}
}
// Discord uses weird indexing, so we'll need the Guilds slice.
ready := s.Ready()
for i := 0; i < len(ready.Guilds) && i < len(ev.MergedMembers); i++ {
guild := ready.Guilds[i]
members := gateway.ConvertSupplementalMembers(ev.MergedMembers[i])
for i := range members {
if err := s.Cabinet.MemberSet(guild.ID, &members[i], false); err != nil {
s.stateErr(err, "failed to set friend presence in Ready Supplemental")
}
}
presences := gateway.ConvertSupplementalPresences(ev.MergedPresences.Guilds[i])
for i := range presences {
if err := s.Cabinet.PresenceSet(guild.ID, &presences[i], false); err != nil {
s.stateErr(err, "failed to set member presence in Ready Supplemental")
}
}
}
case *gateway.GuildCreateEvent:
s.batchLog(storeGuildCreate(s.Cabinet, ev))
case *gateway.GuildUpdateEvent:
if err := s.Cabinet.GuildSet(&ev.Guild, true); err != nil {
s.stateErr(err, "failed to update guild in state")
}
case *gateway.GuildDeleteEvent:
if err := s.Cabinet.GuildRemove(ev.ID); err != nil && !ev.Unavailable {
s.stateErr(err, "failed to delete guild in state")
}
case *gateway.GuildMemberAddEvent:
if err := s.Cabinet.MemberSet(ev.GuildID, &ev.Member, false); err != nil {
s.stateErr(err, "failed to add a member in state")
}
case *gateway.GuildMemberUpdateEvent:
m, err := s.Cabinet.Member(ev.GuildID, ev.User.ID)
if err != nil {
// We can't do much here.
m = &discord.Member{}
}
// Update available fields from ev into m
ev.UpdateMember(m)
if err := s.Cabinet.MemberSet(ev.GuildID, m, true); err != nil {
s.stateErr(err, "failed to update a member in state")
}
case *gateway.GuildMemberRemoveEvent:
if err := s.Cabinet.MemberRemove(ev.GuildID, ev.User.ID); err != nil {
s.stateErr(err, "failed to remove a member in state")
}
case *gateway.GuildMembersChunkEvent:
for i := range ev.Members {
if err := s.Cabinet.MemberSet(ev.GuildID, &ev.Members[i], false); err != nil {
s.stateErr(err, "failed to add a member from chunk in state")
}
}
for i := range ev.Presences {
if err := s.Cabinet.PresenceSet(ev.GuildID, &ev.Presences[i], false); err != nil {
s.stateErr(err, "failed to add a presence from chunk in state")
}
}
case *gateway.GuildRoleCreateEvent:
if err := s.Cabinet.RoleSet(ev.GuildID, &ev.Role, false); err != nil {
s.stateErr(err, "failed to add a role in state")
}
case *gateway.GuildRoleUpdateEvent:
if err := s.Cabinet.RoleSet(ev.GuildID, &ev.Role, true); err != nil {
s.stateErr(err, "failed to update a role in state")
}
case *gateway.GuildRoleDeleteEvent:
if err := s.Cabinet.RoleRemove(ev.GuildID, ev.RoleID); err != nil {
s.stateErr(err, "failed to remove a role in state")
}
case *gateway.GuildEmojisUpdateEvent:
if err := s.Cabinet.EmojiSet(ev.GuildID, ev.Emojis, true); err != nil {
s.stateErr(err, "failed to update emojis in state")
}
case *gateway.ChannelCreateEvent:
if err := s.Cabinet.ChannelSet(&ev.Channel, false); err != nil {
s.stateErr(err, "failed to create a channel in state")
}
case *gateway.ChannelUpdateEvent:
if err := s.Cabinet.ChannelSet(&ev.Channel, true); err != nil {
s.stateErr(err, "failed to update a channel in state")
}
case *gateway.ChannelDeleteEvent:
if err := s.Cabinet.ChannelRemove(&ev.Channel); err != nil {
s.stateErr(err, "failed to remove a channel in state")
}
case *gateway.ChannelPinsUpdateEvent:
// not tracked.
case *gateway.ThreadListSyncEvent:
for i := range ev.Threads {
if err := s.Cabinet.ChannelSet(&ev.Threads[i], true); err != nil {
s.stateErr(err, "failed to set a thread in state sync")
}
}
case *gateway.ThreadCreateEvent:
if err := s.Cabinet.ChannelSet(&ev.Channel, false); err != nil {
s.stateErr(err, "failed to create a thread in state")
}
case *gateway.ThreadUpdateEvent:
if err := s.Cabinet.ChannelSet(&ev.Channel, true); err != nil {
s.stateErr(err, "failed to update a thread in state")
}
case *gateway.ThreadDeleteEvent:
if ch, err := s.Cabinet.Channel(ev.ID); err == nil {
if err := s.Cabinet.ChannelRemove(ch); err != nil {
s.stateErr(err, "failed to delete a thread in state")
}
}
case *gateway.MessageCreateEvent:
if err := s.Cabinet.MessageSet(&ev.Message, false); err != nil {
s.stateErr(err, "failed to add a message in state")
}
case *gateway.MessageUpdateEvent:
if err := s.Cabinet.MessageSet(&ev.Message, true); err != nil {
s.stateErr(err, "failed to update a message in state")
}
case *gateway.MessageDeleteEvent:
if err := s.Cabinet.MessageRemove(ev.ChannelID, ev.ID); err != nil {
s.stateErr(err, "failed to delete a message in state")
}
case *gateway.MessageDeleteBulkEvent:
for _, id := range ev.IDs {
if err := s.Cabinet.MessageRemove(ev.ChannelID, id); err != nil {
s.stateErr(err, "failed to delete bulk messages in state")
}
}
case *gateway.MessageReactionAddEvent:
var me bool
if u, _ := s.Cabinet.Me(); u != nil {
me = ev.UserID == u.ID
}
s.editMessage(ev.ChannelID, ev.MessageID, func(m *discord.Message) bool {
if i := findReaction(m.Reactions, ev.Emoji); i > -1 {
// Copy the reactions slice so it's not racy.
m.Reactions = append([]discord.Reaction(nil), m.Reactions...)
m.Reactions[i].Count++
m.Reactions[i].Me = m.Reactions[i].Me || me
} else {
old := m.Reactions
m.Reactions = make([]discord.Reaction, 0, len(old)+1)
m.Reactions = append(m.Reactions, old...)
m.Reactions = append(m.Reactions, discord.Reaction{
Count: 1,
Me: me,
Emoji: ev.Emoji,
})
}
return true
})
case *gateway.MessageReactionRemoveEvent:
s.editMessage(ev.ChannelID, ev.MessageID, func(m *discord.Message) bool {
var i = findReaction(m.Reactions, ev.Emoji)
if i < 0 {
return false
}
r := &m.Reactions[i]
// If the count is 0:
if (r.Count - 1) < 1 {
old := m.Reactions
m.Reactions = make([]discord.Reaction, len(m.Reactions)-1)
copy(m.Reactions[0:], old[:i])
copy(m.Reactions[i:], old[i+1:])
} else {
r.Count--
if r.Me { // If reaction removal is the user's
u, err := s.Cabinet.Me()
if err == nil && ev.UserID == u.ID {
r.Me = false
}
}
}
return true
})
case *gateway.MessageReactionRemoveAllEvent:
s.editMessage(ev.ChannelID, ev.MessageID, func(m *discord.Message) bool {
m.Reactions = nil
return true
})
case *gateway.MessageReactionRemoveEmojiEvent:
s.editMessage(ev.ChannelID, ev.MessageID, func(m *discord.Message) bool {
var i = findReaction(m.Reactions, ev.Emoji)
if i < 0 {
return false
}
m.Reactions = append(m.Reactions[:i], m.Reactions[i+1:]...)
return true
})
case *gateway.PresenceUpdateEvent:
if err := s.Cabinet.PresenceSet(ev.GuildID, &ev.Presence, true); err != nil {
s.stateErr(err, "failed to update presence in state")
}
case *gateway.PresencesReplaceEvent:
for _, p := range *ev {
if err := s.Cabinet.PresenceSet(p.GuildID, &p.Presence, true); err != nil {
s.stateErr(err, "failed to update presence in state")
}
}
case *gateway.SessionsReplaceEvent:
// TODO
case *gateway.UserGuildSettingsUpdateEvent:
// TODO
case *gateway.UserSettingsUpdateEvent:
s.readyMu.Lock()
s.ready.UserSettings = &ev.UserSettings
s.readyMu.Unlock()
case *gateway.UserNoteUpdateEvent:
// TODO
case *gateway.UserUpdateEvent:
if err := s.Cabinet.MyselfSet(ev.User, true); err != nil {
s.stateErr(err, "failed to update myself from USER_UPDATE")
}
case *gateway.VoiceStateUpdateEvent:
vs := &ev.VoiceState
if !vs.ChannelID.IsValid() {
if err := s.Cabinet.VoiceStateRemove(vs.GuildID, vs.UserID); err != nil {
s.stateErr(err, "failed to remove voice state from state")
}
} else {
if err := s.Cabinet.VoiceStateSet(vs.GuildID, vs, true); err != nil {
s.stateErr(err, "failed to update voice state in state")
}
}
}
}
func (s *State) stateErr(err error, wrap string) {
s.StateLog(fmt.Errorf("%s: %w", wrap, err))
}
func (s *State) batchLog(errors []error) {
for _, err := range errors {
s.StateLog(err)
}
}
// Helper functions
func (s *State) editMessage(ch discord.ChannelID, msg discord.MessageID, fn func(m *discord.Message) bool) {
m, err := s.Cabinet.Message(ch, msg)
if err != nil {
return
}
// Copy the messages.
cpy := *m
m = &cpy
if !fn(m) {
return
}
if err := s.Cabinet.MessageSet(m, true); err != nil {
s.stateErr(err, "failed to save message in reaction add")
}
}
func findReaction(rs []discord.Reaction, emoji discord.Emoji) int {
for i := range rs {
if rs[i].Emoji.ID == emoji.ID && rs[i].Emoji.Name == emoji.Name {
return i
}
}
return -1
}
func storeGuildCreate(cab *store.Cabinet, guild *gateway.GuildCreateEvent) []error {
if guild.Unavailable {
return nil
}
stack, errs := newErrorStack()
if err := cab.GuildSet(&guild.Guild, false); err != nil {
errs(err, "failed to set guild in Ready")
}
// Handle guild emojis
if len(guild.Emojis) > 0 {
if err := cab.EmojiSet(guild.ID, guild.Emojis, false); err != nil {
errs(err, "failed to set guild emojis")
}
}
// Handle guild member
for i := range guild.Members {
if err := cab.MemberSet(guild.ID, &guild.Members[i], false); err != nil {
errs(err, "failed to set guild member in Ready")
}
}
// Handle guild channels
for _, ch := range guild.Channels {
// I HATE Discord.
ch := ch
ch.GuildID = guild.ID
if err := cab.ChannelSet(&ch, false); err != nil {
errs(err, "failed to set guild channel in Ready")
}
}
// Handle threads.
for _, ch := range guild.Threads {
ch := ch
ch.GuildID = guild.ID
if err := cab.ChannelSet(&ch, false); err != nil {
errs(err, "failed to set guild thread in Ready")
}
}
// Handle guild presences
for _, p := range guild.Presences {
p := p
p.GuildID = guild.ID
if err := cab.PresenceSet(guild.ID, &p, false); err != nil {
errs(err, "failed to set guild presence in Ready")
}
}
// Handle guild voice states
for _, v := range guild.VoiceStates {
v := v
v.GuildID = guild.ID
if err := cab.VoiceStateSet(guild.ID, &v, false); err != nil {
errs(err, "failed to set guild voice state in Ready")
}
}
// Handle guild roles
for _, r := range guild.Roles {
r := r
if err := cab.RoleSet(guild.ID, &r, false); err != nil {
errs(err, "failed to set role in Ready")
}
}
return *stack
}
func newErrorStack() (*[]error, func(error, string)) {
var errs = new([]error)
return errs, func(err error, wrap string) {
*errs = append(*errs, fmt.Errorf("%s: %w", wrap, err))
}
}