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
use super::{
    builder::ShardBuilder,
    command::Command,
    config::Config,
    emitter::Emitter,
    event::Events,
    json,
    processor::{ConnectingErrorType, Latency, Session, ShardProcessor},
    raw_message::Message,
    stage::Stage,
};
use crate::Intents;
use leaky_bucket_lite::LeakyBucket;
use serde::{Deserialize, Serialize};
use std::{
    borrow::Cow,
    error::Error,
    fmt::{Display, Formatter, Result as FmtResult},
    sync::{atomic::Ordering, Arc, Mutex},
};
use tokio::{
    sync::{watch::Receiver as WatchReceiver, OnceCell},
    task::JoinHandle,
    time::Instant,
};
use tokio_tungstenite::tungstenite::protocol::{
    frame::coding::CloseCode, CloseFrame as TungsteniteCloseFrame,
};

/// Sending a command failed.
#[derive(Debug)]
pub struct CommandError {
    kind: CommandErrorType,
    source: Option<Box<dyn Error + Send + Sync>>,
}

impl CommandError {
    /// Immutable reference to the type of error that occurred.
    #[must_use = "retrieving the type has no effect if left unused"]
    pub const fn kind(&self) -> &CommandErrorType {
        &self.kind
    }

    /// Consume the error, returning the source error if there is any.
    #[must_use = "consuming the error and retrieving the source has no effect if left unused"]
    pub fn into_source(self) -> Option<Box<dyn Error + Send + Sync>> {
        self.source
    }

    /// Consume the error, returning the owned error type and the source error.
    #[must_use = "consuming the error into its parts has no effect if left unused"]
    pub fn into_parts(self) -> (CommandErrorType, Option<Box<dyn Error + Send + Sync>>) {
        (self.kind, None)
    }
}

impl CommandError {
    pub(crate) fn from_send(error: SendError) -> Self {
        let (kind, source) = error.into_parts();

        let new_kind = match kind {
            SendErrorType::HeartbeaterNotStarted => CommandErrorType::HeartbeaterNotStarted,
            SendErrorType::Sending => CommandErrorType::Sending,
            SendErrorType::SessionInactive => CommandErrorType::SessionInactive,
        };

        Self {
            kind: new_kind,
            source,
        }
    }
}

impl Display for CommandError {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        match &self.kind {
            CommandErrorType::HeartbeaterNotStarted => {
                f.write_str("heartbeater task hasn't been started yet")
            }
            CommandErrorType::Sending => {
                f.write_str("sending the message over the websocket failed")
            }
            CommandErrorType::Serializing => f.write_str("serializing the value as json failed"),
            CommandErrorType::SessionInactive => Display::fmt(&SessionInactiveError, f),
        }
    }
}

impl Error for CommandError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        self.source
            .as_ref()
            .map(|source| &**source as &(dyn Error + 'static))
    }
}

/// Type of [`CommandError`] that occurred.
#[derive(Debug)]
#[non_exhaustive]
pub enum CommandErrorType {
    /// Heartbeater task has not been started yet.
    HeartbeaterNotStarted,
    /// Sending the payload over the WebSocket failed. This is indicative of a
    /// shutdown shard.
    Sending,
    /// Serializing the payload as JSON failed.
    Serializing,
    /// Shard's session is inactive because the shard hasn't been started.
    SessionInactive,
}

/// Shard's session is inactive.
///
/// This means that the shard has not yet been started.
#[derive(Debug)]
#[non_exhaustive]
pub struct SessionInactiveError;

impl Display for SessionInactiveError {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        f.write_str("the shard session is inactive and was not started")
    }
}

impl Error for SessionInactiveError {}

/// Starting a shard and connecting to the gateway failed.
#[derive(Debug)]
pub struct SendError {
    kind: SendErrorType,
    source: Option<Box<dyn Error + Send + Sync>>,
}

impl SendError {
    /// Immutable reference to the type of error that occurred.
    #[must_use = "retrieving the type has no effect if left unused"]
    pub const fn kind(&self) -> &SendErrorType {
        &self.kind
    }

    /// Consume the error, returning the source error if there is any.
    #[must_use = "consuming the error and retrieving the source has no effect if left unused"]
    pub fn into_source(self) -> Option<Box<dyn Error + Send + Sync>> {
        self.source
    }

