titanium_model/
intents.rs

1//! Discord Gateway Intents
2//!
3//! Intents are a bitfield that controls which events the gateway sends.
4//! Some intents are "privileged" and require approval in the Discord Developer Portal.
5
6use bitflags::bitflags;
7use serde::{Deserialize, Deserializer, Serialize, Serializer};
8
9bitflags! {
10    /// Gateway Intents control which events Discord sends to your bot.
11    ///
12    /// See: https://discord.com/developers/docs/topics/gateway#gateway-intents
13    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
14    pub struct Intents: u64 {
15        /// Includes events for guild creation, update, delete, role changes, etc.
16        const GUILDS = 1 << 0;
17
18        /// Includes events for member joins, updates, removes.
19        /// **PRIVILEGED INTENT** - Requires verification for 100+ servers.
20        const GUILD_MEMBERS = 1 << 1;
21
22        /// Includes events for guild bans.
23        const GUILD_MODERATION = 1 << 2;
24
25        /// Includes events for emoji and sticker updates.
26        const GUILD_EMOJIS_AND_STICKERS = 1 << 3;
27
28        /// Includes events for integration updates.
29        const GUILD_INTEGRATIONS = 1 << 4;
30
31        /// Includes events for webhook updates.
32        const GUILD_WEBHOOKS = 1 << 5;
33
34        /// Includes events for invite creation/deletion.
35        const GUILD_INVITES = 1 << 6;
36
37        /// Includes events for voice state updates.
38        const GUILD_VOICE_STATES = 1 << 7;
39
40        /// Includes events for user presence updates.
41        /// **PRIVILEGED INTENT** - Requires verification for 100+ servers.
42        const GUILD_PRESENCES = 1 << 8;
43
44        /// Includes events for messages in guilds (not content without MESSAGE_CONTENT).
45        const GUILD_MESSAGES = 1 << 9;
46
47        /// Includes events for message reactions in guilds.
48        const GUILD_MESSAGE_REACTIONS = 1 << 10;
49
50        /// Includes events for typing indicators in guilds.
51        const GUILD_MESSAGE_TYPING = 1 << 11;
52
53        /// Includes events for direct messages.
54        const DIRECT_MESSAGES = 1 << 12;
55
56        /// Includes events for DM reactions.
57        const DIRECT_MESSAGE_REACTIONS = 1 << 13;
58
59        /// Includes events for DM typing indicators.
60        const DIRECT_MESSAGE_TYPING = 1 << 14;
61
62        /// Enables receiving message content in MESSAGE_CREATE events.
63        /// **PRIVILEGED INTENT** - Requires verification for 100+ servers.
64        const MESSAGE_CONTENT = 1 << 15;
65
66        /// Includes events for scheduled events.
67        const GUILD_SCHEDULED_EVENTS = 1 << 16;
68
69        /// Includes events for AutoMod configuration changes.
70        const AUTO_MODERATION_CONFIGURATION = 1 << 20;
71
72        /// Includes events for AutoMod action execution.
73        const AUTO_MODERATION_EXECUTION = 1 << 21;
74
75        /// Includes events for poll votes in guilds.
76        const GUILD_MESSAGE_POLLS = 1 << 24;
77
78        /// Includes events for poll votes in DMs.
79        const DIRECT_MESSAGE_POLLS = 1 << 25;
80
81        // ===== Convenience Combinations =====
82
83        /// All non-privileged intents.
84        const NON_PRIVILEGED = Self::GUILDS.bits()
85            | Self::GUILD_MODERATION.bits()
86            | Self::GUILD_EMOJIS_AND_STICKERS.bits()
87            | Self::GUILD_INTEGRATIONS.bits()
88            | Self::GUILD_WEBHOOKS.bits()
89            | Self::GUILD_INVITES.bits()
90            | Self::GUILD_VOICE_STATES.bits()
91            | Self::GUILD_MESSAGES.bits()
92            | Self::GUILD_MESSAGE_REACTIONS.bits()
93            | Self::GUILD_MESSAGE_TYPING.bits()
94            | Self::DIRECT_MESSAGES.bits()
95            | Self::DIRECT_MESSAGE_REACTIONS.bits()
96            | Self::DIRECT_MESSAGE_TYPING.bits()
97            | Self::GUILD_SCHEDULED_EVENTS.bits()
98            | Self::AUTO_MODERATION_CONFIGURATION.bits()
99            | Self::AUTO_MODERATION_EXECUTION.bits()
100            | Self::GUILD_MESSAGE_POLLS.bits()
101            | Self::DIRECT_MESSAGE_POLLS.bits();
102
103        /// All privileged intents (require approval).
104        const PRIVILEGED = Self::GUILD_MEMBERS.bits()
105            | Self::GUILD_PRESENCES.bits()
106            | Self::MESSAGE_CONTENT.bits();
107
108        /// All intents (use with caution, requires all privileged intents approved).
109        const ALL = Self::NON_PRIVILEGED.bits() | Self::PRIVILEGED.bits();
110    }
111}
112
113impl Default for Intents {
114    fn default() -> Self {
115        Self::NON_PRIVILEGED
116    }
117}
118
119impl Serialize for Intents {
120    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
121    where
122        S: Serializer,
123    {
124        serializer.serialize_u64(self.bits())
125    }
126}
127
128impl<'de> Deserialize<'de> for Intents {
129    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
130    where
131        D: Deserializer<'de>,
132    {
133        let bits = u64::deserialize(deserializer)?;
134        Ok(Intents::from_bits_truncate(bits))
135    }
136}
137
138#[cfg(test)]
139mod tests {
140    use super::*;
141
142    #[test]
143    fn test_intent_flags() {
144        let intents = Intents::GUILDS | Intents::GUILD_MESSAGES;
145        assert!(intents.contains(Intents::GUILDS));
146        assert!(intents.contains(Intents::GUILD_MESSAGES));
147        assert!(!intents.contains(Intents::GUILD_MEMBERS));
148    }
149
150    #[test]
151    fn test_intent_serialization() {
152        let intents = Intents::GUILDS | Intents::GUILD_MESSAGES;
153        let bits = intents.bits();
154        assert_eq!(bits, (1 << 0) | (1 << 9)); // 513
155    }
156}