use super::*; #[cfg(all(feature = "cache", feature = "methods"))] use ::client::CACHE; #[cfg(feature = "methods")] use ::client::rest; #[cfg(feature = "methods")] use ::internal::prelude::*; impl ChannelId { /// Search the cache for the channel with the Id. #[cfg(all(feature = "cache", feature = "methods"))] pub fn find(&self) -> Option { CACHE.read().unwrap().get_channel(*self).map(|x| x.clone_inner()) } /// Search the cache for the channel. If it can't be found, the channel is /// requested over REST. #[cfg(feature="methods")] pub fn get(&self) -> Result { feature_cache_enabled! {{ if let Some(channel) = CACHE.read().unwrap().get_channel(*self) { return Ok(channel.clone_inner()); } }} rest::get_channel(self.0) } /// Returns a [`Mention`] which will link to the [`Channel`]. /// /// [`Channel`]: enum.Channel.html /// [`Mention`]: struct.Mention.html pub fn mention(&self) -> Mention { Mention { id: self.0, prefix: "<#", } } /// Retrieves the channel's webhooks. /// /// **Note**: Requires the [Manage Webhooks] permission. /// /// [Manage Webhooks]: permissions/constant.MANAGE_WEBHOOKS.html #[cfg(feature="methods")] pub fn webhooks(&self) -> Result> { rest::get_channel_webhooks(self.0) } } impl From for ChannelId { /// Gets the Id of a `Channel`. fn from(channel: Channel) -> ChannelId { match channel { Channel::Group(group) => group.channel_id, Channel::Guild(channel) => channel.id, Channel::Private(channel) => channel.id, } } } impl From for ChannelId { /// Gets the Id of a private channel. fn from(private_channel: PrivateChannel) -> ChannelId { private_channel.id } } impl From for ChannelId { /// Gets the Id of a guild channel. fn from(public_channel: GuildChannel) -> ChannelId { public_channel.id } } impl From for EmojiId { /// Gets the Id of an `Emoji`. fn from(emoji: Emoji) -> EmojiId { emoji.id } } impl GuildId { /// Search the cache for the guild. #[cfg(all(feature = "cache", feature="methods"))] pub fn find(&self) -> Option { CACHE.read().unwrap().get_guild(*self).cloned() } /// Requests the guild over REST. /// /// Note that this will not be a complete guild, as REST does not send /// all data with a guild retrieval. #[cfg(feature="methods")] pub fn get(&self) -> Result { rest::get_guild(self.0) } /// Mentions the [`Guild`]'s default channel. /// /// [`Guild`]: struct.Guild.html pub fn mention(&self) -> Mention { Mention { id: self.0, prefix: "<#", } } /// Returns this Id as a `ChannelId`, which is useful when needing to use /// the guild Id to send a message to the default channel. #[cfg(feature = "methods")] pub fn to_channel(&self) -> ChannelId { ChannelId(self.0) } /// Retrieves the guild's webhooks. /// /// **Note**: Requires the [Manage Webhooks] permission. /// /// [Manage Webhooks]: permissions/constant.MANAGE_WEBHOOKS.html #[cfg(feature="methods")] pub fn webhooks(&self) -> Result> { rest::get_guild_webhooks(self.0) } } impl From for GuildId { /// Gets the Id of a partial guild. fn from(guild: PartialGuild) -> GuildId { guild.id } } impl From for GuildId { /// Gets the Id of Guild information struct. fn from(guild_info: GuildInfo) -> GuildId { guild_info.id } } impl From for GuildId { /// Gets the Id of Invite Guild struct. fn from(invite_guild: InviteGuild) -> GuildId { invite_guild.id } } impl From for GuildId { /// Gets the Id of Guild. fn from(live_guild: Guild) -> GuildId { live_guild.id } } impl From for IntegrationId { /// Gets the Id of integration. fn from(integration: Integration) -> IntegrationId { integration.id } } impl From for MessageId { /// Gets the Id of a `Message`. fn from(message: Message) -> MessageId { message.id } } impl From for RoleId { /// Gets the Id of a `Role`. fn from(role: Role) -> RoleId { role.id } } impl RoleId { /// Search the cache for the role. #[cfg(all(feature = "cache", feature = "methods"))] pub fn find(&self) -> Option { CACHE.read() .unwrap() .guilds .values() .find(|guild| guild.roles.contains_key(self)) .map(|guild| guild.roles.get(self)) .and_then(|v| match v { Some(v) => Some(v), None => None, }) .cloned() } /// Returns a [`Mention`] which will ping members of the role. /// /// [`Mention`]: struct.Mention.html pub fn mention(&self) -> Mention { Mention { id: self.0, prefix: "<@&", } } } impl From for UserId { /// Gets the Id of a `CurrentUser` struct. fn from(current_user: CurrentUser) -> UserId { current_user.id } } impl From for UserId { /// Gets the Id of a `Member`. fn from(member: Member) -> UserId { member.user.id } } impl From for UserId { /// Gets the Id of a `User`. fn from(user: User) -> UserId { user.id } } impl UserId { /// Returns a [`Mention`] which will ping the user. /// /// [`Mention`]: struct.Mention.html pub fn mention(&self) -> Mention { Mention { id: self.0, prefix: "<@", } } } impl WebhookId { /// Retrieves the webhook by the Id. /// /// **Note**: Requires the [Manage Webhooks] permission. /// /// [Manage Webhooks]: permissions/constant.MANAGE_WEBHOOKS.html #[cfg(feature="methods")] pub fn webhooks(&self) -> Result { rest::get_webhook(self.0) } }