-
Notifications
You must be signed in to change notification settings - Fork 563
/
event.rs
1226 lines (1102 loc) · 37.6 KB
/
event.rs
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
//! All the events this library handles.
use std::collections::{BTreeMap, HashMap};
use super::utils::*;
use super::*;
use ::constants::{OpCode, VoiceOpCode};
use ::internal::prelude::*;
use ::utils::decode_array;
type Map = BTreeMap<String, Value>;
#[derive(Clone, Debug)]
pub struct CallCreateEvent {
pub call: Call,
}
impl CallCreateEvent {
#[doc(hidden)]
#[inline]
pub fn decode(map: Map) -> Result<Self> {
Ok(CallCreateEvent {
call: Call::decode(Value::Object(map))?,
})
}
}
#[derive(Clone, Debug)]
pub struct CallDeleteEvent {
pub channel_id: ChannelId,
}
impl CallDeleteEvent {
#[doc(hidden)]
#[inline]
pub fn decode(mut map: Map) -> Result<Self> {
Ok(CallDeleteEvent {
channel_id: remove(&mut map, "channel_id").and_then(ChannelId::decode)?,
})
}
}
#[derive(Clone, Debug)]
pub struct CallUpdateEvent {
pub channel_id: ChannelId,
pub message_id: MessageId,
pub region: String,
pub ringing: Vec<UserId>,
}
impl CallUpdateEvent {
#[doc(hidden)]
#[inline]
pub fn decode(mut map: Map) -> Result<Self> {
Ok(CallUpdateEvent {
channel_id: remove(&mut map, "channel_id").and_then(ChannelId::decode)?,
message_id: remove(&mut map, "message_id").and_then(MessageId::decode)?,
region: remove(&mut map, "region").and_then(into_string)?,
ringing: decode_array(remove(&mut map, "ringing")?, UserId::decode)?,
})
}
}
#[derive(Clone, Debug)]
pub struct ChannelCreateEvent {
pub channel: Channel,
}
impl ChannelCreateEvent {
#[doc(hidden)]
#[inline]
pub fn decode(map: Map) -> Result<Self> {
Ok(ChannelCreateEvent {
channel: Channel::decode(Value::Object(map))?,
})
}
}
#[derive(Clone, Debug)]
pub struct ChannelDeleteEvent {
pub channel: Channel,
}
impl ChannelDeleteEvent {
#[doc(hidden)]
#[inline]
pub fn decode(map: Map) -> Result<Self> {
Ok(ChannelDeleteEvent {
channel: Channel::decode(Value::Object(map))?,
})
}
}
#[derive(Clone, Debug)]
pub struct ChannelPinsAckEvent {
pub channel_id: ChannelId,
pub timestamp: String,
}
impl ChannelPinsAckEvent {
#[doc(hidden)]
#[inline]
pub fn decode(mut map: Map) -> Result<Self> {
Ok(ChannelPinsAckEvent {
channel_id: remove(&mut map, "channel_id").and_then(ChannelId::decode)?,
timestamp: remove(&mut map, "timestamp").and_then(into_string)?,
})
}
}
#[derive(Clone, Debug)]
pub struct ChannelPinsUpdateEvent {
pub channel_id: ChannelId,
pub last_pin_timestamp: Option<String>,
}
impl ChannelPinsUpdateEvent {
#[doc(hidden)]
#[inline]
pub fn decode(mut map: Map) -> Result<Self> {
Ok(ChannelPinsUpdateEvent {
channel_id: remove(&mut map, "channel_id").and_then(ChannelId::decode)?,
last_pin_timestamp: opt(&mut map, "last_pin_timestamp", into_string)?,
})
}
}
#[derive(Clone, Debug)]
pub struct ChannelRecipientAddEvent {
pub channel_id: ChannelId,
pub user: User,
}
impl ChannelRecipientAddEvent {
#[doc(hidden)]
#[inline]
pub fn decode(mut map: Map) -> Result<Self> {
Ok(ChannelRecipientAddEvent {
channel_id: remove(&mut map, "channel_id").and_then(ChannelId::decode)?,
user: remove(&mut map, "user").and_then(User::decode)?,
})
}
}
#[derive(Clone, Debug)]
pub struct ChannelRecipientRemoveEvent {
pub channel_id: ChannelId,
pub user: User,
}
impl ChannelRecipientRemoveEvent {
#[doc(hidden)]
#[inline]
pub fn decode(mut map: Map) -> Result<Self> {
Ok(ChannelRecipientRemoveEvent {
channel_id: remove(&mut map, "channel_id").and_then(ChannelId::decode)?,
user: remove(&mut map, "user").and_then(User::decode)?,
})
}
}
#[derive(Clone, Debug)]
pub struct ChannelUpdateEvent {
pub channel: Channel,
}
impl ChannelUpdateEvent {
#[doc(hidden)]
#[inline]
pub fn decode(map: Map) -> Result<Self> {
Ok(ChannelUpdateEvent {
channel: Channel::decode(Value::Object(map))?,
})
}
}
#[derive(Clone, Debug)]
pub struct FriendSuggestionCreateEvent {
pub reasons: Vec<SuggestionReason>,
pub suggested_user: User,
}
impl FriendSuggestionCreateEvent {
#[doc(hidden)]
#[inline]
pub fn decode(mut map: Map) -> Result<Self> {
Ok(FriendSuggestionCreateEvent {
reasons: decode_array(remove(&mut map, "reasons")?, SuggestionReason::decode)?,
suggested_user: remove(&mut map, "suggested_user").and_then(User::decode)?,
})
}
}
#[derive(Clone, Copy, Debug)]
pub struct FriendSuggestionDeleteEvent {
pub suggested_user_id: UserId,
}
impl FriendSuggestionDeleteEvent {
#[doc(hidden)]
#[inline]
pub fn decode(mut map: Map) -> Result<Self> {
Ok(FriendSuggestionDeleteEvent {
suggested_user_id: remove(&mut map, "suggested_user_id").and_then(UserId::decode)?,
})
}
}
#[derive(Clone, Debug)]
pub struct GuildBanAddEvent {
pub guild_id: GuildId,
pub user: User,
}
impl GuildBanAddEvent {
#[doc(hidden)]
#[inline]
pub fn decode(mut map: Map) -> Result<Self> {
Ok(GuildBanAddEvent {
guild_id: remove(&mut map, "guild_id").and_then(GuildId::decode)?,
user: remove(&mut map, "user").and_then(User::decode)?,
})
}
}
#[derive(Clone, Debug)]
pub struct GuildBanRemoveEvent {
pub guild_id: GuildId,
pub user: User,
}
impl GuildBanRemoveEvent {
#[doc(hidden)]
#[inline]
pub fn decode(mut map: Map) -> Result<Self> {
Ok(GuildBanRemoveEvent {
guild_id: remove(&mut map, "guild_id").and_then(GuildId::decode)?,
user: remove(&mut map, "user").and_then(User::decode)?,
})
}
}
#[derive(Clone, Debug)]
pub struct GuildCreateEvent {
pub guild: Guild,
}
impl GuildCreateEvent {
#[doc(hidden)]
#[inline]
pub fn decode(map: Map) -> Result<Self> {
Ok(GuildCreateEvent {
guild: Guild::decode(Value::Object(map))?,
})
}
}
#[derive(Clone, Debug)]
pub struct GuildDeleteEvent {
pub guild: PartialGuild,
}
impl GuildDeleteEvent {
#[doc(hidden)]
#[inline]
pub fn decode(map: Map) -> Result<Self> {
Ok(GuildDeleteEvent {
guild: PartialGuild::decode(Value::Object(map))?,
})
}
}
#[derive(Clone, Debug)]
pub struct GuildEmojisUpdateEvent {
pub emojis: HashMap<EmojiId, Emoji>,
pub guild_id: GuildId,
}
impl GuildEmojisUpdateEvent {
#[doc(hidden)]
#[inline]
pub fn decode(mut map: Map) -> Result<Self> {
Ok(GuildEmojisUpdateEvent {
emojis: remove(&mut map, "emojis").and_then(decode_emojis)?,
guild_id: remove(&mut map, "guild_id").and_then(GuildId::decode)?,
})
}
}
#[derive(Clone, Debug)]
pub struct GuildIntegrationsUpdateEvent {
pub guild_id: GuildId,
}
impl GuildIntegrationsUpdateEvent {
#[doc(hidden)]
#[inline]
pub fn decode(mut map: Map) -> Result<Self> {
Ok(GuildIntegrationsUpdateEvent {
guild_id: remove(&mut map, "guild_id").and_then(GuildId::decode)?,
})
}
}
#[derive(Clone, Debug)]
pub struct GuildMemberAddEvent {
pub guild_id: GuildId,
pub member: Member,
}
impl GuildMemberAddEvent {
#[doc(hidden)]
#[inline]
pub fn decode(mut map: Map) -> Result<Self> {
Ok(GuildMemberAddEvent {
guild_id: remove(&mut map, "guild_id").and_then(GuildId::decode)?,
member: Member::decode(Value::Object(map))?,
})
}
}
#[derive(Clone, Debug)]
pub struct GuildMemberRemoveEvent {
pub guild_id: GuildId,
pub user: User,
}
impl GuildMemberRemoveEvent {
#[doc(hidden)]
#[inline]
pub fn decode(mut map: Map) -> Result<Self> {
Ok(GuildMemberRemoveEvent {
guild_id: remove(&mut map, "guild_id").and_then(GuildId::decode)?,
user: remove(&mut map, "user").and_then(User::decode)?,
})
}
}
#[derive(Clone, Debug)]
pub struct GuildMemberUpdateEvent {
pub guild_id: GuildId,
pub nick: Option<String>,
pub roles: Vec<RoleId>,
pub user: User,
}
impl GuildMemberUpdateEvent {
#[doc(hidden)]
#[inline]
pub fn decode(mut map: Map) -> Result<Self> {
Ok(GuildMemberUpdateEvent {
guild_id: remove(&mut map, "guild_id").and_then(GuildId::decode)?,
nick: opt(&mut map, "nick", into_string)?,
roles: decode_array(remove(&mut map, "roles")?, RoleId::decode)?,
user: remove(&mut map, "user").and_then(User::decode)?,
})
}
}
#[derive(Clone, Debug)]
pub struct GuildMembersChunkEvent {
pub guild_id: GuildId,
pub members: HashMap<UserId, Member>,
}
impl GuildMembersChunkEvent {
#[doc(hidden)]
#[inline]
pub fn decode(mut map: Map) -> Result<Self> {
Ok(GuildMembersChunkEvent {
guild_id: remove(&mut map, "guild_id").and_then(GuildId::decode)?,
members: remove(&mut map, "members").and_then(decode_members)?,
})
}
}
#[derive(Clone, Debug)]
pub struct GuildRoleCreateEvent {
pub guild_id: GuildId,
pub role: Role,
}
impl GuildRoleCreateEvent {
#[doc(hidden)]
#[inline]
pub fn decode(mut map: Map) -> Result<Self> {
Ok(GuildRoleCreateEvent {
guild_id: remove(&mut map, "guild_id").and_then(GuildId::decode)?,
role: remove(&mut map, "role").and_then(Role::decode)?,
})
}
}
#[derive(Clone, Debug)]
pub struct GuildRoleDeleteEvent {
pub guild_id: GuildId,
pub role_id: RoleId,
}
impl GuildRoleDeleteEvent {
#[doc(hidden)]
#[inline]
pub fn decode(mut map: Map) -> Result<Self> {
Ok(GuildRoleDeleteEvent {
guild_id: remove(&mut map, "guild_id").and_then(GuildId::decode)?,
role_id: remove(&mut map, "role_id").and_then(RoleId::decode)?,
})
}
}
#[derive(Clone, Debug)]
pub struct GuildRoleUpdateEvent {
pub guild_id: GuildId,
pub role: Role,
}
impl GuildRoleUpdateEvent {
#[doc(hidden)]
#[inline]
pub fn decode(mut map: Map) -> Result<Self> {
Ok(GuildRoleUpdateEvent {
guild_id: remove(&mut map, "guild_id").and_then(GuildId::decode)?,
role: remove(&mut map, "role").and_then(Role::decode)?,
})
}
}
#[derive(Clone, Debug)]
pub struct GuildSyncEvent {
pub guild_id: GuildId,
pub large: bool,
pub members: HashMap<UserId, Member>,
pub presences: HashMap<UserId, Presence>,
}
impl GuildSyncEvent {
#[doc(hidden)]
#[inline]
pub fn decode(mut map: Map) -> Result<Self> {
Ok(GuildSyncEvent {
guild_id: remove(&mut map, "id").and_then(GuildId::decode)?,
large: req!(remove(&mut map, "large")?.as_bool()),
members: remove(&mut map, "members").and_then(decode_members)?,
presences: remove(&mut map, "presences").and_then(decode_presences)?,
})
}
}
#[derive(Clone, Debug)]
pub struct GuildUnavailableEvent {
pub guild_id: GuildId,
}
impl GuildUnavailableEvent {
#[doc(hidden)]
#[inline]
pub fn decode(mut map: Map) -> Result<Self> {
Ok(GuildUnavailableEvent {
guild_id: remove(&mut map, "id").and_then(GuildId::decode)?,
})
}
}
#[derive(Clone, Debug)]
pub struct GuildUpdateEvent {
pub guild: PartialGuild,
}
impl GuildUpdateEvent {
#[doc(hidden)]
#[inline]
pub fn decode(map: Map) -> Result<Self> {
Ok(GuildUpdateEvent {
guild: PartialGuild::decode(Value::Object(map))?,
})
}
}
#[derive(Clone, Copy, Debug)]
pub struct MessageAckEvent {
pub channel_id: ChannelId,
/// May be `None` if a private channel with no messages has closed.
pub message_id: Option<MessageId>,
}
impl MessageAckEvent {
#[doc(hidden)]
#[inline]
pub fn decode(mut map: Map) -> Result<Self> {
Ok(MessageAckEvent {
channel_id: remove(&mut map, "channel_id").and_then(ChannelId::decode)?,
message_id: opt(&mut map, "message_id", MessageId::decode)?,
})
}
}
#[derive(Clone, Debug)]
pub struct MessageCreateEvent {
pub message: Message,
}
impl MessageCreateEvent {
#[doc(hidden)]
#[inline]
pub fn decode(map: Map) -> Result<Self> {
Ok(MessageCreateEvent {
message: Message::decode(Value::Object(map))?,
})
}
}
#[derive(Clone, Debug)]
pub struct MessageDeleteBulkEvent {
pub channel_id: ChannelId,
pub ids: Vec<MessageId>,
}
impl MessageDeleteBulkEvent {
#[doc(hidden)]
#[inline]
pub fn decode(mut map: Map) -> Result<Self> {
Ok(MessageDeleteBulkEvent {
channel_id: remove(&mut map, "channel_id").and_then(ChannelId::decode)?,
ids: decode_array(remove(&mut map, "ids")?, MessageId::decode)?,
})
}
}
#[derive(Clone, Copy, Debug)]
pub struct MessageDeleteEvent {
pub channel_id: ChannelId,
pub message_id: MessageId,
}
impl MessageDeleteEvent {
#[doc(hidden)]
#[inline]
pub fn decode(mut map: Map) -> Result<Self> {
Ok(MessageDeleteEvent {
channel_id: remove(&mut map, "channel_id").and_then(ChannelId::decode)?,
message_id: remove(&mut map, "id").and_then(MessageId::decode)?,
})
}
}
#[derive(Clone, Debug)]
pub struct MessageUpdateEvent {
pub id: MessageId,
pub channel_id: ChannelId,
pub kind: Option<MessageType>,
pub content: Option<String>,
pub nonce: Option<String>,
pub tts: Option<bool>,
pub pinned: Option<bool>,
pub timestamp: Option<String>,
pub edited_timestamp: Option<String>,
pub author: Option<User>,
pub mention_everyone: Option<bool>,
pub mentions: Option<Vec<User>>,
pub mention_roles: Option<Vec<RoleId>>,
pub attachments: Option<Vec<Attachment>>,
pub embeds: Option<Vec<Value>>,
}
impl MessageUpdateEvent {
#[doc(hidden)]
#[inline]
pub fn decode(mut map: Map) -> Result<Self> {
Ok(MessageUpdateEvent {
id: remove(&mut map, "id").and_then(MessageId::decode)?,
channel_id: remove(&mut map, "channel_id").and_then(ChannelId::decode)?,
kind: opt(&mut map, "type", MessageType::decode)?,
content: opt(&mut map, "content", into_string)?,
nonce: remove(&mut map, "nonce").and_then(into_string).ok(),
tts: remove(&mut map, "tts").ok().and_then(|v| v.as_bool()),
pinned: remove(&mut map, "pinned").ok().and_then(|v| v.as_bool()),
timestamp: opt(&mut map, "timestamp", into_string)?,
edited_timestamp: opt(&mut map, "edited_timestamp", into_string)?,
author: opt(&mut map, "author", User::decode)?,
mention_everyone: remove(&mut map, "mention_everyone").ok().and_then(|v| v.as_bool()),
mentions: opt(&mut map, "mentions", |v| decode_array(v, User::decode))?,
mention_roles: opt(&mut map, "mention_roles", |v| decode_array(v, RoleId::decode))?,
attachments: opt(&mut map, "attachments", |v| decode_array(v, Attachment::decode))?,
embeds: opt(&mut map, "embeds", |v| decode_array(v, Ok))?,
})
}
}
#[derive(Clone, Debug)]
pub struct PresenceUpdateEvent {
pub guild_id: Option<GuildId>,
pub presence: Presence,
pub roles: Option<Vec<RoleId>>,
}
impl PresenceUpdateEvent {
#[doc(hidden)]
#[inline]
pub fn decode(mut map: Map) -> Result<Self> {
let guild_id = opt(&mut map, "guild_id", GuildId::decode)?;
let roles = opt(&mut map, "roles", |v| decode_array(v, RoleId::decode))?;
let presence = Presence::decode(Value::Object(map))?;
Ok(PresenceUpdateEvent {
guild_id: guild_id,
presence: presence,
roles: roles,
})
}
}
#[derive(Clone, Debug)]
pub struct PresencesReplaceEvent {
pub presences: Vec<Presence>,
}
impl PresencesReplaceEvent {
#[doc(hidden)]
#[inline]
pub fn decode(value: Value) -> Result<Self> {
Ok(PresencesReplaceEvent {
presences: decode_array(value, Presence::decode)?,
})
}
}
#[derive(Clone, Debug)]
pub struct ReactionAddEvent {
pub reaction: Reaction,
}
impl ReactionAddEvent {
#[doc(hidden)]
#[inline]
pub fn decode(map: Map) -> Result<Self> {
Ok(ReactionAddEvent {
reaction: Reaction::decode(Value::Object(map))?
})
}
}
#[derive(Clone, Debug)]
pub struct ReactionRemoveEvent {
pub reaction: Reaction,
}
impl ReactionRemoveEvent {
#[doc(hidden)]
#[inline]
pub fn decode(map: Map) -> Result<Self> {
Ok(ReactionRemoveEvent {
reaction: Reaction::decode(Value::Object(map))?
})
}
}
#[derive(Clone, Copy, Debug)]
pub struct ReactionRemoveAllEvent {
pub channel_id: ChannelId,
pub message_id: MessageId,
}
impl ReactionRemoveAllEvent {
#[doc(hidden)]
#[inline]
pub fn decode(mut map: Map) -> Result<Self> {
Ok(ReactionRemoveAllEvent {
channel_id: remove(&mut map, "channel_id").and_then(ChannelId::decode)?,
message_id: remove(&mut map, "message_id").and_then(MessageId::decode)?,
})
}
}
/// The "Ready" event, containing initial ready cache
#[derive(Clone, Debug)]
pub struct ReadyEvent {
pub ready: Ready,
}
impl ReadyEvent {
#[doc(hidden)]
#[inline]
pub fn decode(map: Map) -> Result<Self> {
Ok(ReadyEvent {
ready: Ready::decode(Value::Object(map))?,
})
}
}
#[derive(Clone, Debug)]
pub struct RelationshipAddEvent {
pub relationship: Relationship,
}
impl RelationshipAddEvent {
#[doc(hidden)]
#[inline]
pub fn decode(map: Map) -> Result<Self> {
Ok(RelationshipAddEvent {
relationship: Relationship::decode(Value::Object(map))?,
})
}
}
#[derive(Clone, Copy, Debug)]
pub struct RelationshipRemoveEvent {
pub kind: RelationshipType,
pub user_id: UserId,
}
impl RelationshipRemoveEvent {
#[doc(hidden)]
#[inline]
pub fn decode(mut map: Map) -> Result<Self> {
Ok(RelationshipRemoveEvent {
kind: remove(&mut map, "type").and_then(RelationshipType::decode)?,
user_id: remove(&mut map, "id").and_then(UserId::decode)?,
})
}
}
#[derive(Clone, Debug)]
pub struct ResumedEvent {
pub trace: Vec<Option<String>>,
}
impl ResumedEvent {
#[doc(hidden)]
#[inline]
pub fn decode(mut map: Map) -> Result<Self> {
Ok(ResumedEvent {
trace: remove(&mut map, "_trace").and_then(|v| decode_array(v, |v| Ok(into_string(v).ok())))?,
})
}
}
#[derive(Clone, Debug)]
pub struct TypingStartEvent {
pub channel_id: ChannelId,
pub timestamp: u64,
pub user_id: UserId,
}
impl TypingStartEvent {
#[doc(hidden)]
#[inline]
pub fn decode(mut map: Map) -> Result<Self> {
Ok(TypingStartEvent {
channel_id: remove(&mut map, "channel_id").and_then(ChannelId::decode)?,
timestamp: req!(remove(&mut map, "timestamp")?.as_u64()),
user_id: remove(&mut map, "user_id").and_then(UserId::decode)?,
})
}
}
#[derive(Clone, Debug)]
pub struct UnknownEvent {
pub kind: String,
pub value: BTreeMap<String, Value>
}
#[derive(Clone, Debug)]
pub struct UserGuildSettingsUpdateEvent {
pub settings: UserGuildSettings,
}
impl UserGuildSettingsUpdateEvent {
#[doc(hidden)]
#[inline]
pub fn decode(map: Map) -> Result<Self> {
Ok(UserGuildSettingsUpdateEvent {
settings: UserGuildSettings::decode(Value::Object(map))?,
})
}
}
#[derive(Clone, Debug)]
pub struct UserNoteUpdateEvent {
pub note: String,
pub user_id: UserId,
}
impl UserNoteUpdateEvent {
#[doc(hidden)]
#[inline]
pub fn decode(mut map: Map) -> Result<Self> {
Ok(UserNoteUpdateEvent {
note: remove(&mut map, "note").and_then(into_string)?,
user_id: remove(&mut map, "id").and_then(UserId::decode)?,
})
}
}
#[derive(Clone, Debug)]
pub struct UserUpdateEvent {
pub current_user: CurrentUser,
}
impl UserUpdateEvent {
#[doc(hidden)]
#[inline]
pub fn decode(map: Map) -> Result<Self> {
Ok(UserUpdateEvent {
current_user: CurrentUser::decode(Value::Object(map))?,
})
}
}
#[derive(Clone, Debug)]
pub struct UserSettingsUpdateEvent {
pub enable_tts_command: Option<bool>,
pub inline_attachment_media: Option<bool>,
pub inline_embed_media: Option<bool>,
pub locale: Option<String>,
pub message_display_compact: Option<bool>,
pub render_embeds: Option<bool>,
pub show_current_game: Option<bool>,
pub theme: Option<String>,
pub convert_emoticons: Option<bool>,
pub friend_source_flags: Option<FriendSourceFlags>,
}
impl UserSettingsUpdateEvent {
#[doc(hidden)]
#[inline]
pub fn decode(mut map: Map) -> Result<Self> {
Ok(UserSettingsUpdateEvent {
enable_tts_command: remove(&mut map, "enable_tts_command").ok().and_then(|v| v.as_bool()),
inline_attachment_media: remove(&mut map, "inline_attachment_media").ok().and_then(|v| v.as_bool()),
inline_embed_media: remove(&mut map, "inline_embed_media").ok().and_then(|v| v.as_bool()),
locale: opt(&mut map, "locale", into_string)?,
message_display_compact: remove(&mut map, "message_display_compact").ok().and_then(|v| v.as_bool()),
render_embeds: remove(&mut map, "render_embeds").ok().and_then(|v| v.as_bool()),
show_current_game: remove(&mut map, "show_current_game").ok().and_then(|v| v.as_bool()),
theme: opt(&mut map, "theme", into_string)?,
convert_emoticons: remove(&mut map, "convert_emoticons").ok().and_then(|v| v.as_bool()),
friend_source_flags: opt(&mut map, "friend_source_flags", FriendSourceFlags::decode)?,
})
}
}
#[derive(Clone, Debug)]
pub struct VoiceServerUpdateEvent {
pub channel_id: Option<ChannelId>,
pub endpoint: Option<String>,
pub guild_id: Option<GuildId>,
pub token: String,
}
impl VoiceServerUpdateEvent {
#[doc(hidden)]
#[inline]
pub fn decode(mut map: Map) -> Result<Self> {
Ok(VoiceServerUpdateEvent {
guild_id: opt(&mut map, "guild_id", GuildId::decode)?,
channel_id: opt(&mut map, "channel_id", ChannelId::decode)?,
endpoint: opt(&mut map, "endpoint", into_string)?,
token: remove(&mut map, "token").and_then(into_string)?,
})
}
}
#[derive(Clone, Debug)]
pub struct VoiceStateUpdateEvent {
pub guild_id: Option<GuildId>,
pub voice_state: VoiceState,
}
impl VoiceStateUpdateEvent {
#[doc(hidden)]
#[inline]
pub fn decode(mut map: Map) -> Result<Self> {
Ok(VoiceStateUpdateEvent {
guild_id: opt(&mut map, "guild_id", GuildId::decode)?,
voice_state: VoiceState::decode(Value::Object(map))?,
})
}
}
#[derive(Clone, Debug)]
pub struct WebhookUpdateEvent {
pub channel_id: ChannelId,
pub guild_id: GuildId,
}
impl WebhookUpdateEvent {
#[doc(hidden)]
#[inline]
pub fn decode(mut map: Map) -> Result<Self> {
Ok(WebhookUpdateEvent {
channel_id: remove(&mut map, "channel_id").and_then(ChannelId::decode)?,
guild_id: remove(&mut map, "guild_id").and_then(GuildId::decode)?,
})
}
}
#[derive(Debug, Clone)]
pub enum GatewayEvent {
Dispatch(u64, Event),
Heartbeat(u64),
Reconnect,
InvalidateSession,
Hello(u64),
HeartbeatAck,
}
impl GatewayEvent {
pub fn decode(value: Value) -> Result<Self> {
let mut value = into_map(value)?;
let op = req!(value.get("op").and_then(|x| x.as_u64()));
match OpCode::from_num(op).ok_or(Error::Client(ClientError::InvalidOpCode))? {
OpCode::Event => Ok(GatewayEvent::Dispatch(
req!(remove(&mut value, "s")?.as_u64()),
Event::decode(
remove(&mut value, "t").and_then(into_string)?,
remove(&mut value, "d")?
)?
)),
OpCode::Heartbeat => {
Ok(GatewayEvent::Heartbeat(req!(remove(&mut value, "s")?
.as_u64())))
},
OpCode::Reconnect => Ok(GatewayEvent::Reconnect),
OpCode::InvalidSession => Ok(GatewayEvent::InvalidateSession),
OpCode::Hello => {
let mut data = remove(&mut value, "d").and_then(into_map)?;
let interval = req!(remove(&mut data, "heartbeat_interval")?.as_u64());
Ok(GatewayEvent::Hello(interval))
},
OpCode::HeartbeatAck => Ok(GatewayEvent::HeartbeatAck),
_ => Err(Error::Decode("Unexpected opcode", Value::Object(value))),
}
}
}
/// Event received over a websocket connection
#[derive(Clone, Debug)]
pub enum Event {
/// A new group call has been created
CallCreate(CallCreateEvent),
/// A group call has been deleted (the call ended)
CallDelete(CallDeleteEvent),
/// A group call has been updated
CallUpdate(CallUpdateEvent),
ChannelCreate(ChannelCreateEvent),
ChannelDelete(ChannelDeleteEvent),
ChannelPinsAck(ChannelPinsAckEvent),
ChannelPinsUpdate(ChannelPinsUpdateEvent),
/// A user has been added to a group
ChannelRecipientAdd(ChannelRecipientAddEvent),
/// A user has been removed from a group
ChannelRecipientRemove(ChannelRecipientRemoveEvent),
ChannelUpdate(ChannelUpdateEvent),
/// When a suggestion for a friend is created, due to a connection like
/// [`Skype`].
///
/// [`Connection::Skype`]: enum.Connection.html#variant.Skype
FriendSuggestionCreate(FriendSuggestionCreateEvent),
/// When a suggestion for a friend is removed.
FriendSuggestionDelete(FriendSuggestionDeleteEvent),
GuildBanAdd(GuildBanAddEvent),
GuildBanRemove(GuildBanRemoveEvent),
GuildCreate(GuildCreateEvent),
GuildDelete(GuildDeleteEvent),
GuildEmojisUpdate(GuildEmojisUpdateEvent),
GuildIntegrationsUpdate(GuildIntegrationsUpdateEvent),
GuildMemberAdd(GuildMemberAddEvent),
GuildMemberRemove(GuildMemberRemoveEvent),
/// A member's roles have changed
GuildMemberUpdate(GuildMemberUpdateEvent),
GuildMembersChunk(GuildMembersChunkEvent),
GuildRoleCreate(GuildRoleCreateEvent),
GuildRoleDelete(GuildRoleDeleteEvent),
GuildRoleUpdate(GuildRoleUpdateEvent),
GuildSync(GuildSyncEvent),
/// When a guild is unavailable, such as due to a Discord server outage.
GuildUnavailable(GuildUnavailableEvent),
GuildUpdate(GuildUpdateEvent),
/// Another logged-in device acknowledged this message
MessageAck(MessageAckEvent),
MessageCreate(MessageCreateEvent),
MessageDelete(MessageDeleteEvent),
MessageDeleteBulk(MessageDeleteBulkEvent),
/// A message has been edited, either by the user or the system
MessageUpdate(MessageUpdateEvent),
/// A member's presence state (or username or avatar) has changed
PresenceUpdate(PresenceUpdateEvent),
/// The precense list of the user's friends should be replaced entirely
PresencesReplace(PresencesReplaceEvent),
/// A reaction was added to a message.
///
/// Fires the [`on_message_reaction_add`] event handler.
///
/// [`on_message_reaction_add`]: ../client/struct.Client.html#method.on_message_reaction_add
ReactionAdd(ReactionAddEvent),
/// A reaction was removed to a message.
///
/// Fires the [`on_message_reaction_remove`] event handler.
///