diff options
Diffstat (limited to 'src/model/invite.rs')
| -rw-r--r-- | src/model/invite.rs | 59 |
1 files changed, 48 insertions, 11 deletions
diff --git a/src/model/invite.rs b/src/model/invite.rs index adfba60..47ae28a 100644 --- a/src/model/invite.rs +++ b/src/model/invite.rs @@ -1,12 +1,15 @@ use super::{Invite, RichInvite}; use ::client::rest; use ::internal::prelude::*; +use ::model::ChannelId; +use ::utils::builder::CreateInvite; +use ::utils; #[cfg(feature="cache")] use super::permissions; -#[cfg(all(feature="cache", feature="methods"))] -use super::utils; -#[cfg(feature = "cache")] +#[cfg(feature="cache")] +use super::utils as model_utils; +#[cfg(feature="cache")] use ::client::CACHE; impl Invite { @@ -26,11 +29,10 @@ impl Invite { /// [`ClientError::InvalidOperationAsBot`] if the current user does not have /// the required [permission]. /// - /// [`ClientError::InvalidOperationAsBot`]: enum.ClientError.html#variant.InvalidOperationAsBot + /// [`ClientError::InvalidOperationAsBot`]: ../client/enum.ClientError.html#variant.InvalidOperationAsBot /// [`Guild`]: struct.Guild.html /// [`rest::accept_invite`]: ../client/rest/fn.accept_invite.html /// [permission]: permissions/index.html - #[cfg(feature="methods")] pub fn accept(&self) -> Result<Invite> { #[cfg(feature="cache")] { @@ -42,6 +44,39 @@ impl Invite { rest::accept_invite(&self.code) } + /// Creates an invite for a [`GuildChannel`], providing a builder so that + /// fields may optionally be set. + /// + /// See the documentation for the [`CreateInvite`] builder for information + /// on how to use this and the default values that it provides. + /// + /// Requires the [Create Invite] permission. + /// + /// # Errors + /// + /// If the `cache` is enabled, returns a [`ClientError::InvalidPermissions`] + /// if the current user does not have the required [permission]. + /// + /// [`CreateInvite`]: ../utils/builder/struct.CreateInvite.html + /// [`GuildChannel`]: struct.GuildChannel.html + /// [Create Invite]: permissions/constant.CREATE_INVITE.html + /// [permission]: permissions/index.html + pub fn create<C, F>(channel_id: C, f: F) -> Result<RichInvite> + where C: Into<ChannelId>, F: FnOnce(CreateInvite) -> CreateInvite { + let channel_id = channel_id.into(); + + #[cfg(feature="cache")] + { + let req = permissions::CREATE_INVITE; + + if !model_utils::user_has_perms(channel_id, req)? { + return Err(Error::Client(ClientError::InvalidPermissions(req))); + } + } + + rest::create_invite(channel_id.0, f(CreateInvite::default()).0.build()) + } + /// Deletes the invite. /// /// **Note**: Requires the [Manage Guild] permission. @@ -54,19 +89,23 @@ impl Invite { /// [`ClientError::InvalidPermissions`]: ../client/enum.ClientError.html#variant.InvalidPermissions /// [Manage Guild]: permissions/constant.MANAGE_GUILD.html /// [permission]: permissions/index.html - #[cfg(feature="methods")] pub fn delete(&self) -> Result<Invite> { #[cfg(feature="cache")] { let req = permissions::MANAGE_GUILD; - if !utils::user_has_perms(self.channel.id, req)? { + if !model_utils::user_has_perms(self.channel.id, req)? { return Err(Error::Client(ClientError::InvalidPermissions(req))); } } rest::delete_invite(&self.code) } + + /// Gets the information about an invite. + pub fn get(code: &str) -> Result<Invite> { + rest::get_invite(utils::parse_invite(code)) + } } impl RichInvite { @@ -86,10 +125,9 @@ impl RichInvite { /// [`ClientError::InvalidOperationAsBot`] if the current user is a bot /// user. /// - /// [`ClientError::InvalidOperationAsBot`]: enum.ClientError.html#variant.InvalidOperationAsBot + /// [`ClientError::InvalidOperationAsBot`]: ../client/enum.ClientError.html#variant.InvalidOperationAsBot /// [`Guild`]: struct.Guild.html /// [`rest::accept_invite`]: ../client/rest/fn.accept_invite.html - #[cfg(feature="methods")] pub fn accept(&self) -> Result<Invite> { #[cfg(feature="cache")] { @@ -118,13 +156,12 @@ impl RichInvite { /// [`rest::delete_invite`]: ../client/rest/fn.delete_invite.html /// [Manage Guild]: permissions/constant.MANAGE_GUILD.html /// [permission]: permissions/index.html - #[cfg(feature="methods")] pub fn delete(&self) -> Result<Invite> { #[cfg(feature="cache")] { let req = permissions::MANAGE_GUILD; - if !utils::user_has_perms(self.channel.id, req)? { + if !model_utils::user_has_perms(self.channel.id, req)? { return Err(Error::Client(ClientError::InvalidPermissions(req))); } } |