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
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
#![doc = include_str!("../README.md")]
#![warn(
    clippy::missing_const_for_fn,
    clippy::missing_docs_in_private_items,
    clippy::pedantic,
    missing_docs,
    unsafe_code
)]
#![allow(
    clippy::module_name_repetitions,
    clippy::must_use_candidate,
    clippy::unnecessary_wraps
)]

pub mod future;

use self::future::{
    WaitForComponentFuture, WaitForComponentStream, WaitForEventFuture, WaitForEventStream,
    WaitForGuildEventFuture, WaitForGuildEventStream, WaitForMessageFuture, WaitForMessageStream,
    WaitForReactionFuture, WaitForReactionStream,
};
use dashmap::DashMap;
use std::{
    fmt::{Debug, Display, Formatter, Result as FmtResult},
    hash::Hash,
    sync::atomic::{AtomicU64, Ordering},
};
use tokio::sync::{
    mpsc::{self, UnboundedReceiver, UnboundedSender as MpscSender},
    oneshot::{self, Receiver, Sender as OneshotSender},
};
use twilight_model::{
    application::interaction::{Interaction, InteractionType},
    gateway::{
        event::Event,
        payload::incoming::{MessageCreate, ReactionAdd},
    },
    id::{
        marker::{ChannelMarker, GuildMarker, MessageMarker},
        Id,
    },
};

/// Map keyed by an ID - such as a channel ID or message ID - storing a list of
/// bystanders.
type BystanderMap<K, V> = DashMap<K, Vec<Bystander<V>>>;

/// Sender to a caller that may be for a future bystander or a stream bystander.
#[derive(Debug)]
enum Sender<E> {
    /// Bystander is a future and the sender is a oneshot.
    Future(OneshotSender<E>),
    /// Bystander is a stream and the sender is an MPSC.
    Stream(MpscSender<E>),
}

impl<E> Sender<E> {
    /// Whether the channel is closed.
    fn is_closed(&self) -> bool {
        match self {
            Self::Future(sender) => sender.is_closed(),
            Self::Stream(sender) => sender.is_closed(),
        }
    }
}

/// Registration for a caller to wait for an event based on a predicate
/// function.
struct Bystander<T> {
    /// Predicate check to perform on an event.
    func: Box<dyn Fn(&T) -> bool + Send + Sync>,
    /// [`Sender::Future`]s consume themselves once upon sending so the sender
    /// needs to be able to be taken out separately.
    sender: Option<Sender<T>>,
}

impl<T: Debug> Debug for Bystander<T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        f.debug_struct("Bystander")
            .field("func", &"<dyn Fn(&T) -> bool>")
            .field("sender", &self.sender)
            .finish()
    }
}

/// The `Standby` struct, used by the main event loop to process events and by
/// tasks to wait for an event.
///
/// Refer to the crate-level documentation for more information.
///
/// # Using Standby in multiple tasks
///
/// To use a Standby instance in multiple tasks, consider wrapping it in an
/// [`std::sync::Arc`] or [`std::rc::Rc`].
///
/// # Examples
///
/// ## Timeouts
///
/// Futures can be timed out by passing the future returned by Standby to
/// functions such as [`tokio::time::timeout`]:
///
/// ```rust,no_run
/// # #[tokio::main] async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use std::time::Duration;
/// use twilight_model::gateway::event::{Event, EventType};
/// use twilight_standby::Standby;
///
/// let standby = Standby::new();
/// let future = standby.wait_for_event(|event: &Event| event.kind() == EventType::Ready);
/// let event = tokio::time::timeout(Duration::from_secs(1), future).await?;
/// # Ok(()) }
/// ```
///
/// [`tokio::time::timeout`]: https://docs.rs/tokio/latest/tokio/time/fn.timeout.html
#[derive(Debug, Default)]
pub struct Standby {
    /// List of component bystanders where the ID of the message is known
    /// beforehand.
    components: DashMap<Id<MessageMarker>, Vec<Bystander<Interaction>>>,
    /// Bystanders for any event that may not be in any particular guild.
    ///
    /// The key is generated via [`event_counter`].
    ///
    /// [`event_counter`]: Self::event_counter
    events: DashMap<u64, Bystander<Event>>,
    /// Event counter to be used as the key of [`events`].
    ///
    /// [`events`]: Self::events
    event_counter: AtomicU64,
    /// List of bystanders where the ID of the guild is known beforehand.
    guilds: DashMap<Id<GuildMarker>, Vec<Bystander<Event>>>,
    /// List of message bystanders where the ID of the channel is known
    /// beforehand.
    messages: DashMap<Id<ChannelMarker>, Vec<Bystander<MessageCreate>>>,
    /// List of reaction bystanders where the ID of the message is known
    /// beforehand.
    reactions: DashMap<Id<MessageMarker>, Vec<Bystander<ReactionAdd>>>,
}

impl Standby {
    /// Create a new instance of `Standby`.
    ///
    /// Once a `Standby` has been created it must process gateway events via
    /// [`process`]. Awaiting an event can start via methods such as
    /// [`wait_for`] and [`wait_for_message_stream`].
    ///
    /// [`process`]: Self::process
    /// [`wait_for`]: Self::wait_for
    /// [`wait_for_message_stream`]: Self::wait_for_message_stream
    #[must_use = "must process events to be useful"]
    pub fn new() -> Self {
        Self::default()
    }

    /// Process an event, calling any bystanders that might be waiting on it.
    ///
    /// Returns statistics about matched [`Standby`] calls and how they were
    /// processed. For example, by using [`ProcessResults::matched`] you can
    /// determine how many calls were sent an event.
    ///
    /// When a bystander checks to see if an event is what it's waiting for, it
    /// will receive the event by cloning it.
    ///
    /// This function must be called when events are received in order for
    /// futures returned by methods to fulfill.
    pub fn process(&self, event: &Event) -> ProcessResults {
        tracing::trace!(event_type = ?event.kind(), ?event, "processing event");

        let mut completions = ProcessResults::new();

        match event {
            Event::InteractionCreate(e) => {
                if e.kind == InteractionType::MessageComponent {
                    if let Some(message) = &e.message {
                        completions.add_with(&Self::process_specific_event(
                            &self.components,
                            message.id,
                            e,
                        ));
                    }
                }
            }
            Event::MessageCreate(e) => {
                completions.add_with(&Self::process_specific_event(
                    &self.messages,
                    e.0.channel_id,
                    e,
                ));
            }
            Event::ReactionAdd(e) => {
                completions.add_with(&Self::process_specific_event(
                    &self.reactions,
                    e.0.message_id,
                    e,
                ));
            }
            _ => {}
        }

        if let Some(guild_id) = event.guild_id() {
            completions.add_with(&Self::process_specific_event(&self.guilds, guild_id, event));
        }

        completions.add_with(&Self::process_event(&self.events, event));

        completions
    }

