Skip to main content

djs_model/gateway/
intents.rs

1use crate::macros::int_flags;
2
3int_flags! {
4    /// [Gateway intents](https://docs.discord.com/developers/events/gateway#gateway-intents):
5    /// the groups of events a client subscribes to when identifying.
6    ///
7    /// Only request what the bot needs — every intent increases the amount of data Discord
8    /// sends and the client must process.
9    ///
10    /// Three intents are **privileged** ([`Intents::PRIVILEGED`]) and must be enabled in the
11    /// Developer Portal (and, for bots in 100+ guilds, approved by Discord) before they can be
12    /// requested; otherwise the gateway closes with
13    /// [`CloseCode::DisallowedIntents`](crate::gateway::CloseCode::DisallowedIntents).
14    ///
15    /// Unknown bits received from or sent to Discord are preserved.
16    ///
17    /// discord.js counterpart: `GatewayIntentBits` / `IntentsBitField`.
18    ///
19    /// # Examples
20    ///
21    /// ```
22    /// use djs_model::gateway::Intents;
23    ///
24    /// let intents = Intents::GUILDS | Intents::GUILD_MESSAGES | Intents::MESSAGE_CONTENT;
25    /// assert_eq!(intents.bits(), 33281);
26    /// assert!(intents.intersects(Intents::PRIVILEGED));
27    /// ```
28    pub struct Intents: u64 {
29        /// Guild lifecycle, roles, channels, threads and stage instances.
30        const GUILDS = 1 << 0;
31        /// Guild member add/update/remove and thread member updates. **Privileged.**
32        const GUILD_MEMBERS = 1 << 1;
33        /// Bans and audit log entry creation.
34        const GUILD_MODERATION = 1 << 2;
35        /// Emoji, sticker and soundboard sound updates.
36        const GUILD_EXPRESSIONS = 1 << 3;
37        /// Integration create/update/delete.
38        const GUILD_INTEGRATIONS = 1 << 4;
39        /// Webhook updates.
40        const GUILD_WEBHOOKS = 1 << 5;
41        /// Invite create/delete.
42        const GUILD_INVITES = 1 << 6;
43        /// Voice state updates and voice channel effects.
44        const GUILD_VOICE_STATES = 1 << 7;
45        /// Presence updates. **Privileged.**
46        const GUILD_PRESENCES = 1 << 8;
47        /// Messages in guilds.
48        const GUILD_MESSAGES = 1 << 9;
49        /// Reactions in guilds.
50        const GUILD_MESSAGE_REACTIONS = 1 << 10;
51        /// Typing indicators in guilds.
52        const GUILD_MESSAGE_TYPING = 1 << 11;
53        /// Messages in direct messages.
54        const DIRECT_MESSAGES = 1 << 12;
55        /// Reactions in direct messages.
56        const DIRECT_MESSAGE_REACTIONS = 1 << 13;
57        /// Typing indicators in direct messages.
58        const DIRECT_MESSAGE_TYPING = 1 << 14;
59        /// Access to message content, embeds, attachments, components and polls.
60        /// **Privileged.**
61        ///
62        /// Not an event group: without it, those message fields are empty except in DMs and
63        /// messages that mention the bot.
64        const MESSAGE_CONTENT = 1 << 15;
65        /// Guild scheduled events.
66        const GUILD_SCHEDULED_EVENTS = 1 << 16;
67        /// Auto moderation rule create/update/delete.
68        const AUTO_MODERATION_CONFIGURATION = 1 << 20;
69        /// Auto moderation action execution.
70        const AUTO_MODERATION_EXECUTION = 1 << 21;
71        /// Poll votes in guilds.
72        const GUILD_MESSAGE_POLLS = 1 << 24;
73        /// Poll votes in direct messages.
74        const DIRECT_MESSAGE_POLLS = 1 << 25;
75    }
76}
77
78impl Intents {
79    /// The privileged intents: [`GUILD_MEMBERS`](Self::GUILD_MEMBERS),
80    /// [`GUILD_PRESENCES`](Self::GUILD_PRESENCES) and [`MESSAGE_CONTENT`](Self::MESSAGE_CONTENT).
81    pub const PRIVILEGED: Self = Self::GUILD_MEMBERS
82        .union(Self::GUILD_PRESENCES)
83        .union(Self::MESSAGE_CONTENT);
84
85    /// Every known intent that is not privileged.
86    pub const NON_PRIVILEGED: Self = Self::all().difference(Self::PRIVILEGED);
87}
88
89#[cfg(test)]
90mod tests {
91    use super::*;
92
93    #[test]
94    fn privileged() {
95        assert_eq!(Intents::PRIVILEGED.bits(), (1 << 1) | (1 << 8) | (1 << 15));
96        assert!(!Intents::NON_PRIVILEGED.intersects(Intents::PRIVILEGED));
97        assert_eq!(
98            Intents::NON_PRIVILEGED | Intents::PRIVILEGED,
99            Intents::all()
100        );
101    }
102
103    #[test]
104    fn serde_preserves_unknown_bits() {
105        let intents: Intents = serde_json::from_str("1073741825").unwrap();
106        assert!(intents.contains(Intents::GUILDS));
107        assert_eq!(serde_json::to_string(&intents).unwrap(), "1073741825");
108    }
109}