    /// Consume the error, returning the owned error type and the source error.
    #[must_use = "consuming the error into its parts has no effect if left unused"]
    pub fn into_parts(self) -> (SendErrorType, Option<Box<dyn Error + Send + Sync>>) {
        (self.kind, None)
    }
}

impl Display for SendError {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        match &self.kind {
            SendErrorType::HeartbeaterNotStarted { .. } => {
                f.write_str("heartbeater task hasn't been started yet")
            }
            SendErrorType::Sending { .. } => {
                f.write_str("sending the message over the websocket failed")
            }
            SendErrorType::SessionInactive { .. } => f.write_str("shard hasn't been started"),
        }
    }
}

impl Error for SendError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        self.source
            .as_ref()
            .map(|source| &**source as &(dyn Error + 'static))
    }
}

/// Type of [`SendError`] that occurred.
#[derive(Debug)]
#[non_exhaustive]
pub enum SendErrorType {
    /// Heartbeater task has not been started yet.
    HeartbeaterNotStarted,
    /// Sending the payload over the WebSocket failed. This is indicative of a
    /// shard that isn't properly running.
    Sending,
    /// Shard's session is inactive because the shard hasn't been started.
    SessionInactive,
}

/// Starting a shard and connecting to the gateway failed.
#[derive(Debug)]
pub struct ShardStartError {
    pub(super) kind: ShardStartErrorType,
    pub(super) source: Option<Box<dyn Error + Send + Sync>>,
}

impl ShardStartError {
    /// Immutable reference to the type of error that occurred.
    #[must_use = "retrieving the type has no effect if left unused"]
    pub const fn kind(&self) -> &ShardStartErrorType {
        &self.kind
    }

    /// Consume the error, returning the source error if there is any.
    #[must_use = "consuming the error and retrieving the source has no effect if left unused"]
    pub fn into_source(self) -> Option<Box<dyn Error + Send + Sync>> {
        self.source
    }

    /// Consume the error, returning the owned error type and the source error.
    #[must_use = "consuming the error into its parts has no effect if left unused"]
    pub fn into_parts(self) -> (ShardStartErrorType, Option<Box<dyn Error + Send + Sync>>) {
        (self.kind, None)
    }
}

impl Display for ShardStartError {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        match &self.kind {
            ShardStartErrorType::AlreadyStarted => {
                f.write_str("shard has already been previously started")
            }
            ShardStartErrorType::Establishing => f.write_str("establishing the connection failed"),
            ShardStartErrorType::ParsingGatewayUrl { url } => {
                f.write_str("the gateway url `")?;
                f.write_str(url)?;

                f.write_str("` is invalid")
            }
        }
    }
}

impl Error for ShardStartError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        self.source
            .as_ref()
            .map(|source| &**source as &(dyn Error + 'static))
    }
}

/// Type of [`ShardStartError`] that occurred.
#[derive(Debug)]
#[non_exhaustive]
pub enum ShardStartErrorType {
    /// Shard has already been previously started.
    ///
    /// Shards can't be started multiple times; you need to create a new
    /// instance of the shard.
    AlreadyStarted,
    /// Establishing a connection to the gateway failed.
    Establishing,
    /// Parsing the gateway URL provided by Discord to connect to the gateway
    /// failed due to an invalid URL.
    ParsingGatewayUrl {
        /// URL that couldn't be parsed.
        url: String,
    },
}

/// Information about a shard, including its latency, current session sequence,
/// and connection stage.
#[derive(Clone, Debug)]
pub struct Information {
    id: u64,
    latency: Latency,
    ratelimit_refill: Option<Instant>,
    ratelimit_requests: Option<u32>,
    session_id: Option<Box<str>>,
    gateway_url: String,
    seq: u64,
    stage: Stage,
}

impl Information {
    /// Return the ID of the shard.
    pub const fn id(&self) -> u64 {
        self.id
    }

    /// Return an immutable reference to the latency information for the shard.
    ///
    /// This includes the average latency over all time, and the latency
    /// information for the 5 most recent heartbeats.
    pub const fn latency(&self) -> &Latency {
        &self.latency
    }