    /// Wait for an event in a certain guild.
    ///
    /// To wait for multiple guild events matching the given predicate use
    /// [`wait_for_stream`].
    ///
    /// # Examples
    ///
    /// Wait for a [`BanAdd`] event in guild 123:
    ///
    /// ```no_run
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use twilight_model::{
    ///     gateway::event::{Event, EventType},
    ///     id::Id,
    /// };
    /// use twilight_standby::Standby;
    ///
    /// let standby = Standby::new();
    ///
    /// let guild_id = Id::new(123);
    ///
    /// let reaction = standby
    ///     .wait_for(guild_id, |event: &Event| event.kind() == EventType::BanAdd)
    ///     .await?;
    /// # Ok(()) }
    /// ```
    ///
    /// # Errors
    ///
    /// The returned future resolves to a [`Canceled`] error if the associated
    /// [`Standby`] instance is dropped.
    ///
    /// [`BanAdd`]: twilight_model::gateway::payload::incoming::BanAdd
    /// [`Canceled`]: future::Canceled
    /// [`wait_for_stream`]: Self::wait_for_stream
    pub fn wait_for<F: Fn(&Event) -> bool + Send + Sync + 'static>(
        &self,
        guild_id: Id<GuildMarker>,
        check: impl Into<Box<F>>,
    ) -> WaitForGuildEventFuture {
        tracing::trace!(%guild_id, "waiting for event in guild");

        WaitForGuildEventFuture {
            rx: Self::insert_future(&self.guilds, guild_id, check),
        }
    }

