1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//! Mappings of objects received from the API, with optional helper methods for
//! ease of use.
//!
//! Models can optionally have additional helper methods compiled, by enabling
//! the `methods` feature.
//!
//! Methods like [`Message::delete`] or [`Webhook::execute`] are provided with
//! this feature, which can be shorthands for operations that are otherwise in
//! the [`Context`], or the much lower-level [`rest`] module.
//!
//! [`Context`]: ../client/struct.Context.html
//! [`Message::delete`]: struct.Message.html#method.delete
//! [`Webhook::execute`]: struct.Webhook.html#method.execute
//! [`rest`]: ../client/rest/index.html

#[macro_use]
mod utils;

pub mod event;
pub mod permissions;


mod channel;
mod gateway;
mod guild;
mod invite;
mod misc;
mod user;
mod webhook;

pub use self::channel::*;
pub use self::gateway::*;
pub use self::guild::*;
pub use self::invite::*;
pub use self::misc::*;
pub use self::permissions::Permissions;
pub use self::user::*;
pub use self::webhook::*;

use self::utils::*;
use std::collections::HashMap;
use time::Timespec;
use ::internal::prelude::*;
use ::utils::{Colour, decode_array};

// All of the enums and structs are imported here. These are built from the
// build script located at `./build.rs`.
//
// These use definitions located in `./definitions`, to map to structs and
// enums, each respectively located in their own folder.
//
// For structs, this will almost always include their decode method, although
// some require their own decoding due to many special fields.
//
// For enums, this will include the variants, and will automatically generate
// the number/string decoding methods where appropriate.
//
// As only the struct/enum itself and common mappings can be built, this leaves
// unique methods on each to be implemented here.
include!(concat!(env!("OUT_DIR"), "/models/built.rs"));

macro_rules! id {
    ($(#[$attr:meta] $name:ident;)*) => {
        $(
            #[$attr]
            #[derive(Copy, Clone, Debug, Eq, Hash, PartialOrd, Ord)]
            #[allow(derive_hash_xor_eq)]
            pub struct $name(pub u64);

            impl $name {
                fn decode(value: Value) -> Result<Self> {
                    decode_id(value).map($name)
                }

                /// Retrieves the time that the Id was created at.
                pub fn created_at(&self) -> Timespec {
                    let offset = (self.0 >> 22) / 1000;
                    Timespec::new(1420070400 + offset as i64, 0)
                }
            }

            impl From<u64> for $name {
                fn from(id_as_u64: u64) -> $name {
                    $name(id_as_u64)
                }
            }

            impl PartialEq for $name {
                fn eq(&self, other: &Self) -> bool {
                    self.0 == other.0
                }
            }

            impl PartialEq<u64> for $name {
                fn eq(&self, u: &u64) -> bool {
                    self.0 == *u
                }
            }
        )*
    }
}

id! {
    /// An identifier for a Channel
    ChannelId;
    /// An identifier for an Emoji
    EmojiId;
    /// An identifier for a Guild
    GuildId;
    /// An identifier for an Integration
    IntegrationId;
    /// An identifier for a Message
    MessageId;
    /// An identifier for a Role
    RoleId;
    /// An identifier for a User
    UserId;
    /// An identifier for a [`Webhook`](struct.Webhook.html).
    WebhookId;
}

/// A container for any channel.
#[derive(Clone, Debug)]
pub enum Channel {
    /// A group. A group comprises of only one channel.
    Group(Group),
    /// A [text] or [voice] channel within a [`Guild`].
    ///
    /// [`Guild`]: struct.Guild.html
    /// [text]: enum.ChannelType.html#variant.Text
    /// [voice]: enum.ChannelType.html#variant.Voice
    Guild(GuildChannel),
    /// A private channel to another [`User`]. No other users may access the
    /// channel. For multi-user "private channels", use a group.
    ///
    /// [`User`]: struct.User.html
    Private(PrivateChannel),
}

/// A container for guilds.
///
/// This is used to differentiate whether a guild itself can be used or whether
/// a guild needs to be retrieved from the cache.
pub enum GuildContainer {
    /// A guild which can have its contents directly searched.
    Guild(PartialGuild),
    /// A guild's id, which can be used to search the cache for a guild.
    Id(GuildId),
}

/// The type of edit being made to a Channel's permissions.
///
/// This is for use with methods such as `Context::create_permission`.
///
/// [`Context::create_permission`]: ../client/
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum PermissionOverwriteType {
    /// A member which is having its permission overwrites edited.
    Member(UserId),
    /// A role which is having its permission overwrites edited.
    Role(RoleId),
}

/// A guild which may or may not currently be available.
#[derive(Clone, Debug)]
pub enum PossibleGuild<T> {
    /// An indicator that a guild is currently unavailable for at least one of
    /// a variety of reasons.
    Offline(GuildId),
    /// An indicator that a guild is currently available.
    Online(T),
}

#[derive(Copy, Clone, Debug)]
pub enum SearchTarget {
    Channel(ChannelId),
    Guild(GuildId),
}

impl From<ChannelId> for SearchTarget {
    fn from(channel_id: ChannelId) -> SearchTarget {
        SearchTarget::Channel(channel_id)
    }
}

impl From<GuildId> for SearchTarget {
    fn from(guild_id: GuildId) -> SearchTarget {
        SearchTarget::Guild(guild_id)
    }
}