    /// When the ratelimiter will next refill the [`ratelimit_requests`].
    ///
    /// This will be `None` if payload ratelimiting has been disabled.
    ///
    /// [`ratelimit_requests`]: Self::ratelimit_requests
    pub const fn ratelimit_refill(&self) -> Option<Instant> {
        self.ratelimit_refill
    }

    /// Number of requests remaining until the next [`ratelimit_refill`].
    ///
    /// This will be `None` if payload ratelimiting has been disabled.
    ///
    /// [`ratelimit_refill`]: Self::ratelimit_refill
    pub const fn ratelimit_requests(&self) -> Option<u32> {
        self.ratelimit_requests
    }

    /// Return an immutable reference to the session ID of the shard.
    pub fn session_id(&self) -> Option<&str> {
        self.session_id.as_deref()
    }

    /// Return an immutable reference to the gateway url of the shard.
    pub fn gateway_url(&self) -> &str {
        self.gateway_url.as_str()
    }

    /// Current sequence of the connection.
    ///
    /// This is the number of the event that was received this session (without
    /// reconnecting). A larger number typically correlates that the shard has
    /// been connected for a longer time, while a smaller number typically
    /// correlates to meaning that it's been connected for a less amount of
    /// time.
    pub const fn seq(&self) -> u64 {
        self.seq
    }

    /// Current stage of the shard.
    ///
    /// For example, once a shard is fully booted then it will be [`Connected`].
    ///
    /// [`Connected`]: Stage::Connected
    pub const fn stage(&self) -> Stage {
        self.stage
    }
}

/// Details to resume a gateway session.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ResumeSession {
    /// URL used to resume
    #[serde(default)]
    pub resume_url: Option<String>,
    /// ID of the session being resumed.
    pub session_id: String,
    /// Last received event sequence number.
    pub sequence: u64,
}

/// Shard to run and manage a session with the gateway.
///
/// Shards are responsible for handling incoming events, process events relevant
/// to the operation of shards - such as requests from the gateway to re-connect
/// or invalidate a session - and then pass the events on to the user via an
/// event stream.
///
/// Shards will [go through a queue][`queue`] to initialize new ratelimited
/// sessions with the ratelimit. Refer to Discord's [documentation][docs:shards]
/// on shards to have a better understanding of what they are.
///
/// # Using a shard in multiple tasks
///
/// To use a shard instance in multiple tasks, consider wrapping it in an
/// [`std::sync::Arc`] or [`std::rc::Rc`].
///
/// # Examples
///
/// Create and start a shard and print new and deleted messages:
///
/// ```no_run
/// use futures::stream::StreamExt;
/// use std::env;
/// use twilight_gateway::{Event, EventTypeFlags, Intents, Shard};
///
/// # #[tokio::main] async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// // Use the value of the "DISCORD_TOKEN" environment variable as the bot's
/// // token. Of course, you may pass this into your program however you want.
/// let token = env::var("DISCORD_TOKEN")?;
/// let event_types = EventTypeFlags::MESSAGE_CREATE | EventTypeFlags::MESSAGE_DELETE;
///
/// let (shard, mut events) = Shard::builder(token, Intents::GUILD_MESSAGES)
///     .event_types(event_types)
///     .build();
///
/// // Start the shard.
/// shard.start().await?;
///
/// // Create a loop of only new message and deleted message events.
///
/// while let Some(event) = events.next().await {
///     match event {
///         Event::MessageCreate(message) => {
///             println!("message received with content: {}", message.content);
///         }
///         Event::MessageDelete(message) => {
///             println!("message with ID {} deleted", message.id);
///         }
///         _ => {}
///     }
/// }
/// # Ok(()) }
/// ```
///
/// [`queue`]: crate::queue
/// [docs:shards]: https://discord.com/developers/docs/topics/gateway#sharding
#[derive(Debug)]
pub struct Shard {
    config: Arc<Config>,
    emitter: Mutex<Option<Emitter>>,
    processor_handle: OnceCell<JoinHandle<()>>,
    session: OnceCell<WatchReceiver<Arc<Session>>>,
}