    /// Wait for a stream of events in a certain guild.
    ///
    /// To wait for only one guild event matching the given predicate use
    /// [`wait_for`].
    ///
    /// # Examples
    ///
    /// Wait for multiple [`BanAdd`] events in guild 123:
    ///
    /// ```no_run
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use tokio_stream::StreamExt;
    /// use twilight_model::{
    ///     gateway::event::{Event, EventType},
    ///     id::Id,
    /// };
    /// use twilight_standby::Standby;
    ///
    /// let standby = Standby::new();
    ///
    /// let guild_id = Id::new(123);
    ///
    /// let mut stream =
    ///     standby.wait_for_stream(guild_id, |event: &Event| event.kind() == EventType::BanAdd);
    ///
    /// while let Some(event) = stream.next().await {
    ///     if let Event::BanAdd(ban) = event {
    ///         println!("user {} was banned in guild {}", ban.user.id, ban.guild_id);
    ///     }
    /// }
    /// # Ok(()) }
    /// ```
    ///
    /// # Errors
    ///
    /// The returned stream ends when the associated [`Standby`] instance is
    /// dropped.
    ///
    /// [`BanAdd`]: twilight_model::gateway::payload::incoming::BanAdd
    /// [`wait_for`]: Self::wait_for
    pub fn wait_for_stream<F: Fn(&Event) -> bool + Send + Sync + 'static>(
        &self,
        guild_id: Id<GuildMarker>,
        check: impl Into<Box<F>>,
    ) -> WaitForGuildEventStream {
        tracing::trace!(%guild_id, "waiting for event in guild");

        WaitForGuildEventStream {
            rx: Self::insert_stream(&self.guilds, guild_id, check),
        }
    }

    /// Wait for an event not in a certain guild. This must be filtered by an
    /// event type.
    ///
    /// To wait for multiple events matching the given predicate use
    /// [`wait_for_event_stream`].
    ///
    /// # Examples
    ///
    /// Wait for a [`Ready`] event for shard 5:
    ///
    /// ```no_run
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use twilight_model::gateway::event::{Event, EventType};
    /// use twilight_standby::Standby;
    ///
    /// let standby = Standby::new();
    ///
    /// let ready = standby
    ///     .wait_for_event(|event: &Event| {
    ///         if let Event::Ready(ready) = event {
    ///             ready.shard.map_or(false, |id| id.number() == 5)
    ///         } else {
    ///             false
    ///         }
    ///     })
    ///     .await?;
    /// # Ok(()) }
    /// ```
    ///
    /// # Errors
    ///
    /// The returned future resolves to a [`Canceled`] error if the associated
    /// [`Standby`] instance is dropped.
    ///
    /// [`Canceled`]: future::Canceled
    /// [`Ready`]: twilight_model::gateway::payload::incoming::Ready
    /// [`wait_for_event_stream`]: Self::wait_for_event_stream
    pub fn wait_for_event<F: Fn(&Event) -> bool + Send + Sync + 'static>(
        &self,
        check: impl Into<Box<F>>,
    ) -> WaitForEventFuture {
        tracing::trace!("waiting for event");

        let (tx, rx) = oneshot::channel();

        self.events.insert(
            self.next_event_id(),
            Bystander {
                func: check.into(),
                sender: Some(Sender::Future(tx)),
            },
        );

        WaitForEventFuture { rx }
    }

    /// Wait for a stream of events not in a certain guild. This must be
    /// filtered by an event type.
    ///
    /// To wait for only one event matching the given predicate use
    /// [`wait_for_event`].
    ///
    /// # Examples
    ///
    /// Wait for multiple [`Ready`] events on shard 5:
    ///
    /// ```no_run
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use tokio_stream::StreamExt;
    /// use twilight_model::gateway::event::{Event, EventType};
    /// use twilight_standby::Standby;
    ///
    /// let standby = Standby::new();
    ///
    /// let mut events = standby.wait_for_event_stream(|event: &Event| {
    ///     if let Event::Ready(ready) = event {
    ///         ready.shard.map_or(false, |id| id.number() == 5)
    ///     } else {
    ///         false
    ///     }
    /// });
    ///
    /// while let Some(event) = events.next().await {
    ///     println!("got event with type {:?}", event.kind());
    /// }
    /// # Ok(()) }
    /// ```
    ///
    /// # Errors
    ///
    /// The returned stream ends when the associated [`Standby`] instance is
    /// dropped.
    ///
    /// [`Ready`]: twilight_model::gateway::payload::incoming::Ready
    /// [`wait_for_event`]: Self::wait_for_event
    pub fn wait_for_event_stream<F: Fn(&Event) -> bool + Send + Sync + 'static>(
        &self,
        check: impl Into<Box<F>>,
    ) -> WaitForEventStream {
        tracing::trace!("waiting for event");

        let (tx, rx) = mpsc::unbounded_channel();

        self.events.insert(
            self.next_event_id(),
            Bystander {
                func: check.into(),
                sender: Some(Sender::Stream(tx)),
            },
        );

        WaitForEventStream { rx }
    }

    /// Wait for a message in a certain channel.
    ///
    /// To wait for multiple messages matching the given predicate use
    /// [`wait_for_message_stream`].
    ///
    /// # Examples
    ///
    /// Wait for a message in channel 123 by user 456 with the content "test":
    ///
    /// ```no_run
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use twilight_model::{gateway::payload::incoming::MessageCreate, id::Id};
    /// use twilight_standby::Standby;
    ///
    /// let standby = Standby::new();
    ///
    /// let author_id = Id::new(456);
    /// let channel_id = Id::new(123);
    ///
    /// let message = standby
    ///     .wait_for_message(channel_id, move |event: &MessageCreate| {
    ///         event.author.id == author_id && event.content == "test"
    ///     })
    ///     .await?;
    /// # Ok(()) }
    /// ```
    ///
    /// # Errors
    ///
    /// The returned future resolves to a [`Canceled`] error if the associated
    /// [`Standby`] instance is dropped.
    ///
    /// [`Canceled`]: future::Canceled
    /// [`wait_for_message_stream`]: Self::wait_for_message_stream
    pub fn wait_for_message<F: Fn(&MessageCreate) -> bool + Send + Sync + 'static>(
        &self,
        channel_id: Id<ChannelMarker>,
        check: impl Into<Box<F>>,
    ) -> WaitForMessageFuture {
        tracing::trace!(%channel_id, "waiting for message in channel");

        WaitForMessageFuture {
            rx: Self::insert_future(&self.messages, channel_id, check),
        }
    }

    /// Wait for a stream of message in a certain channel.
    ///
    /// To wait for only one message matching the given predicate use
    /// [`wait_for_message`].
    ///
    /// # Examples
    ///
    /// Wait for multiple messages in channel 123 by user 456 with the content
    /// "test":
    ///
    /// ```no_run
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use tokio_stream::StreamExt;
    /// use twilight_model::{gateway::payload::incoming::MessageCreate, id::Id};
    /// use twilight_standby::Standby;
    ///
    /// let standby = Standby::new();
    ///
    /// let author_id = Id::new(456);
    /// let channel_id = Id::new(123);
    ///
    /// let mut messages = standby.wait_for_message_stream(channel_id, move |event: &MessageCreate| {
    ///     event.author.id == author_id && event.content == "test"
    /// });
    ///
    /// while let Some(message) = messages.next().await {
    ///     println!("got message by {}", message.author.id);
    /// }
    /// # Ok(()) }
    /// ```
    ///
    /// # Errors
    ///
    /// The returned stream ends when the associated [`Standby`] instance is
    /// dropped.
    ///
    /// [`wait_for_message`]: Self::wait_for_message
    pub fn wait_for_message_stream<F: Fn(&MessageCreate) -> bool + Send + Sync + 'static>(
        &self,
        channel_id: Id<ChannelMarker>,
        check: impl Into<Box<F>>,
    ) -> WaitForMessageStream {
        tracing::trace!(%channel_id, "waiting for message in channel");

        WaitForMessageStream {
            rx: Self::insert_stream(&self.messages, channel_id, check),
        }
    }

    /// Wait for a reaction on a certain message.
    ///
    /// To wait for multiple reactions matching the given predicate use
    /// [`wait_for_reaction_stream`].
    ///
    /// # Examples
    ///
    /// Wait for a reaction on message 123 by user 456:
    ///
    /// ```no_run
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use twilight_model::{gateway::payload::incoming::ReactionAdd, id::Id};
    /// use twilight_standby::Standby;
    ///
    /// let standby = Standby::new();
    ///
    /// let message_id = Id::new(123);
    /// let user_id = Id::new(456);
    ///
    /// let reaction = standby
    ///     .wait_for_reaction(message_id, move |event: &ReactionAdd| {
    ///         event.user_id == user_id
    ///     })
    ///     .await?;
    /// # Ok(()) }
    /// ```
    ///
    /// # Errors
    ///
    /// The returned future resolves to a [`Canceled`] error if the associated
    /// [`Standby`] instance is dropped.
    ///
    /// [`Canceled`]: future::Canceled
    /// [`wait_for_reaction_stream`]: Self::wait_for_reaction_stream
    pub fn wait_for_reaction<F: Fn(&ReactionAdd) -> bool + Send + Sync + 'static>(
        &self,
        message_id: Id<MessageMarker>,
        check: impl Into<Box<F>>,
    ) -> WaitForReactionFuture {
        tracing::trace!(%message_id, "waiting for reaction on message");

        WaitForReactionFuture {
            rx: Self::insert_future(&self.reactions, message_id, check),
        }
    }

    /// Wait for a stream of reactions on a certain message.
    ///
    /// To wait for only one reaction matching the given predicate use
    /// [`wait_for_reaction`].
    ///
    /// # Examples
    ///
    /// Wait for multiple reactions on message 123 with unicode reaction "🤠":
    ///
    /// ```no_run
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use tokio_stream::StreamExt;
    /// use twilight_model::{
    ///     channel::message::ReactionType,
    ///     gateway::payload::incoming::ReactionAdd,
    ///     id::Id,
    /// };
    /// use twilight_standby::Standby;
    ///
    /// let standby = Standby::new();
    ///
    /// let message_id = Id::new(123);
    ///
    /// let mut reactions = standby.wait_for_reaction_stream(message_id, |event: &ReactionAdd| {
    ///     matches!(&event.emoji, ReactionType::Unicode { name } if name == "🤠")
    /// });
    ///
    /// while let Some(reaction) = reactions.next().await {
    ///     println!("got a reaction by {}", reaction.user_id);
    /// }
    /// # Ok(()) }
    /// ```
    ///
    /// # Errors
    ///
    /// The returned stream ends when the associated [`Standby`] instance is
    /// dropped.
    ///
    /// [`wait_for_reaction`]: Self::wait_for_reaction
    pub fn wait_for_reaction_stream<F: Fn(&ReactionAdd) -> bool + Send + Sync + 'static>(
        &self,
        message_id: Id<MessageMarker>,
        check: impl Into<Box<F>>,
    ) -> WaitForReactionStream {
        tracing::trace!(%message_id, "waiting for reaction on message");

        WaitForReactionStream {
            rx: Self::insert_stream(&self.reactions, message_id, check),
        }
    }

    /// Wait for a component on a certain message.
    ///
    /// Returns a `Canceled` error if the `Standby` struct was dropped.
    ///
    /// If you need to wait for multiple components matching the given predicate,
    /// use [`wait_for_component_stream`].
    ///
    /// # Examples
    ///
    /// Wait for a component on message 123 by user 456:
    ///
    /// ```no_run
    /// # #[tokio::main] async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use twilight_model::{application::interaction::Interaction, id::Id};
    /// use twilight_standby::Standby;
    ///
    /// let standby = Standby::new();
    /// let message_id = Id::new(123);
    ///
    /// let component = standby
    ///     .wait_for_component(message_id, |event: &Interaction| {
    ///         event.author_id() == Some(Id::new(456))
    ///     })
    ///     .await?;
    /// # Ok(()) }
    /// ```
    ///
    /// [`wait_for_component_stream`]: Self::wait_for_component_stream
    pub fn wait_for_component<F: Fn(&Interaction) -> bool + Send + Sync + 'static>(
        &self,
        message_id: Id<MessageMarker>,
        check: impl Into<Box<F>>,
    ) -> WaitForComponentFuture {
        tracing::trace!(%message_id, "waiting for component on message");

        WaitForComponentFuture {
            rx: Self::insert_future(&self.components, message_id, check),
        }
    }

    /// Wait for a stream of components on a certain message.
    ///
    /// Returns a `Canceled` error if the `Standby` struct was dropped.
    ///
    /// If you need to wait for only one component matching the given predicate,
    /// use [`wait_for_component`].
    ///
    /// # Examples
    ///
    /// Wait for multiple button components on message 123 with a `custom_id` of
    /// "Click":
    ///
    /// ```no_run
    /// # #[tokio::main] async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use tokio_stream::StreamExt;
    /// use twilight_model::{
    ///     application::interaction::{Interaction, InteractionData},
    ///     id::Id,
    /// };
    /// use twilight_standby::Standby;
    ///
    /// let standby = Standby::new();
    /// let message_id = Id::new(123);
    ///
    /// let mut components = standby.wait_for_component_stream(message_id, |event: &Interaction| {
    ///     if let Some(InteractionData::MessageComponent(data)) = &event.data {
    ///         data.custom_id == "Click".to_string()
    ///     } else {
    ///         false
    ///     }
    /// });
    ///
    /// while let Some(component) = components.next().await {
    ///     println!("got a component by {}", component.author_id().unwrap());
    /// }
    /// # Ok(()) }
    /// ```
    ///
    /// [`wait_for_component`]: Self::wait_for_component
    pub fn wait_for_component_stream<F: Fn(&Interaction) -> bool + Send + Sync + 'static>(
        &self,
        message_id: Id<MessageMarker>,
        check: impl Into<Box<F>>,
    ) -> WaitForComponentStream {
        tracing::trace!(%message_id, "waiting for component on message");

        WaitForComponentStream {
            rx: Self::insert_stream(&self.components, message_id, check),
        }
    }

    /// Next event ID in [`Standby::event_counter`].
    fn next_event_id(&self) -> u64 {
        self.event_counter.fetch_add(1, Ordering::SeqCst)
    }

    /// Append a new future bystander into a map according to the ID.
    fn insert_future<F: Fn(&V) -> bool + Send + Sync + 'static, K: Eq + Hash, V>(
        map: &BystanderMap<K, V>,
        id: K,
        check: impl Into<Box<F>>,
    ) -> Receiver<V> {
        let (tx, rx) = oneshot::channel();

        let mut entry = map.entry(id).or_default();
        entry.push(Bystander {
            func: check.into(),
            sender: Some(Sender::Future(tx)),
        });

        rx
    }

    /// Append a new stream bystander into a map according to the ID.
    fn insert_stream<F: Fn(&V) -> bool + Send + Sync + 'static, K: Eq + Hash, V>(
        map: &BystanderMap<K, V>,
        id: K,
        check: impl Into<Box<F>>,
    ) -> UnboundedReceiver<V> {
        let (tx, rx) = mpsc::unbounded_channel();

        let mut entry = map.entry(id).or_default();
        entry.push(Bystander {
            func: check.into(),
            sender: Some(Sender::Stream(tx)),
        });

        rx
    }

    /// Process a general event that is not of any particular type or in any
    /// particular guild.
    #[tracing::instrument(level = "trace")]
    fn process_event<K: Debug + Display + Eq + Hash + PartialEq + 'static, V: Clone + Debug>(
        map: &DashMap<K, Bystander<V>>,
        event: &V,
    ) -> ProcessResults {
        tracing::trace!(?event, "processing event");

        let mut results = ProcessResults::new();

        map.retain(|id, bystander| {
            let result = Self::bystander_process(bystander, event);
            results.handle(result);

            tracing::trace!(bystander_id = %id, ?result, "event bystander processed");

            // We want to retain bystanders that are *incomplete* and remove
            // bystanders that are *complete*.
            !result.is_complete()
        });

        results
    }

    /// Process a general event that is either of a particular type or in a
    /// particular guild.
    #[tracing::instrument(level = "trace")]
    fn process_specific_event<
        K: Debug + Display + Eq + Hash + PartialEq + 'static,
        V: Clone + Debug,
    >(
        map: &DashMap<K, Vec<Bystander<V>>>,
        guild_id: K,
        event: &V,
    ) -> ProcessResults {
        // Iterate over a guild's bystanders and mark it for removal if there
        // are no bystanders remaining.
        let (remove_guild, results) = if let Some(mut bystanders) = map.get_mut(&guild_id) {
            let results = Self::bystander_iter(&mut bystanders, event);

            (bystanders.is_empty(), results)
        } else {
            tracing::trace!(%guild_id, "guild has no event bystanders");

            return ProcessResults::new();
        };

        if remove_guild {
            tracing::trace!(%guild_id, "removing guild from map");

            map.remove(&guild_id);
        }

        results
    }

    /// Iterate over bystanders and remove the ones that match the predicate.
    #[tracing::instrument(level = "trace")]
    fn bystander_iter<E: Clone + Debug>(
        bystanders: &mut Vec<Bystander<E>>,
        event: &E,
    ) -> ProcessResults {
        tracing::trace!(?bystanders, "iterating over bystanders");

        // Iterate over the list of bystanders by using an index and manually
        // indexing in to the list.
        //
        // # Logic
        //
        // In each iteration we decide whether to retain a bystander: if we do
        // then we can increment our index and move on, but if we opt to instead
        // remove it then we do so and don't increment the index. The reason we
        // don't increment the index is because when we remove an element the
        // index does not become empty and instead everything to the right is
        // shifted to the left, illustrated as such:
        //
        //     |---|
        //     v   |
        // A - B - C - D
        //     |   ^   |
        //     |   |---|
        //     |
        //  Remove B
        //
        // After: A - C - D
        //
        // # Reasons not to use alternatives
        //
        // **`Vec::retain`** we need to mutate the entries in order to take out
        // the sender and `Vec::retain` only gives us immutable references.
        //
        // A form of enumeration can't be used because sometimes the index
        // doesn't advance; iterators would continue to provide incrementing
        // enumeration indexes while we sometimes want to reuse an index.
        let mut index = 0;
        let mut results = ProcessResults::new();

        while index < bystanders.len() {
            tracing::trace!(%index, "checking bystander");

            let status = Self::bystander_process(&mut bystanders[index], event);
            results.handle(status);

            tracing::trace!(%index, ?status, "checked bystander");

            if status.is_complete() {
                bystanders.remove(index);
            } else {
                index += 1;
            }
        }

        results
    }

    /// Process a bystander, sending the event if the sender is active and the
    /// predicate matches. Returns whether the bystander has fulfilled.
    ///
    /// Returns whether the bystander is fulfilled; if the bystander has been
    /// fulfilled then the channel is now closed.
    #[tracing::instrument(level = "trace")]
    fn bystander_process<T: Clone + Debug>(
        bystander: &mut Bystander<T>,
        event: &T,
    ) -> ProcessStatus {
        // We need to take the sender out because `OneshotSender`s consume
        // themselves when calling `OneshotSender::send`.
        let Some(sender) = bystander.sender.take() else {
            tracing::trace!("bystander has no sender, indicating for removal");

            return ProcessStatus::AlreadyComplete;
        };

        // The channel may have closed due to the receiver dropping their end,
        // in which case we can say we're done.
        if sender.is_closed() {
            tracing::trace!("bystander's rx dropped, indicating for removal");

            return ProcessStatus::Dropped;
        }

        // Lastly check to see if the predicate matches the event. If it doesn't
        // then we can short-circuit.
        if !(bystander.func)(event) {
            tracing::trace!("bystander check doesn't match, not removing");

            // Put the sender back into its bystander since we'll still need it
            // next time around.
            bystander.sender.replace(sender);

            return ProcessStatus::Skip;
        }

        match sender {
            Sender::Future(tx) => {
                // We don't care if the event successfully sends or not since
                // we're going to be tossing out the bystander anyway.
                drop(tx.send(event.clone()));

                tracing::trace!("bystander matched event, indicating for removal");

                ProcessStatus::SentFuture
            }
            Sender::Stream(tx) => {
                // If we can send an event to the receiver and the channel is
                // still open then we need to retain the bystander, otherwise we
                // need to mark it for removal.
                if tx.send(event.clone()).is_ok() {
                    tracing::trace!("bystander is a stream, retaining in map");

                    bystander.sender.replace(Sender::Stream(tx));

                    ProcessStatus::SentStream
                } else {
                    ProcessStatus::Dropped
                }
            }
        }
    }
}
/// Number of [`Standby`] calls that were completed.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct ProcessResults {
    /// Number of bystanders that were dropped due to the receiving end
    /// dropping.
    dropped: usize,
    /// Number of future bystanders that were open and were sent an event.
    fulfilled: usize,
    /// Number of stream bystanders that were open and were sent an event.
    sent: usize,
}

