titanium_model/
intents.rs1use bitflags::bitflags;
7use serde::{Deserialize, Deserializer, Serialize, Serializer};
8
9bitflags! {
10 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
14 pub struct Intents: u64 {
15 const GUILDS = 1 << 0;
17
18 const GUILD_MEMBERS = 1 << 1;
21
22 const GUILD_MODERATION = 1 << 2;
24
25 const GUILD_EMOJIS_AND_STICKERS = 1 << 3;
27
28 const GUILD_INTEGRATIONS = 1 << 4;
30
31 const GUILD_WEBHOOKS = 1 << 5;
33
34 const GUILD_INVITES = 1 << 6;
36
37 const GUILD_VOICE_STATES = 1 << 7;
39
40 const GUILD_PRESENCES = 1 << 8;
43
44 const GUILD_MESSAGES = 1 << 9;
46
47 const GUILD_MESSAGE_REACTIONS = 1 << 10;
49
50 const GUILD_MESSAGE_TYPING = 1 << 11;
52
53 const DIRECT_MESSAGES = 1 << 12;
55
56 const DIRECT_MESSAGE_REACTIONS = 1 << 13;
58
59 const DIRECT_MESSAGE_TYPING = 1 << 14;
61
62 const MESSAGE_CONTENT = 1 << 15;
65
66 const GUILD_SCHEDULED_EVENTS = 1 << 16;
68
69 const AUTO_MODERATION_CONFIGURATION = 1 << 20;
71
72 const AUTO_MODERATION_EXECUTION = 1 << 21;
74
75 const GUILD_MESSAGE_POLLS = 1 << 24;
77
78 const DIRECT_MESSAGE_POLLS = 1 << 25;
80
81 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 const PRIVILEGED = Self::GUILD_MEMBERS.bits()
105 | Self::GUILD_PRESENCES.bits()
106 | Self::MESSAGE_CONTENT.bits();
107
108 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)); }
156}