impl Shard {
    /// Create a new unconfigured shard.
    ///
    /// Use [`start`] to initiate the gateway session.
    ///
    /// # Examples
    ///
    /// Create a new shard and start it, wait a second, and then print its
    /// current connection stage:
    ///
    /// ```no_run
    /// use std::{env, time::Duration};
    /// use tokio::time as tokio_time;
    /// use twilight_gateway::{Intents, Shard};
    ///
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let token = env::var("DISCORD_TOKEN")?;
    ///
    /// let intents = Intents::GUILD_MESSAGES | Intents::GUILD_MESSAGE_TYPING;
    /// let (shard, _) = Shard::new(token, intents);
    /// shard.start().await?;
    ///
    /// tokio_time::sleep(Duration::from_secs(1)).await;
    ///
    /// let info = shard.info()?;
    /// println!("Shard stage: {}", info.stage());
    /// # Ok(()) }
    /// ```
    ///
    /// [`start`]: Self::start
    pub fn new(token: String, intents: Intents) -> (Self, Events) {
        Self::builder(token, intents).build()
    }

    pub(crate) fn new_with_config(config: Config) -> (Self, Events) {
        let config = Arc::new(config);
        let event_types = config.event_types();

        let (emitter, rx) = Emitter::new(event_types);

        let this = Self {
            config,
            emitter: Mutex::new(Some(emitter)),
            processor_handle: OnceCell::new(),
            session: OnceCell::new(),
        };

        (this, Events::new(event_types, rx))
    }

    /// Create a builder to configure and construct a shard.
    ///
    /// Refer to the builder for more information.
    pub fn builder(token: String, intents: Intents) -> ShardBuilder {
        ShardBuilder::new(token, intents)
    }

    /// Return an immutable reference to the configuration used for this client.
    pub fn config(&self) -> &Config {
        &self.config
    }

    /// Start the shard, connecting it to the gateway and starting the process
    /// of receiving and processing events.
    ///
    /// The same shard can't be started multiple times. If you stop a shard via
    /// [`shutdown`] or [`shutdown_resumable`] you need to create a new instance
    /// of the shard.
    ///
    /// # Errors
    ///
    /// Returns a [`ShardStartErrorType::AlreadyStarted`] error type if the
    /// shard has already been started.
    ///
    /// Returns a [`ShardStartErrorType::Establishing`] error type if
    /// establishing a connection to the gateway failed.
    ///
    /// Returns a [`ShardStartErrorType::ParsingGatewayUrl`] error type if the
    /// gateway URL couldn't be parsed.
    ///
    /// [`shutdown_resumable`]: Self::shutdown_resumable
    /// [`shutdown`]: Self::shutdown
    pub async fn start(&self) -> Result<(), ShardStartError> {
        let emitter = self
            .emitter
            .lock()
            .expect("emitter poisoned")
            .take()
            .ok_or(ShardStartError {
                kind: ShardStartErrorType::AlreadyStarted,
                source: None,
            })?;

        let config = Arc::clone(&self.config);
        let (processor, wrx) = ShardProcessor::new(config, emitter)
            .await
            .map_err(|source| {
                let (kind, source) = source.into_parts();

                let new_kind = match kind {
                    ConnectingErrorType::Establishing => ShardStartErrorType::Establishing,
                    ConnectingErrorType::ParsingUrl { url } => {
                        ShardStartErrorType::ParsingGatewayUrl { url }
                    }
                };

                ShardStartError {
                    source,
                    kind: new_kind,
                }
            })?;

        let handle = tokio::spawn(async {
            processor.run().await;

            tracing::debug!("shard processor future ended");
        });

        // We know that these haven't been set, so we can ignore the result.
        let _res = self.processor_handle.set(handle);
        let _session = self.session.set(wrx);

        Ok(())
    }

    /// Retrieve information about the running of the shard, such as the current
    /// connection stage.
    ///
    /// # Errors
    ///
    /// Returns a [`SessionInactiveError`] if the shard's session is inactive.
    pub fn info(&self) -> Result<Information, SessionInactiveError> {
        let session = self.session()?;

        let (ratelimit_requests, ratelimit_refill) = if let Some(limiter) = session.ratelimit.get()
        {
            (
                limiter.as_ref().map(LeakyBucket::tokens),
                limiter.as_ref().map(LeakyBucket::next_refill),
            )
        } else {
            return Err(SessionInactiveError);
        };

        Ok(Information {
            id: self.config().shard()[0],
            latency: session.heartbeats.latency(),
            ratelimit_refill,
            ratelimit_requests,
            gateway_url: self.config().gateway_url().to_owned(),
            session_id: session.id(),
            seq: session.seq(),
            stage: session.stage(),
        })
    }