impl ProcessResults {
    /// Create a new set of zeroed out results.
    const fn new() -> Self {
        Self {
            dropped: 0,
            fulfilled: 0,
            sent: 0,
        }
    }

    /// Number of [`Standby`] calls where the receiver had already dropped their
    /// end.
    ///
    /// This may happen when a caller calls into [`Standby`] but then times out
    /// or otherwise cancels their request.
    pub const fn dropped(&self) -> usize {
        self.dropped
    }

    /// Number of [`Standby`] calls that were fulfilled.
    ///
    /// Calls for futures via methods such as [`Standby::wait_for`] will be
    /// marked as fulfilled once matched and an event is sent over the channel.
    ///
    /// **Caveat**: although an event has been sent over the channel to the
    /// receiver it is not guaranteed whether the receiver end actually received
    /// it; the receiver end may drop shortly after an event is sent. In this
    /// case the call is considered to be fulfilled.
    pub const fn fulfilled(&self) -> usize {
        self.fulfilled
    }

    /// Number of calls that were matched and were sent an event.
    ///
    /// This is the sum of [`fulfilled`] and [`sent`].
    ///
    /// See the caveats for both methods.
    ///
    /// [`fulfilled`]: Self::fulfilled
    /// [`sent`]: Self::sent
    pub const fn matched(&self) -> usize {
        self.fulfilled() + self.sent()
    }

