forked from andersfylling/disgord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.go
602 lines (472 loc) · 16 KB
/
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
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
package disgord
//go:generate go run generate/events/main.go
// This file contains resource objects for the event reactor
import (
"context"
"sync"
"github.com/andersfylling/disgord/json"
)
// Resource represents a discord event.
// This is used internally for readability only.
type resource = interface{}
// ---------------------------
type EventType interface {
evtResource
}
type evtResource interface {
registerContext(ctx context.Context)
setShardID(id uint)
}
// ---------------------------
// Ready contains the initial state information
type Ready struct {
APIVersion int `json:"v"`
User *User `json:"user"`
Guilds []*GuildUnavailable `json:"guilds"`
// not really needed, as it is handled on the socket layer.
SessionID string `json:"session_id"`
// private_channels will be an empty array. As bots receive private messages,
// they will be notified via Channel Create events.
//PrivateChannels []*channel.Channel `json:"private_channels"`
// bot can't have presences
//Presences []*Presence `json:"presences"`
// bot cant have relationships
//Relationships []interface{} `son:"relationships"`
// bot can't have user settings
// UserSettings interface{} `json:"user_settings"`
sync.RWMutex `json:"-"`
Ctx context.Context `json:"-"`
ShardID uint `json:"-"`
}
// ---------------------------
// Resumed response to Resume
type Resumed struct {
Ctx context.Context `json:"-"`
ShardID uint `json:"-"`
}
// ---------------------------
// ChannelCreate new channel created
type ChannelCreate struct {
Channel *Channel `json:"channel"`
Ctx context.Context `json:"-"`
ShardID uint `json:"-"`
}
// UnmarshalJSON ...
func (obj *ChannelCreate) UnmarshalJSON(data []byte) error {
obj.Channel = &Channel{}
return json.Unmarshal(data, obj.Channel)
}
// ---------------------------
// ChannelUpdate channel was updated
type ChannelUpdate struct {
Channel *Channel `json:"channel"`
Ctx context.Context `json:"-"`
ShardID uint `json:"-"`
}
// UnmarshalJSON ...
func (obj *ChannelUpdate) UnmarshalJSON(data []byte) error {
obj.Channel = &Channel{}
return json.Unmarshal(data, obj.Channel)
}
// ---------------------------
// ChannelDelete channel was deleted
type ChannelDelete struct {
Channel *Channel `json:"channel"`
Ctx context.Context `json:"-"`
ShardID uint `json:"-"`
}
// UnmarshalJSON ...
func (obj *ChannelDelete) UnmarshalJSON(data []byte) error {
obj.Channel = &Channel{}
return json.Unmarshal(data, obj.Channel)
}
// ---------------------------
// ChannelPinsUpdate message was pinned or unpinned
type ChannelPinsUpdate struct {
// ChannelID snowflake the id of the channel
ChannelID Snowflake `json:"channel_id"`
GuildID Snowflake `json:"guild_id,omitempty"`
// LastPinTimestamp ISO8601 timestamp the time at which the most recent pinned message was pinned
LastPinTimestamp Time `json:"last_pin_timestamp,omitempty"`
Ctx context.Context `json:"-"`
ShardID uint `json:"-"`
}
// ---------------------------
// TypingStart user started typing in a channel
type TypingStart struct {
ChannelID Snowflake `json:"channel_id"`
UserID Snowflake `json:"user_id"`
TimestampUnix int `json:"timestamp"`
Ctx context.Context `json:"-"`
ShardID uint `json:"-"`
}
// ---------------------------
// InviteDelete Sent when an invite is deleted.
type InviteDelete struct {
ChannelID Snowflake `json:"channel_id"`
GuildID Snowflake `json:"guild_id"`
Code string `json:"code"`
Ctx context.Context `json:"-"`
ShardID uint `json:"-"`
}
// ---------------------------
// MessageCreate message was created
type MessageCreate struct {
Message *Message
Ctx context.Context `json:"-"`
ShardID uint `json:"-"`
}
var _ Reseter = (*MessageCreate)(nil)
var _ internalUpdater = (*MessageCreate)(nil)
func (obj *MessageCreate) updateInternals() {
obj.Message.updateInternals()
}
// UnmarshalJSON ...
func (obj *MessageCreate) UnmarshalJSON(data []byte) error {
obj.Message = &Message{}
if err := json.Unmarshal(data, obj.Message); err != nil {
return err
}
if obj.Message.Member != nil {
obj.Message.Member.GuildID = obj.Message.GuildID
}
return nil
}
// ---------------------------
// MessageUpdate message was edited
type MessageUpdate struct {
Message *Message
Ctx context.Context `json:"-"`
ShardID uint `json:"-"`
}
var _ internalUpdater = (*MessageUpdate)(nil)
func (obj *MessageUpdate) updateInternals() {
obj.Message.updateInternals()
}
// UnmarshalJSON ...
func (obj *MessageUpdate) UnmarshalJSON(data []byte) error {
obj.Message = &Message{}
if err := json.Unmarshal(data, obj.Message); err != nil {
return err
}
if obj.Message.Member != nil {
obj.Message.Member.GuildID = obj.Message.GuildID
}
return nil
}
// ---------------------------
// MessageDelete message was deleted
type MessageDelete struct {
MessageID Snowflake `json:"id"`
ChannelID Snowflake `json:"channel_id"`
GuildID Snowflake `json:"guild_id,omitempty"`
Ctx context.Context `json:"-"`
ShardID uint `json:"-"`
}
// ---------------------------
// MessageDeleteBulk multiple messages were deleted at once
type MessageDeleteBulk struct {
MessageIDs []Snowflake `json:"ids"`
ChannelID Snowflake `json:"channel_id"`
Ctx context.Context `json:"-"`
ShardID uint `json:"-"`
}
// ---------------------------
// MessageReactionAdd user reacted to a message
// Note! do not cache emoji, unless it's updated with guildID
// TODO: find guildID when given UserID, ChannelID and MessageID
type MessageReactionAdd struct {
UserID Snowflake `json:"user_id"`
ChannelID Snowflake `json:"channel_id"`
MessageID Snowflake `json:"message_id"`
// PartialEmoji id and name. id might be nil
PartialEmoji *Emoji `json:"emoji"`
Ctx context.Context `json:"-"`
ShardID uint `json:"-"`
}
// ---------------------------
// MessageReactionRemove user removed a reaction from a message
// Note! do not cache emoji, unless it's updated with guildID
// TODO: find guildID when given UserID, ChannelID and MessageID
type MessageReactionRemove struct {
UserID Snowflake `json:"user_id"`
ChannelID Snowflake `json:"channel_id"`
MessageID Snowflake `json:"message_id"`
// PartialEmoji id and name. id might be nil
PartialEmoji *Emoji `json:"emoji"`
Ctx context.Context `json:"-"`
ShardID uint `json:"-"`
}
// ---------------------------
// MessageReactionRemoveAll all reactions were explicitly removed from a message
type MessageReactionRemoveAll struct {
ChannelID Snowflake `json:"channel_id"`
MessageID Snowflake `json:"message_id"`
Ctx context.Context `json:"-"`
ShardID uint `json:"-"`
}
// ---------------------------
// GuildEmojisUpdate guild emojis were updated
type GuildEmojisUpdate struct {
GuildID Snowflake `json:"guild_id"`
Emojis []*Emoji `json:"emojis"`
Ctx context.Context `json:"-"`
ShardID uint `json:"-"`
}
var _ internalUpdater = (*GuildEmojisUpdate)(nil)
func (g *GuildEmojisUpdate) updateInternals() {
for i := range g.Emojis {
g.Emojis[i].guildID = g.GuildID
}
}
// ---------------------------
// GuildCreate This event can be sent in three different scenarios:
// 1. When a user is initially connecting, to lazily load and backfill information for all unavailable Guilds
// sent in the Ready event.
// 2. When a Guild becomes available again to the Client.
// 3. When the current user joins a new Guild.
type GuildCreate struct {
Guild *Guild `json:"guild"`
Ctx context.Context `json:"-"`
ShardID uint `json:"-"`
}
var _ internalUpdater = (*GuildCreate)(nil)
func (g *GuildCreate) updateInternals() {
g.Guild.updateInternals()
}
// UnmarshalJSON ...
func (obj *GuildCreate) UnmarshalJSON(data []byte) error {
obj.Guild = &Guild{}
if err := json.Unmarshal(data, obj.Guild); err != nil {
return err
}
for _, v := range obj.Guild.Members {
v.GuildID = obj.Guild.ID
}
return nil
}
// ---------------------------
// GuildUpdate guild was updated
type GuildUpdate struct {
Guild *Guild `json:"guild"`
Ctx context.Context `json:"-"`
ShardID uint `json:"-"`
}
var _ internalUpdater = (*GuildUpdate)(nil)
func (g *GuildUpdate) updateInternals() {
g.Guild.updateInternals()
}
// UnmarshalJSON ...
func (obj *GuildUpdate) UnmarshalJSON(data []byte) error {
obj.Guild = &Guild{}
return json.Unmarshal(data, obj.Guild)
}
// ---------------------------
// GuildDelete guild became unavailable, or user left/was removed from a guild
type GuildDelete struct {
UnavailableGuild *GuildUnavailable `json:"guild_unavailable"`
Ctx context.Context `json:"-"`
ShardID uint `json:"-"`
}
// UserWasRemoved ... TODO
func (obj *GuildDelete) UserWasRemoved() bool {
return obj.UnavailableGuild.Unavailable == false
}
// UnmarshalJSON ...
func (obj *GuildDelete) UnmarshalJSON(data []byte) error {
obj.UnavailableGuild = &GuildUnavailable{}
return json.Unmarshal(data, obj.UnavailableGuild)
}
// ---------------------------
// GuildBanAdd user was banned from a guild
type GuildBanAdd struct {
GuildID Snowflake `json:"guild_id"`
User *User `json:"user"`
Ctx context.Context `json:"-"`
ShardID uint `json:"-"`
}
// ---------------------------
// GuildBanRemove user was unbanned from a guild
type GuildBanRemove struct {
GuildID Snowflake `json:"guild_id"`
User *User `json:"user"`
Ctx context.Context `json:"-"`
ShardID uint `json:"-"`
}
// ---------------------------
// GuildIntegrationsUpdate guild integration was updated
type GuildIntegrationsUpdate struct {
GuildID Snowflake `json:"guild_id"`
Ctx context.Context `json:"-"`
ShardID uint `json:"-"`
}
// ---------------------------
// GuildMemberAdd new user joined a guild
type GuildMemberAdd struct {
Member *Member `json:"member"`
Ctx context.Context `json:"-"`
ShardID uint `json:"-"`
}
var _ internalUpdater = (*GuildMemberAdd)(nil)
func (g *GuildMemberAdd) updateInternals() {
g.Member.updateInternals()
}
// UnmarshalJSON ...
func (obj *GuildMemberAdd) UnmarshalJSON(data []byte) error {
obj.Member = &Member{}
return json.Unmarshal(data, obj.Member)
}
// ---------------------------
// GuildMemberRemove user was removed from a guild
type GuildMemberRemove struct {
GuildID Snowflake `json:"guild_id"`
User *User `json:"user"`
Ctx context.Context `json:"-"`
ShardID uint `json:"-"`
}
// ---------------------------
// GuildMemberUpdate guild member was updated
type GuildMemberUpdate struct {
GuildID Snowflake `json:"guild_id"`
Roles []Snowflake `json:"roles"`
User *User `json:"user"`
Nick string `json:"nick"`
Ctx context.Context `json:"-"`
ShardID uint `json:"-"`
}
// ---------------------------
// GuildMembersChunk response to Request Guild Members
type GuildMembersChunk struct {
GuildID Snowflake `json:"guild_id"`
Members []*Member `json:"members"`
Ctx context.Context `json:"-"`
ShardID uint `json:"-"`
}
var _ internalUpdater = (*GuildMembersChunk)(nil)
func (g *GuildMembersChunk) updateInternals() {
for i := range g.Members {
g.Members[i].updateInternals()
}
}
// ---------------------------
// GuildRoleCreate guild role was created
type GuildRoleCreate struct {
GuildID Snowflake `json:"guild_id"`
Role *Role `json:"role"`
Ctx context.Context `json:"-"`
ShardID uint `json:"-"`
}
var _ internalUpdater = (*GuildRoleCreate)(nil)
func (g *GuildRoleCreate) updateInternals() {
g.Role.guildID = g.GuildID
}
// ---------------------------
// GuildRoleUpdate guild role was updated
type GuildRoleUpdate struct {
GuildID Snowflake `json:"guild_id"`
Role *Role `json:"role"`
Ctx context.Context `json:"-"`
ShardID uint `json:"-"`
}
var _ internalUpdater = (*GuildRoleUpdate)(nil)
func (g *GuildRoleUpdate) updateInternals() {
g.Role.guildID = g.GuildID
}
// ---------------------------
// GuildRoleDelete a guild role was deleted
type GuildRoleDelete struct {
GuildID Snowflake `json:"guild_id"`
RoleID Snowflake `json:"role_id"`
Ctx context.Context `json:"-"`
ShardID uint `json:"-"`
}
// ---------------------------
// PresenceUpdate user's presence was updated in a guild
type PresenceUpdate struct {
User *User `json:"user"`
RoleIDs []Snowflake `json:"roles"`
Game *Activity `json:"game"`
GuildID Snowflake `json:"guild_id"`
Activities []*Activity `json:"activities"`
// Status either "idle", "dnd", "online", or "offline"
// TODO: constants somewhere..
Status string `json:"status"`
Ctx context.Context `json:"-"`
ShardID uint `json:"-"`
}
// ---------------------------
// UserUpdate properties about a user changed
type UserUpdate struct {
*User
Ctx context.Context `json:"-"`
ShardID uint `json:"-"`
}
// UnmarshalJSON ...
func (obj *UserUpdate) UnmarshalJSON(data []byte) error {
obj.User = &User{}
return json.Unmarshal(data, obj.User)
}
// ---------------------------
// VoiceStateUpdate someone joined, left, or moved a voice channel
type VoiceStateUpdate struct {
*VoiceState
Ctx context.Context `json:"-"`
ShardID uint `json:"-"`
}
// UnmarshalJSON ...
func (h *VoiceStateUpdate) UnmarshalJSON(data []byte) error {
h.VoiceState = &VoiceState{}
return json.Unmarshal(data, h.VoiceState)
}
// ---------------------------
// VoiceServerUpdate guild's voice server was updated. Sent when a guild's voice server is updated. This is sent when initially
// connecting to voice, and when the current voice instance fails over to a new server.
type VoiceServerUpdate struct {
Token string `json:"token"`
GuildID Snowflake `json:"guild_id"`
Endpoint string `json:"endpoint"`
Ctx context.Context `json:"-"`
ShardID uint `json:"-"`
}
// ---------------------------
// WebhooksUpdate guild channel webhook was created, update, or deleted
type WebhooksUpdate struct {
GuildID Snowflake `json:"guild_id"`
ChannelID Snowflake `json:"channel_id"`
Ctx context.Context `json:"-"`
ShardID uint `json:"-"`
}
// InviteCreate guild invite was created
type InviteCreate struct {
// Code the invite code (unique Snowflake)
Code string `json:"code"`
// GuildID the guild this invite is for
GuildID Snowflake `json:"guild_id,omitempty"`
// ChannelID the channel this invite is for
ChannelID Snowflake `json:"channel_id"`
// Inviter the user that created the invite
Inviter *User `json:"inviter"`
// Target the target user for this invite
Target *User `json:"target_user,omitempty"`
// TargetType the type of user target for this invite
// 1 STREAM (currently the STREAM only)
TargetType int `json:"target_user_type"`
// CreatedAt the time at which the invite was created
CreatedAt Time `json:"created_at"`
// MaxAge how long the invite is valid for (in seconds)
MaxAge int `json:"max_age"`
// MaxUses the maximum number of times the invite can be used
MaxUses int `json:"max_uses"`
// Temporary whether or not the invite is temporary (invited Users will be kicked on disconnect unless they're assigned a role)
Temporary bool `json:"temporary"`
// Uses how many times the invite has been used (always will be 0)
Uses int `json:"uses"`
Revoked bool `json:"revoked"`
Unique bool `json:"unique"`
// ApproximatePresenceCount approximate count of online members
ApproximatePresenceCount int `json:"approximate_presence_count,omitempty"`
// ApproximatePresenceCount approximate count of total members
ApproximateMemberCount int `json:"approximate_member_count,omitempty"`
Ctx context.Context `json:"-"`
ShardID uint `json:"-"`
}