    /// Send a command over the gateway.
    ///
    /// # Examples
    ///
    /// Updating the shard's presence after identifying can be done by sending
    /// an [`UpdatePresence`] command. For example, updating the active presence
    /// with the custom status "running on twilight":
    ///
    /// ```no_run
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use std::env;
    /// use twilight_gateway::{shard::Shard, Intents};
    /// use twilight_model::gateway::{
    ///     payload::outgoing::UpdatePresence,
    ///     presence::{Activity, ActivityType, MinimalActivity, Status},
    /// };
    ///
    /// let intents = Intents::GUILDS;
    /// let token = env::var("DISCORD_TOKEN")?;
    ///
    /// let (shard, _events) = Shard::new(token, intents);
    /// shard.start().await?;
    ///
    /// let minimal_activity = MinimalActivity {
    ///     kind: ActivityType::Custom,
    ///     name: "running on twilight".to_owned(),
    ///     url: None,
    /// };
    /// let command = UpdatePresence::new(
    ///     Vec::from([Activity::from(minimal_activity)]),
    ///     false,
    ///     Some(1),
    ///     Status::Online,
    /// )?;
    /// shard.command(&command).await?;
    /// # Ok(()) }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns a [`CommandErrorType::Sending`] error type if the message could
    /// not be sent over the websocket. This indicates the shard is currently
    /// restarting.
    ///
    /// Returns a [`CommandErrorType::Serializing`] error type if the provided
    /// value failed to serialize into JSON.
    ///
    /// Returns a [`CommandErrorType::SessionInactive`] error type if the shard
    /// has not been started.
    ///
    /// [`UpdatePresence`]: twilight_model::gateway::payload::outgoing::UpdatePresence
    pub async fn command(&self, value: &impl Command) -> Result<(), CommandError> {
        let json = json::to_vec(value).map_err(|source| CommandError {
            source: Some(Box::new(source)),
            kind: CommandErrorType::Serializing,
        })?;

        self.send(Message::Binary(json))
            .await
            .map_err(CommandError::from_send)
    }

    /// Send a raw websocket message.
    ///
    /// # Examples
    ///
    /// Send a ping message:
    ///
    /// ```no_run
    /// # #[tokio::main] async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use std::env;
    /// use twilight_gateway::{
    ///     shard::{raw_message::Message, Shard},
    ///     Intents,
    /// };
    ///
    /// let token = env::var("DISCORD_TOKEN")?;
    /// let (shard, _) = Shard::new(token, Intents::GUILDS);
    /// shard.start().await?;
    ///
    /// shard.send(Message::Ping(Vec::new())).await?;
    /// # Ok(()) }
    /// ```
    ///
    /// Send a normal close (you may prefer to use [`shutdown`]):
    ///
    /// ```no_run
    /// # #[tokio::main] async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use std::{borrow::Cow, env};
    /// use twilight_gateway::{
    ///     shard::{
    ///         raw_message::{CloseFrame, Message},
    ///         Shard,
    ///     },
    ///     Intents,
    /// };
    ///
    /// let token = env::var("DISCORD_TOKEN")?;
    /// let (shard, _) = Shard::new(token, Intents::GUILDS);
    /// shard.start().await?;
    ///
    /// let close = CloseFrame::from((1000, ""));
    /// let message = Message::Close(Some(close));
    /// shard.send(message).await?;
    /// # Ok(()) }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns a [`SendErrorType::HeartbeaterNotStarted`] error type if the
    /// shard hasn't started the heartbeater.
    ///
    /// Returns a [`SendErrorType::Sending`] error type if there is an issue
    /// with sending via the shard's session. This may occur when the shard is
    /// between sessions.
    ///
    /// Returns a [`SendErrorType::SessionInactive`] error type when the shard
    /// has not been started.
    ///
    /// [`shutdown`]: Self::shutdown
    pub async fn send(&self, message: Message) -> Result<(), SendError> {
        let session = self.session().map_err(|source| SendError {
            source: Some(Box::new(source)),
            kind: SendErrorType::SessionInactive,
        })?;

        // Getting the value of the OnceCell will only return None if it has not been
        // initialized yet, i.e. ratelimiting is enabled and HELLO has not been
        // received yet.
        if let Some(maybe_ratelimiter) = session.ratelimit.get() {
            // The value of the cell has been set, it will be Some if ratelimiting
            // is enabled, else None. We can ignore the second case.
            if let Some(ratelimiter) = maybe_ratelimiter {
                ratelimiter.acquire_one().await;
            }
        } else {
            return Err(SendError {
                kind: SendErrorType::HeartbeaterNotStarted,
                source: None,
            });
        }

        session
            .tx
            .send(message.into_tungstenite())
            .map_err(|source| SendError {
                kind: SendErrorType::Sending,
                source: Some(Box::new(source)),
            })
    }