    /// Number of [`Standby`] streaming calls that were matched and had an event
    /// sent to them.
    ///
    /// **Caveat**: although an event has been sent over the channel to the
    /// receiver it is not guaranteed whether the receiver end actually received
    /// it; the receiver end may drop shortly after an event is sent. In this
    /// case the call is considered to be sent. Checks over this call will in
    /// the future be considered [`dropped`].
    ///
    /// [`dropped`]: Self::dropped
    pub const fn sent(&self) -> usize {
        self.sent
    }

    /// Add another set of results to this set.
    fn add_with(&mut self, other: &Self) {
        self.dropped = self.dropped.saturating_add(other.dropped);
        self.fulfilled = self.fulfilled.saturating_add(other.fulfilled);
        self.sent = self.sent.saturating_add(other.sent);
    }

    /// Handle a process status.
    fn handle(&mut self, status: ProcessStatus) {
        match status {
            ProcessStatus::Dropped => {
                self.dropped += 1;
            }
            ProcessStatus::SentFuture => {
                self.fulfilled += 1;
            }
            ProcessStatus::SentStream => {
                self.sent += 1;
            }
            ProcessStatus::AlreadyComplete | ProcessStatus::Skip => {}
        }
    }
}

/// Status result of processing a bystander via [`Standby::bystander_process`].
#[derive(Clone, Copy, Debug)]
enum ProcessStatus {
    /// Call matched but already matched previously and was not removed, so the
    /// subject must be removed and not counted towards results.
    AlreadyComplete,
    /// Call matched but the receiver dropped their end.
    Dropped,
    /// Call matched a oneshot.
    SentFuture,
    /// Call matched a stream.
    SentStream,
    /// Call was not matched.
    Skip,
}

