-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdispatch.go
406 lines (379 loc) · 12.7 KB
/
dispatch.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
package harmony
import (
"encoding/json"
"fmt"
"github.com/skwair/harmony/discord"
"github.com/skwair/harmony/voice"
)
const (
eventHello = "HELLO"
eventReady = "READY"
eventResumed = "RESUMED"
eventInvalidSession = "INVALID_SESSION"
eventChannelCreate = "CHANNEL_CREATE"
eventChannelUpdate = "CHANNEL_UPDATE"
eventChannelDelete = "CHANNEL_DELETE"
eventChannelPinsUpdate = "CHANNEL_PINS_UPDATE"
eventGuildCreate = "GUILD_CREATE"
eventGuildUpdate = "GUILD_UPDATE"
eventGuildDelete = "GUILD_DELETE"
eventGuildBanAdd = "GUILD_BAN_ADD"
eventGuildBanRemove = "GUILD_BAN_REMOVE"
eventGuildEmojisUpdate = "GUILD_EMOJIS_UPDATE"
eventGuildIntegrationsUpdate = "GUILD_INTEGRATIONS_UPDATE"
eventGuildMemberAdd = "GUILD_MEMBER_ADD"
eventGuildMemberRemove = "GUILD_MEMBER_REMOVE"
eventGuildMemberUpdate = "GUILD_MEMBER_UPDATE"
eventGuildMembersChunk = "GUILD_MEMBERS_CHUNK"
eventGuildRoleCreate = "GUILD_ROLE_CREATE"
eventGuildRoleUpdate = "GUILD_ROLE_UPDATE"
eventGuildRoleDelete = "GUILD_ROLE_DELETE"
eventGuildInviteCreate = "INVITE_CREATE"
eventGuildInviteDelete = "INVITE_DELETE"
eventMessageCreate = "MESSAGE_CREATE"
eventMessageUpdate = "MESSAGE_UPDATE"
eventMessageDelete = "MESSAGE_DELETE"
eventMessageDeleteBulk = "MESSAGE_DELETE_BULK"
eventMessageAck = "MESSAGE_ACK"
eventMessageReactionAdd = "MESSAGE_REACTION_ADD"
eventMessageReactionRemove = "MESSAGE_REACTION_REMOVE"
eventMessageReactionRemoveAll = "MESSAGE_REACTION_REMOVE_ALL"
eventMessageReactionRemoveEmoji = "MESSAGE_REACTION_REMOVE_EMOJI"
eventPresenceUpdate = "PRESENCE_UPDATE"
eventTypingStart = "TYPING_START"
eventUserUpdate = "USER_UPDATE"
eventVoiceStateUpdate = "VOICE_STATE_UPDATE"
eventVoiceServerUpdate = "VOICE_SERVER_UPDATE"
eventWebhooksUpdate = "WEBHOOKS_UPDATE"
)
// NOTE: consider using a map[string]sync.Pool to cache event objects.
// dispatch dispatches events to user handlers, updating the State
// if it is enabled.
func (c *Client) dispatch(typ string, data json.RawMessage) error {
var err error
switch typ {
case eventHello:
case eventReady:
c.connected.Store(true)
var r Ready
if err = json.Unmarshal(data, &r); err != nil {
return fmt.Errorf("unmarshal ready event: %w", err)
}
c.handle(eventReady, &r)
case eventResumed:
c.connected.Store(true)
case eventInvalidSession:
case eventChannelCreate:
var ch discord.Channel
if err = json.Unmarshal(data, &ch); err != nil {
return fmt.Errorf("unmarshal channel create event: %w", err)
}
if c.withStateTracking {
c.State.updateChannel(&ch)
}
c.handle(eventChannelCreate, &ch)
case eventChannelUpdate:
var ch discord.Channel
if err = json.Unmarshal(data, &ch); err != nil {
return fmt.Errorf("unmarshal channel update event: %w", err)
}
if c.withStateTracking {
c.State.updateChannel(&ch)
}
c.handle(eventChannelUpdate, &ch)
case eventChannelDelete:
var ch discord.Channel
if err = json.Unmarshal(data, &ch); err != nil {
return fmt.Errorf("unmarshal channel delete event: %w", err)
}
if c.withStateTracking {
c.State.removeChannel(&ch)
}
c.handle(eventChannelDelete, &ch)
case eventChannelPinsUpdate:
var pins ChannelPinsUpdate
if err = json.Unmarshal(data, &pins); err != nil {
return fmt.Errorf("unmarshal channel pins update event: %w", err)
}
if c.withStateTracking {
c.State.updatePins(&pins)
}
c.handle(eventChannelPinsUpdate, &pins)
case eventGuildCreate:
var g discord.Guild
if err = json.Unmarshal(data, &g); err != nil {
return fmt.Errorf("unmarshal guild create event: %w", err)
}
if c.withStateTracking {
c.State.updateGuild(&g)
}
c.handle(eventGuildCreate, &g)
case eventGuildUpdate:
var g discord.Guild
if err = json.Unmarshal(data, &g); err != nil {
return fmt.Errorf("unmarshal guild update event: %w", err)
}
if c.withStateTracking {
c.State.updateGuild(&g)
}
c.handle(eventGuildUpdate, &g)
case eventGuildDelete:
var g discord.UnavailableGuild
if err = json.Unmarshal(data, &g); err != nil {
return fmt.Errorf("unmarshal guild delete event: %w", err)
}
if c.withStateTracking {
c.State.removeGuild(&g)
}
c.handle(eventGuildDelete, &g)
case eventGuildBanAdd:
var ban GuildBan
if err = json.Unmarshal(data, &ban); err != nil {
return fmt.Errorf("unmarshal ban add event: %w", err)
}
c.handle(eventGuildBanAdd, &ban)
case eventGuildBanRemove:
var ban GuildBan
if err = json.Unmarshal(data, &ban); err != nil {
return fmt.Errorf("unmarshal ban remove event: %w", err)
}
c.handle(eventGuildBanRemove, &ban)
case eventGuildEmojisUpdate:
var ge GuildEmojis
if err = json.Unmarshal(data, &ge); err != nil {
return fmt.Errorf("unmarshal guild emojis update event: %w", err)
}
if c.withStateTracking {
c.State.updateGuildEmojis(ge.GuildID, ge.Emojis)
}
c.handle(eventGuildEmojisUpdate, &ge)
case eventGuildIntegrationsUpdate:
var giu GuildIntegrationUpdate
if err = json.Unmarshal(data, &giu); err != nil {
return fmt.Errorf("unmarshal guild integrations update event: %w", err)
}
c.handle(eventGuildIntegrationsUpdate, &giu)
case eventGuildMemberAdd:
var m GuildMemberAdd
if err = json.Unmarshal(data, &m); err != nil {
return fmt.Errorf("unmarshal guild member add event: %w", err)
}
if c.withStateTracking {
c.State.guildMemberAdd(&m)
}
c.handle(eventGuildMemberAdd, &m)
case eventGuildMemberRemove:
var m GuildMemberRemove
if err = json.Unmarshal(data, &m); err != nil {
return fmt.Errorf("unmarshal guild member remove event: %w", err)
}
if c.withStateTracking {
c.State.guildMemberRemove(&m)
}
c.handle(eventGuildMemberRemove, &m)
case eventGuildMemberUpdate:
var m GuildMemberUpdate
if err = json.Unmarshal(data, &m); err != nil {
return fmt.Errorf("unmarshal guild member update event: %w", err)
}
if c.withStateTracking {
c.State.guildMemberUpdate(&m)
}
c.handle(eventGuildMemberUpdate, &m)
case eventGuildMembersChunk:
var chunk GuildMembersChunk
if err = json.Unmarshal(data, &chunk); err != nil {
return fmt.Errorf("unmarshal guild members chunk event: %w", err)
}
if c.withStateTracking {
for _, m := range chunk.Members {
c.State.guildMemberUpdate(&GuildMemberUpdate{
GuildID: chunk.GuildID,
Roles: m.Roles,
User: m.User,
Nick: m.Nick,
})
}
}
c.handle(eventGuildMembersChunk, &chunk)
case eventGuildRoleCreate:
var gr GuildRole
if err = json.Unmarshal(data, &gr); err != nil {
return fmt.Errorf("unmarshal guild role create event: %w", err)
}
if c.withStateTracking {
c.State.guildRoleCreate(&gr)
}
c.handle(eventGuildRoleCreate, &gr)
case eventGuildRoleUpdate:
var gr GuildRole
if err = json.Unmarshal(data, &gr); err != nil {
return fmt.Errorf("unmarshal guild role update event: %w", err)
}
if c.withStateTracking {
c.State.guildRoleUpdate(&gr)
}
c.handle(eventGuildRoleUpdate, &gr)
case eventGuildRoleDelete:
var gr GuildRoleDelete
if err = json.Unmarshal(data, &gr); err != nil {
return fmt.Errorf("unmarshal guild role delete event: %w", err)
}
if c.withStateTracking {
c.State.guildRoleRemove(&gr)
}
c.handle(eventGuildRoleDelete, &gr)
case eventGuildInviteCreate:
var gic GuildInviteCreate
if err = json.Unmarshal(data, &gic); err != nil {
return fmt.Errorf("unmarshal guild invite create event: %w", err)
}
c.handle(eventGuildInviteCreate, &gic)
case eventGuildInviteDelete:
var gid GuildInviteDelete
if err = json.Unmarshal(data, &gid); err != nil {
return fmt.Errorf("unmarshal guild invite delete event: %w", err)
}
c.handle(eventGuildInviteDelete, &gid)
case eventMessageCreate:
var msg discord.Message
if err = json.Unmarshal(data, &msg); err != nil {
return fmt.Errorf("unmarshal message create event: %w", err)
}
c.handle(eventMessageCreate, &msg)
case eventMessageUpdate:
var msg discord.Message
if err = json.Unmarshal(data, &msg); err != nil {
return fmt.Errorf("unmarshal message update event: %w", err)
}
c.handle(eventMessageUpdate, &msg)
case eventMessageDelete:
var md MessageDelete
if err = json.Unmarshal(data, &md); err != nil {
return fmt.Errorf("unmarshal message delete event: %w", err)
}
c.handle(eventMessageDelete, &md)
case eventMessageDeleteBulk:
var md MessageDeleteBulk
if err = json.Unmarshal(data, &md); err != nil {
return fmt.Errorf("unmarshal message delete bulk event: %w", err)
}
c.handle(eventMessageDeleteBulk, &md)
case eventMessageAck:
var ma MessageAck
if err = json.Unmarshal(data, &ma); err != nil {
return fmt.Errorf("unmarshal message ack event: %w", err)
}
c.handle(eventMessageAck, &ma)
case eventMessageReactionAdd:
var mr MessageReaction
if err = json.Unmarshal(data, &mr); err != nil {
return fmt.Errorf("unmarshal message reaction add event: %w", err)
}
c.handle(eventMessageReactionAdd, &mr)
case eventMessageReactionRemove:
var mr MessageReaction
if err = json.Unmarshal(data, &mr); err != nil {
return fmt.Errorf("unmarshal message reaction remove event: %w", err)
}
c.handle(eventMessageReactionRemove, &mr)
case eventMessageReactionRemoveAll:
var mr MessageReactionRemoveAll
if err = json.Unmarshal(data, &mr); err != nil {
return fmt.Errorf("unmarshal message reaction remove all event: %w", err)
}
c.handle(eventMessageReactionRemoveAll, &mr)
case eventMessageReactionRemoveEmoji:
var m MessageReactionRemoveEmoji
if err = json.Unmarshal(data, &m); err != nil {
return fmt.Errorf("unmarshal message reaction remove emoji event: %w", err)
}
c.handle(eventMessageReactionRemoveEmoji, &m)
case eventPresenceUpdate:
var p discord.Presence
if err = json.Unmarshal(data, &p); err != nil {
return fmt.Errorf("unmarshal presence update event: %w", err)
}
if c.withStateTracking {
c.State.updatePresence(&p)
}
c.handle(eventPresenceUpdate, &p)
case eventTypingStart:
var ts TypingStart
if err = json.Unmarshal(data, &ts); err != nil {
return fmt.Errorf("unmarshal typing start event: %w", err)
}
c.handle(eventTypingStart, &ts)
case eventUserUpdate:
var u discord.User
if err = json.Unmarshal(data, &u); err != nil {
return fmt.Errorf("unmarshal user update event: %w", err)
}
if c.withStateTracking {
c.State.updateUser(&u)
}
c.handle(eventUserUpdate, &u)
case eventVoiceStateUpdate:
var vs voice.StateUpdate
if err = json.Unmarshal(data, &vs); err != nil {
return fmt.Errorf("unmarshal voice state update event: %w", err)
}
// If this update concerns a voice connection managed
// by the Client and it's not a channel leave, make
// sure to update its state so it stays coherent.
// Failing to do so would make this connection try to
// reconnect to a wrong channel or with a wrong state
// (deafen/muted) if it had to reconnect.
if vs.UserID == c.userID && vs.ChannelID != nil {
conn, ok := c.voiceConnections[vs.GuildID]
if ok {
conn.SetState(&vs.State)
}
}
if c.withStateTracking {
c.State.updateGuildVoiceStates(&vs)
}
c.handle(eventVoiceStateUpdate, &vs)
case eventVoiceServerUpdate:
var vs voice.ServerUpdate
if err = json.Unmarshal(data, &vs); err != nil {
return fmt.Errorf("unmarshal voice server update event: %w", err)
}
// If this update concerns a voice connection managed
// by the Client, make sure to update it accordingly
// so it can connect to the new voice server.
if conn, ok := c.voiceConnections[vs.GuildID]; ok {
go func() {
if err = conn.UpdateServer(&vs); err != nil {
c.logger.Errorf("could not update voice server (guild=%q): %v", vs.GuildID, err)
return
}
c.logger.Debugf("successfully update voice server (guild=%q)", vs.GuildID)
}()
}
c.handle(eventVoiceServerUpdate, &vs)
case eventWebhooksUpdate:
var wu WebhooksUpdate
if err = json.Unmarshal(data, &wu); err != nil {
return fmt.Errorf("unmarshal webhooks update event: %w", err)
}
c.handle(eventWebhooksUpdate, &wu)
default:
c.logger.Infof("unrecognized event %s: %s", typ, string(data))
return nil
}
return nil
}
// handle calls the registered user event handler for the given event,
// if there is one.
func (c *Client) handle(event string, d interface{}) {
c.handlersMu.RLock()
h, ok := c.handlers[event]
c.handlersMu.RUnlock()
if ok {
// Call the registered handler in its own goroutine
// so it does not block the dispatcher and events
// can continue to be treated as we receive them.
go h.handle(d)
}
}