Skip to main content

djs_model/gateway/
close_code.rs

1use crate::macros::int_enum;
2
3int_enum! {
4    /// A [gateway close event code](https://docs.discord.com/developers/topics/opcodes-and-status-codes#gateway-gateway-close-event-codes):
5    /// the reason Discord gives when it closes the WebSocket connection.
6    ///
7    /// Use [`CloseCode::can_reconnect`] to decide whether reconnecting is allowed.
8    ///
9    /// discord.js counterpart: `GatewayCloseCodes` (re-exported from discord-api-types).
10    pub enum CloseCode: u16 {
11        /// Something went wrong on Discord's side. Reconnecting is allowed.
12        UnknownError = 4000,
13        /// The client sent an invalid opcode or an invalid payload for an opcode.
14        UnknownOpcode = 4001,
15        /// The client sent a payload Discord could not decode.
16        DecodeError = 4002,
17        /// The client sent a payload before identifying, or the session was invalidated.
18        NotAuthenticated = 4003,
19        /// The token sent in the identify payload is incorrect. **Do not reconnect.**
20        AuthenticationFailed = 4004,
21        /// The client sent more than one identify payload.
22        AlreadyAuthenticated = 4005,
23        /// The sequence number sent when resuming was invalid. Reconnect and start a new session.
24        InvalidSequence = 4007,
25        /// The client is sending payloads too quickly.
26        RateLimited = 4008,
27        /// The session timed out. Reconnect and start a new session.
28        SessionTimedOut = 4009,
29        /// The client sent an invalid shard when identifying. **Do not reconnect.**
30        InvalidShard = 4010,
31        /// The session would have handled too many guilds; sharding is required.
32        /// **Do not reconnect.**
33        ShardingRequired = 4011,
34        /// The client requested an invalid gateway version. **Do not reconnect.**
35        InvalidApiVersion = 4012,
36        /// The client sent an invalid intent bitfield. **Do not reconnect.**
37        InvalidIntents = 4013,
38        /// The client requested an intent it is not enabled or approved for (usually a privileged
39        /// intent). **Do not reconnect.**
40        DisallowedIntents = 4014,
41    }
42}
43
44impl CloseCode {
45    /// Returns `true` if Discord allows reconnecting after receiving this close code.
46    ///
47    /// Unknown codes are assumed to allow reconnecting, matching the documented behaviour of
48    /// unexpected disconnects.
49    ///
50    /// discord.js counterpart: none (`@discordjs/ws` decides inside the shard).
51    ///
52    /// # Examples
53    ///
54    /// ```
55    /// use djs_model::gateway::CloseCode;
56    ///
57    /// assert!(CloseCode::SessionTimedOut.can_reconnect());
58    /// assert!(!CloseCode::AuthenticationFailed.can_reconnect());
59    /// ```
60    #[must_use]
61    pub const fn can_reconnect(self) -> bool {
62        !matches!(
63            self,
64            Self::AuthenticationFailed
65                | Self::InvalidShard
66                | Self::ShardingRequired
67                | Self::InvalidApiVersion
68                | Self::InvalidIntents
69                | Self::DisallowedIntents
70        )
71    }
72}