impl ProcessStatus {
    /// Whether the call is complete.
    const fn is_complete(self) -> bool {
        matches!(
            self,
            Self::AlreadyComplete | Self::Dropped | Self::SentFuture
        )
    }
}

#[cfg(test)]
mod tests {
    #![allow(clippy::non_ascii_literal)]

    use crate::Standby;
    use static_assertions::assert_impl_all;
    use std::fmt::Debug;
    use tokio_stream::StreamExt;
    use twilight_gateway::{Event, EventType};
    use twilight_model::{
        application::interaction::{
            message_component::MessageComponentInteractionData, Interaction, InteractionData,
            InteractionType,
        },
        channel::{
            message::{component::ComponentType, Message, MessageType, ReactionType},
            Channel, ChannelType,
        },
        gateway::{
            payload::incoming::{InteractionCreate, MessageCreate, ReactionAdd, Ready, RoleDelete},
            GatewayReaction, ShardId,
        },
        guild::Permissions,
        id::{marker::GuildMarker, Id},
        oauth::{ApplicationFlags, PartialApplication},
        user::{CurrentUser, User},
        util::Timestamp,
    };

    assert_impl_all!(Standby: Debug, Default, Send, Sync);

    fn message() -> Message {
        Message {
            activity: None,
            application: None,
            application_id: None,
            attachments: Vec::new(),
            author: User {
                accent_color: None,
                avatar: None,
                avatar_decoration: None,
                banner: None,
                bot: false,
                discriminator: 1,
                email: None,
                flags: None,
                global_name: Some("test".to_owned()),
                id: Id::new(2),
                locale: None,
                mfa_enabled: None,
                name: "twilight".to_owned(),
                premium_type: None,
                public_flags: None,
                system: None,
                verified: None,
            },
            channel_id: Id::new(1),
            components: Vec::new(),
            content: "test".to_owned(),
            edited_timestamp: None,
            embeds: Vec::new(),
            flags: None,
            guild_id: Some(Id::new(4)),
            id: Id::new(3),
            interaction: None,
            kind: MessageType::Regular,
            member: None,
            mention_channels: Vec::new(),
            mention_everyone: false,
            mention_roles: Vec::new(),
            mentions: Vec::new(),
            pinned: false,
            reactions: Vec::new(),
            reference: None,
            referenced_message: None,
            role_subscription_data: None,
            sticker_items: Vec::new(),
            timestamp: Timestamp::from_secs(1_632_072_645).expect("non zero"),
            thread: None,
            tts: false,
            webhook_id: None,
        }
    }

    fn reaction() -> GatewayReaction {
        GatewayReaction {
            channel_id: Id::new(2),
            emoji: ReactionType::Unicode {
                name: "🍎".to_owned(),
            },
            guild_id: Some(Id::new(1)),
            member: None,
            message_author_id: None,
            message_id: Id::new(4),
            user_id: Id::new(3),
        }
    }

    #[allow(deprecated)]
    fn button() -> Interaction {
        Interaction {
            app_permissions: Some(Permissions::SEND_MESSAGES),
            application_id: Id::new(1),
            channel: Some(Channel {
                bitrate: None,
                guild_id: None,
                id: Id::new(400),
                kind: ChannelType::GuildText,
                last_message_id: None,
                last_pin_timestamp: None,
                name: None,
                nsfw: None,
                owner_id: None,
                parent_id: None,
                permission_overwrites: None,
                position: None,
                rate_limit_per_user: None,
                recipients: None,
                rtc_region: None,
                topic: None,
                user_limit: None,
                application_id: None,
                applied_tags: None,
                available_tags: None,
                default_auto_archive_duration: None,
                default_forum_layout: None,
                default_reaction_emoji: None,
                default_sort_order: None,
                default_thread_rate_limit_per_user: None,
                flags: None,
                icon: None,
                invitable: None,
                managed: None,
                member: None,
                member_count: None,
                message_count: None,
                newly_created: None,
                thread_metadata: None,
                video_quality_mode: None,
            }),
            channel_id: None,
            data: Some(InteractionData::MessageComponent(Box::new(
                MessageComponentInteractionData {
                    custom_id: String::from("Click"),
                    component_type: ComponentType::Button,
                    resolved: None,
                    values: Vec::new(),
                },
            ))),
            guild_id: Some(Id::new(3)),
            guild_locale: None,
            id: Id::new(4),
            kind: InteractionType::MessageComponent,
            locale: Some("en-GB".to_owned()),
            member: None,
            message: Some(message()),
            token: String::from("token"),
            user: Some(User {
                accent_color: None,
                avatar: None,
                avatar_decoration: None,
                banner: None,
                bot: false,
                discriminator: 1,
                email: None,
                flags: None,
                global_name: Some("test".to_owned()),
                id: Id::new(2),
                locale: None,
                mfa_enabled: None,
                name: "twilight".to_owned(),
                premium_type: None,
                public_flags: None,
                system: None,
                verified: None,
            }),
        }
    }