    /// Shut down the shard.
    ///
    /// The shard will cleanly close the connection by sending a normal close
    /// code, causing Discord to show the bot as being offline. The session will
    /// not be resumable.
    pub fn shutdown(&self) {
        if let Some(processor_handle) = self.processor_handle.get() {
            processor_handle.abort();
        }

        if let Ok(session) = self.session() {
            // Since we're shutting down now, we don't care if it sends or not.
            let _res = session.close(Some(TungsteniteCloseFrame {
                code: CloseCode::Normal,
                reason: "".into(),
            }));
            session.stop_heartbeater();
        }
    }

    /// Shut down the shard in a resumable fashion.
    ///
    /// The shard will cleanly close the connection by sending a restart close
    /// code, causing Discord to keep the bot as showing online. The connection
    /// will be resumable by using the provided session resume information
    /// to [`ClusterBuilder::resume_sessions`].
    ///
    /// [`ClusterBuilder::resume_sessions`]: crate::cluster::ClusterBuilder::resume_sessions
    pub fn shutdown_resumable(&self) -> (u64, Option<ResumeSession>) {
        if let Some(processor_handle) = self.processor_handle.get() {
            processor_handle.abort();
        }

        let shard_id = self.config().shard()[0];

        let session = if let Ok(session) = self.session() {
            session
        } else {
            return (shard_id, None);
        };

        let _res = session.close(Some(TungsteniteCloseFrame {
            code: CloseCode::Restart,
            reason: Cow::from("Closing in a resumable way"),
        }));

        let session_id = session.id();
        let sequence = session.seq.load(Ordering::Relaxed);

        session.stop_heartbeater();

        let data = session_id.map(|id| ResumeSession {
            resume_url: session.resume_url().map(String::from),
            session_id: id.into_string(),
            sequence,
        });

        (shard_id, data)
    }

    /// Return a handle to the current session.
    ///
    /// # Errors
    ///
    /// Returns a [`SessionInactiveError`] if the shard's session is inactive.
    fn session(&self) -> Result<Arc<Session>, SessionInactiveError> {
        let session = self.session.get().ok_or(SessionInactiveError)?;

        Ok(Arc::clone(&session.borrow()))
    }
}

#[cfg(test)]
mod tests {
    use super::{
        CommandError, CommandErrorType, Information, ResumeSession, SendError, SendErrorType,
        SessionInactiveError, Shard, ShardStartError, ShardStartErrorType,
    };
    use static_assertions::{assert_fields, assert_impl_all};
    use std::{error::Error, fmt::Debug};

    assert_impl_all!(CommandErrorType: Debug, Send, Sync);
    assert_impl_all!(CommandError: Error, Send, Sync);
    assert_impl_all!(Information: Clone, Debug, Send, Sync);
    assert_impl_all!(ResumeSession: Clone, Debug, Send, Sync);
    assert_impl_all!(SendErrorType: Debug, Send, Sync);
    assert_impl_all!(SendError: Error, Send, Sync);
    assert_impl_all!(SessionInactiveError: Error, Send, Sync);
    assert_fields!(ShardStartErrorType::ParsingGatewayUrl: url);
    assert_impl_all!(ShardStartErrorType: Debug, Send, Sync);
    assert_impl_all!(ShardStartError: Error, Send, Sync);
    assert_impl_all!(Shard: Debug, Send, Sync);
}