use serde::{Deserialize, Serialize};
macro_rules! intent_accessors {
($($with:ident, $check:ident, $intent:ident, $doc:literal);+ $(;)?) => {
$(
#[doc = concat!("Enable ", $doc, " intent.")]
pub const fn $with(self) -> Self {
self.with_intent(Self::$intent)
}
#[doc = concat!("Check if ", $doc, " intent is enabled.")]
pub const fn $check(self) -> bool {
self.contains(Self::$intent)
}
)+
};
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Intents {
pub bits: u32,
}
impl Intents {
pub const fn new() -> Self {
Self { bits: 0 }
}
pub const fn none() -> Self {
Self::new()
}
pub const fn all() -> Self {
Self {
bits: Self::GUILDS
| Self::GUILD_MEMBERS
| Self::GUILD_MESSAGES
| Self::GUILD_MESSAGE_REACTIONS
| Self::DIRECT_MESSAGE
| Self::INTERACTION
| Self::MESSAGE_AUDIT
| Self::FORUMS
| Self::AUDIO_ACTION
| Self::PUBLIC_GUILD_MESSAGES
| Self::AUDIO_OR_LIVE_CHANNEL_MEMBER
| Self::OPEN_FORUM_EVENT
| Self::ENTER_AIO
| Self::PUBLIC_MESSAGES,
}
}
pub const fn default() -> Self {
Self::all()
.without_intent(Self::GUILD_MESSAGES)
.without_intent(Self::FORUMS)
}
pub const GUILDS: u32 = 1 << 0;
pub const GUILD_MEMBERS: u32 = 1 << 1;
pub const GUILD_MESSAGES: u32 = 1 << 9;
pub const GUILD_MESSAGE_REACTIONS: u32 = 1 << 10;
pub const DIRECT_MESSAGE: u32 = 1 << 12;
pub const INTERACTION: u32 = 1 << 26;
pub const MESSAGE_AUDIT: u32 = 1 << 27;
pub const FORUMS: u32 = 1 << 28;
pub const AUDIO_ACTION: u32 = 1 << 29;
pub const PUBLIC_GUILD_MESSAGES: u32 = 1 << 30;
pub const AUDIO_OR_LIVE_CHANNEL_MEMBER: u32 = 1 << 19;
pub const OPEN_FORUM_EVENT: u32 = 1 << 18;
pub const ENTER_AIO: u32 = 1 << 23;
pub const PUBLIC_MESSAGES: u32 = 1 << 25;
pub const fn contains(self, intent: u32) -> bool {
(self.bits & intent) == intent
}
pub const fn with_intent(mut self, intent: u32) -> Self {
self.bits |= intent;
self
}
pub const fn without_intent(mut self, intent: u32) -> Self {
self.bits &= !intent;
self
}
intent_accessors! {
with_guilds, guilds, GUILDS, "guilds";
with_guild_members, guild_members, GUILD_MEMBERS, "guild members";
with_guild_messages, guild_messages, GUILD_MESSAGES, "guild messages";
with_guild_message_reactions, guild_message_reactions, GUILD_MESSAGE_REACTIONS, "guild message reactions";
with_direct_message, direct_message, DIRECT_MESSAGE, "direct messages";
with_interaction, interaction, INTERACTION, "interaction";
with_message_audit, message_audit, MESSAGE_AUDIT, "message audit";
with_forums, forums, FORUMS, "forums";
with_audio_action, audio_action, AUDIO_ACTION, "audio action";
with_public_guild_messages, public_guild_messages, PUBLIC_GUILD_MESSAGES, "public guild messages";
with_audio_or_live_channel_member, audio_or_live_channel_member, AUDIO_OR_LIVE_CHANNEL_MEMBER, "audio or live channel member";
with_open_forum_event, open_forum_event, OPEN_FORUM_EVENT, "open forum event";
with_enter_aio, enter_aio, ENTER_AIO, "enter AIO";
with_public_messages, public_messages, PUBLIC_MESSAGES, "public messages";
}
pub const fn has_privileged(self) -> bool {
self.contains(Self::GUILD_MESSAGES) || self.contains(Self::FORUMS)
}
pub const fn bits(self) -> u32 {
self.bits
}
pub const fn from_bits(bits: u32) -> Self {
Self { bits }
}
}
impl Default for Intents {
fn default() -> Self {
Self::default()
}
}