    /// Test that if a receiver drops their end, the result properly counts the
    /// statistic.
    #[tokio::test]
    async fn test_dropped() {
        let standby = Standby::new();
        let guild_id = Id::new(1);

        {
            let _rx = standby.wait_for(guild_id, move |_: &Event| false);
        }

        let results = standby.process(&Event::RoleDelete(RoleDelete {
            guild_id,
            role_id: Id::new(2),
        }));

        assert_eq!(1, results.dropped());
        assert_eq!(0, results.fulfilled());
        assert_eq!(0, results.sent());
    }

    /// Test that both events in guild 1 is matched but the event in guild 2 is
    /// not matched by testing the returned matched amount.
    #[tokio::test]
    async fn test_matched() {
        fn check(event: &Event, guild_id: Id<GuildMarker>) -> bool {
            matches!(event, Event::RoleDelete(e) if e.guild_id == guild_id)
        }

        let standby = Standby::new();
        let guild_id_one = Id::new(1);
        let guild_id_two = Id::new(2);
        let _one = standby.wait_for(guild_id_one, move |event: &Event| {
            check(event, guild_id_one)
        });
        let _two = standby.wait_for(guild_id_one, move |event: &Event| {
            check(event, guild_id_one)
        });
        let _three = standby.wait_for(guild_id_two, move |event: &Event| {
            check(event, guild_id_two)
        });

        let results = standby.process(&Event::RoleDelete(RoleDelete {
            guild_id: Id::new(1),
            role_id: Id::new(2),
        }));

        assert_eq!(0, results.dropped());
        assert_eq!(2, results.fulfilled());
        assert_eq!(0, results.sent());
    }

    /// Test that the [`ProcessResults::sent`] counter increments if a match is
    /// sent to it.
    #[tokio::test]
    async fn test_sent() {
        let standby = Standby::new();
        let guild_id = Id::new(1);

        let _rx = standby.wait_for_stream(guild_id, move |_: &Event| true);

        let results = standby.process(&Event::RoleDelete(RoleDelete {
            guild_id,
            role_id: Id::new(2),
        }));

        assert_eq!(0, results.dropped());
        assert_eq!(0, results.fulfilled());
        assert_eq!(1, results.sent());
    }

    /// Test basic functionality of the [`Standby::wait_for`] method.
    #[tokio::test]
    async fn test_wait_for() {
        let standby = Standby::new();
        let wait = standby.wait_for(
            Id::new(1),
            |event: &Event| matches!(event, Event::RoleDelete(e) if e.guild_id.get() == 1),
        );
        standby.process(&Event::RoleDelete(RoleDelete {
            guild_id: Id::new(1),
            role_id: Id::new(2),
        }));

        assert_eq!(
            wait.await.unwrap(),
            Event::RoleDelete(RoleDelete {
                guild_id: Id::new(1),
                role_id: Id::new(2),
            })
        );
        assert!(standby.guilds.is_empty());
    }

    /// Test basic functionality of the [`Standby::wait_for_stream`] method.
    #[tokio::test]
    async fn test_wait_for_stream() {
        let standby = Standby::new();
        let mut stream = standby.wait_for_stream(
            Id::new(1),
            |event: &Event| matches!(event, Event::RoleDelete(e) if e.guild_id.get() == 1),
        );
        standby.process(&Event::RoleDelete(RoleDelete {
            guild_id: Id::new(1),
            role_id: Id::new(2),
        }));
        standby.process(&Event::RoleDelete(RoleDelete {
            guild_id: Id::new(1),
            role_id: Id::new(3),
        }));

        assert_eq!(
            stream.next().await,
            Some(Event::RoleDelete(RoleDelete {
                guild_id: Id::new(1),
                role_id: Id::new(2)
            }))
        );
        assert_eq!(
            stream.next().await,
            Some(Event::RoleDelete(RoleDelete {
                guild_id: Id::new(1),
                role_id: Id::new(3)
            }))
        );
        assert!(!standby.guilds.is_empty());
        drop(stream);
        standby.process(&Event::RoleDelete(RoleDelete {
            guild_id: Id::new(1),
            role_id: Id::new(4),
        }));
        assert!(standby.guilds.is_empty());
    }

    /// Test basic functionality of the [`Standby::wait_for_event`] method.
    #[tokio::test]
    async fn test_wait_for_event() {
        let ready = Ready {
            application: PartialApplication {
                flags: ApplicationFlags::empty(),
                id: Id::new(1),
            },
            guilds: Vec::new(),
            resume_gateway_url: "wss://gateway.discord.gg".into(),
            session_id: String::new(),
            shard: Some(ShardId::new(5, 7)),
            user: CurrentUser {
                accent_color: None,
                avatar: None,
                banner: None,
                bot: false,
                discriminator: 1,
                email: None,
                id: Id::new(1),
                mfa_enabled: true,
                name: "twilight".to_owned(),
                verified: Some(false),
                premium_type: None,
                public_flags: None,
                flags: None,
                locale: None,
            },
            version: 6,
        };
        let event = Event::Ready(Box::new(ready));

        let standby = Standby::new();
        let wait = standby.wait_for_event(|event: &Event| match event {
            Event::Ready(ready) => ready.shard.map_or(false, |id| id.number() == 5),
            _ => false,
        });
        assert!(!standby.events.is_empty());
        standby.process(&event);

        assert_eq!(event, wait.await.unwrap());
        assert!(standby.events.is_empty());
    }

    /// Test basic functionality of the [`Standby::wait_for_event_stream`]
    /// method.
    #[tokio::test]
    async fn test_wait_for_event_stream() {
        let standby = Standby::new();
        let mut stream =
            standby.wait_for_event_stream(|event: &Event| event.kind() == EventType::Resumed);
        standby.process(&Event::Resumed);
        assert_eq!(stream.next().await, Some(Event::Resumed));
        assert!(!standby.events.is_empty());
        drop(stream);
        standby.process(&Event::Resumed);
        assert!(standby.events.is_empty());
    }

