zynko 0.1.0

Biblioteca oficial para bots do Zynko: API, gateway e voz.
Documentation
//! Intents, permission bits and id times.

/// Intents: the kinds of event a bot asks the gateway for (docs.zynko.org/api/gateway#intents).
/// READY, GUILD_CREATE, GUILD_DELETE, CHANNEL_CREATE, USER_UPDATE and INTERACTION_CREATE always
/// arrive.
///
/// ```
/// use zynko::Intents;
/// let intents = Intents::GUILDS | Intents::MESSAGES;
/// assert_eq!(intents, 9);
/// ```
pub struct Intents;

impl Intents {
    /// Servers changing, and their channels, roles and emojis.
    pub const GUILDS: u64 = 1 << 0;
    /// Members joining, leaving or changing.
    pub const GUILD_MEMBERS: u64 = 1 << 1;
    /// Who is online.
    pub const PRESENCES: u64 = 1 << 2;
    /// Messages created, edited and deleted, in servers and DMs.
    pub const MESSAGES: u64 = 1 << 3;
    pub const REACTIONS: u64 = 1 << 4;
    pub const TYPING: u64 = 1 << 5;
    /// Who is in which voice room. A bot that joins voice needs it.
    pub const VOICE_STATES: u64 = 1 << 6;
    pub const ALL: u64 = (1 << 7) - 1;
}

/// Role permissions, as bits (docs.zynko.org/api/permissoes).
pub struct Permissions;

impl Permissions {
    pub const ADMINISTRATOR: u64 = 1 << 0;
    pub const MANAGE_GUILD: u64 = 1 << 1;
    pub const MANAGE_CHANNELS: u64 = 1 << 2;
    pub const MANAGE_ROLES: u64 = 1 << 3;
    pub const KICK_MEMBERS: u64 = 1 << 4;
    pub const BAN_MEMBERS: u64 = 1 << 5;
    pub const MANAGE_MESSAGES: u64 = 1 << 6;
    pub const MANAGE_NICKNAMES: u64 = 1 << 7;
    pub const CREATE_INVITE: u64 = 1 << 8;
    pub const SEND_MESSAGES: u64 = 1 << 9;
    pub const ATTACH_FILES: u64 = 1 << 10;
    pub const CONNECT: u64 = 1 << 11;
    pub const SPEAK: u64 = 1 << 12;
    pub const MANAGE_EMOJIS: u64 = 1 << 13;
    pub const VIEW_CHANNEL: u64 = 1 << 14;
    pub const ADD_REACTIONS: u64 = 1 << 15;
    pub const READ_MESSAGE_HISTORY: u64 = 1 << 16;
    pub const STREAM: u64 = 1 << 17;
    /// Every bit above.
    pub const ALL: u64 = (1 << 18) - 1;
}

const EPOCH_MS: u64 = 1_767_225_600_000;

/// When an id was made, in milliseconds since 1970 (ids are snowflakes). `None` if the id is
/// not a number.
pub fn id_time(id: &str) -> Option<u64> {
    id.parse::<u64>().ok().map(|n| (n >> 22) + EPOCH_MS)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn intents_match_the_docs() {
        assert_eq!(Intents::GUILDS | Intents::MESSAGES, 9);
        assert_eq!(Intents::ALL, 127);
    }

    #[test]
    fn id_time_decodes_snowflakes() {
        assert_eq!(id_time("0"), Some(EPOCH_MS));
        assert_eq!(id_time(&(1000u64 << 22).to_string()), Some(EPOCH_MS + 1000));
        assert_eq!(id_time("abc"), None);
    }
}