    /// Test basic functionality of the [`Standby::wait_for_message`] method.
    #[tokio::test]
    async fn test_wait_for_message() {
        let message = message();
        let event = Event::MessageCreate(Box::new(MessageCreate(message)));

        let standby = Standby::new();
        let wait = standby.wait_for_message(Id::new(1), |message: &MessageCreate| {
            message.author.id.get() == 2
        });
        standby.process(&event);

        assert_eq!(3, wait.await.map(|msg| msg.id.get()).unwrap());
        assert!(standby.messages.is_empty());
    }

    /// Test basic functionality of the [`Standby::wait_for_message_stream`]
    /// method.
    #[tokio::test]
    async fn test_wait_for_message_stream() {
        let standby = Standby::new();
        let mut stream = standby.wait_for_message_stream(Id::new(1), |_: &MessageCreate| true);
        standby.process(&Event::MessageCreate(Box::new(MessageCreate(message()))));
        standby.process(&Event::MessageCreate(Box::new(MessageCreate(message()))));

        assert!(stream.next().await.is_some());
        assert!(stream.next().await.is_some());
        drop(stream);
        assert_eq!(1, standby.messages.len());
        standby.process(&Event::MessageCreate(Box::new(MessageCreate(message()))));
        assert!(standby.messages.is_empty());
    }

    /// Test basic functionality of the [`Standby::wait_for_reaction`] method.
    #[tokio::test]
    async fn test_wait_for_reaction() {
        let event = Event::ReactionAdd(Box::new(ReactionAdd(reaction())));

        let standby = Standby::new();
        let wait = standby.wait_for_reaction(Id::new(4), |reaction: &ReactionAdd| {
            reaction.user_id.get() == 3
        });

        standby.process(&event);

        assert_eq!(
            Id::new(3),
            wait.await.map(|reaction| reaction.user_id).unwrap()
        );
        assert!(standby.reactions.is_empty());
    }

    /// Test basic functionality of the [`Standby::wait_for_reaction_stream`]
    /// method.
    #[tokio::test]
    async fn test_wait_for_reaction_stream() {
        let standby = Standby::new();
        let mut stream = standby.wait_for_reaction_stream(Id::new(4), |_: &ReactionAdd| true);
        standby.process(&Event::ReactionAdd(Box::new(ReactionAdd(reaction()))));
        standby.process(&Event::ReactionAdd(Box::new(ReactionAdd(reaction()))));

        assert!(stream.next().await.is_some());
        assert!(stream.next().await.is_some());
        drop(stream);
        assert_eq!(1, standby.reactions.len());
        standby.process(&Event::ReactionAdd(Box::new(ReactionAdd(reaction()))));
        assert!(standby.reactions.is_empty());
    }

    /// Assert that Standby processing some non-matching events will not affect
    /// the matching of a later event.
    #[tokio::test]
    async fn test_wait_for_component() {
        let event = Event::InteractionCreate(Box::new(InteractionCreate(button())));

        let standby = Standby::new();
        let wait = standby.wait_for_component(Id::new(3), |button: &Interaction| {
            button.author_id() == Some(Id::new(2))
        });

        standby.process(&event);

        assert_eq!(
            Some(Id::new(2)),
            wait.await.map(|button| button.author_id()).unwrap()
        );
        assert!(standby.components.is_empty());
    }

    #[tokio::test]
    async fn test_wait_for_component_stream() {
        let standby = Standby::new();
        let mut stream = standby.wait_for_component_stream(Id::new(3), |_: &Interaction| true);
        standby.process(&Event::InteractionCreate(Box::new(InteractionCreate(
            button(),
        ))));
        standby.process(&Event::InteractionCreate(Box::new(InteractionCreate(
            button(),
        ))));

        assert!(stream.next().await.is_some());
        assert!(stream.next().await.is_some());
        drop(stream);
        assert_eq!(1, standby.components.len());
        standby.process(&Event::InteractionCreate(Box::new(InteractionCreate(
            button(),
        ))));
        assert!(standby.components.is_empty());
    }

    #[tokio::test]
    async fn test_handles_wrong_events() {
        let standby = Standby::new();
        let wait = standby.wait_for_event(|event: &Event| event.kind() == EventType::Resumed);

        standby.process(&Event::GatewayHeartbeatAck);
        standby.process(&Event::GatewayHeartbeatAck);
        standby.process(&Event::Resumed);

        assert_eq!(Event::Resumed, wait.await.unwrap());
    }

    /// Test that generic event handlers will be given the opportunity to
    /// process events with specific handlers (message creates, reaction adds)
    /// and guild events. Similarly, guild handlers should be able to process
    /// specific handler events as well.
    #[tokio::test]
    async fn test_process_nonspecific_handling() {
        let standby = Standby::new();

        // generic event handler gets message creates
        let wait = standby.wait_for_event(|event: &Event| event.kind() == EventType::MessageCreate);
        standby.process(&Event::MessageCreate(Box::new(MessageCreate(message()))));
        assert!(matches!(wait.await, Ok(Event::MessageCreate(_))));

        // generic event handler gets reaction adds
        let wait = standby.wait_for_event(|event: &Event| event.kind() == EventType::ReactionAdd);
        standby.process(&Event::ReactionAdd(Box::new(ReactionAdd(reaction()))));
        assert!(matches!(wait.await, Ok(Event::ReactionAdd(_))));

        // generic event handler gets other guild events
        let wait = standby.wait_for_event(|event: &Event| event.kind() == EventType::RoleDelete);
        standby.process(&Event::RoleDelete(RoleDelete {
            guild_id: Id::new(1),
            role_id: Id::new(2),
        }));
        assert!(matches!(wait.await, Ok(Event::RoleDelete(_))));

        // guild event handler gets message creates or reaction events
        let wait = standby.wait_for(Id::new(1), |event: &Event| {
            event.kind() == EventType::ReactionAdd
        });
        standby.process(&Event::ReactionAdd(Box::new(ReactionAdd(reaction()))));
        assert!(matches!(wait.await, Ok(Event::ReactionAdd(_))));
    }
}