diff options
| author | Zeyla Hellyer <[email protected]> | 2017-03-25 15:41:47 -0700 |
|---|---|---|
| committer | Zeyla Hellyer <[email protected]> | 2017-03-25 15:41:47 -0700 |
| commit | 9114963daf708cfaeaf54d8c788206ccfbae5df8 (patch) | |
| tree | 08d6aba5b8ad40189b312865ac776bb6fa30c45d /src/model/guild/partial_guild.rs | |
| parent | Add slightly more documentation (diff) | |
| download | serenity-9114963daf708cfaeaf54d8c788206ccfbae5df8.tar.xz serenity-9114963daf708cfaeaf54d8c788206ccfbae5df8.zip | |
Rework the models directory
Diffstat (limited to 'src/model/guild/partial_guild.rs')
| -rw-r--r-- | src/model/guild/partial_guild.rs | 482 |
1 files changed, 482 insertions, 0 deletions
diff --git a/src/model/guild/partial_guild.rs b/src/model/guild/partial_guild.rs new file mode 100644 index 0000000..e5d463e --- /dev/null +++ b/src/model/guild/partial_guild.rs @@ -0,0 +1,482 @@ +use ::client::CACHE; +use ::model::*; +use ::utils::builder::{EditGuild, EditMember, EditRole, Search}; + +impl PartialGuild { + /// Ban a [`User`] from the guild. All messages by the + /// user within the last given number of days given will be deleted. This + /// may be a range between `0` and `7`. + /// + /// **Note**: Requires the [Ban Members] permission. + /// + /// # Examples + /// + /// Ban a member and remove all messages they've sent in the last 4 days: + /// + /// ```rust,ignore + /// // assumes a `user` and `guild` have already been bound + /// let _ = guild.ban(user, 4); + /// ``` + /// + /// # Errors + /// + /// Returns a [`ClientError::DeleteMessageDaysAmount`] if the number of + /// days' worth of messages to delete is over the maximum. + /// + /// [`ClientError::DeleteMessageDaysAmount`]: ../client/enum.ClientError.html#variant.DeleteMessageDaysAmount + /// [`User`]: struct.User.html + /// [Ban Members]: permissions/constant.BAN_MEMBERS.html + pub fn ban<U: Into<UserId>>(&self, user: U, delete_message_days: u8) -> Result<()> { + if delete_message_days > 7 { + return Err(Error::Client(ClientError::DeleteMessageDaysAmount(delete_message_days))); + } + + self.id.ban(user, delete_message_days) + } + + /// Creates a [`GuildChannel`] in the guild. + /// + /// Refer to [`rest::create_channel`] for more information. + /// + /// Requires the [Manage Channels] permission. + /// + /// # Examples + /// + /// Create a voice channel in a guild with the name `test`: + /// + /// ```rust,ignore + /// use serenity::model::ChannelType; + /// + /// guild.create_channel("test", ChannelType::Voice); + /// ``` + /// + /// [`GuildChannel`]: struct.GuildChannel.html + /// [`rest::create_channel`]: ../client/rest/fn.create_channel.html + /// [Manage Channels]: permissions/constant.MANAGE_CHANNELS.html + #[inline] + pub fn create_channel(&self, name: &str, kind: ChannelType) -> Result<GuildChannel> { + self.id.create_channel(name, kind) + } + + /// Creates an emoji in the guild with a name and base64-encoded image. + /// + /// Refer to the documentation for [`Guild::create_emoji`] for more + /// information. + /// + /// Requires the [Manage Emojis] permission. + /// + /// # Examples + /// + /// See the [`EditProfile::avatar`] example for an in-depth example as to + /// how to read an image from the filesystem and encode it as base64. Most + /// of the example can be applied similarly for this method. + /// + /// [`EditProfile::avatar`]: ../utils/builder/struct.EditProfile.html#method.avatar + /// [`Guild::create_emoji`]: struct.Guild.html#method.create_emoji + /// [`utils::read_image`]: ../utils/fn.read_image.html + /// [Manage Emojis]: permissions/constant.MANAGE_EMOJIS.html + #[inline] + pub fn create_emoji(&self, name: &str, image: &str) -> Result<Emoji> { + self.id.create_emoji(name, image) + } + + /// Creates an integration for the guild. + /// + /// Requires the [Manage Guild] permission. + /// + /// [Manage Guild]: permissions/constant.MANAGE_GUILD.html + #[inline] + pub fn create_integration<I>(&self, integration_id: I, kind: &str) -> Result<()> + where I: Into<IntegrationId> { + self.id.create_integration(integration_id, kind) + } + + /// Creates a new role in the guild with the data set, if any. + /// + /// See the documentation for [`Guild::create_role`] on how to use this. + /// + /// **Note**: Requires the [Manage Roles] permission. + /// + /// # Errors + /// + /// If the `cache` is enabled, returns a [`ClientError::InvalidPermissions`] + /// if the current user does not have permission to perform bans. + /// + /// [`ClientError::InvalidPermissions`]: ../client/enum.ClientError.html#variant.InvalidPermissions + /// [`Guild::create_role`]: struct.Guild.html#method.create_role + /// [Manage Roles]: permissions/constant.MANAGE_ROLES.html + #[inline] + pub fn create_role<F: FnOnce(EditRole) -> EditRole>(&self, f: F) -> Result<Role> { + self.id.create_role(f) + } + + /// Deletes the current guild if the current user is the owner of the + /// guild. + /// + /// **Note**: Requires the current user to be the owner of the guild. + #[inline] + pub fn delete(&self) -> Result<PartialGuild> { + self.id.delete() + } + + /// Deletes an [`Emoji`] from the guild. + /// + /// Requires the [Manage Emojis] permission. + /// + /// [`Emoji`]: struct.Emoji.html + /// [Manage Emojis]: permissions/constant.MANAGE_EMOJIS.html + #[inline] + pub fn delete_emoji<E: Into<EmojiId>>(&self, emoji_id: E) -> Result<()> { + self.id.delete_emoji(emoji_id) + } + + /// Deletes an integration by Id from the guild. + /// + /// Requires the [Manage Guild] permission. + /// + /// [Manage Guild]: permissions/constant.MANAGE_GUILD.html + #[inline] + pub fn delete_integration<I: Into<IntegrationId>>(&self, integration_id: I) -> Result<()> { + self.id.delete_integration(integration_id) + } + + /// Deletes a [`Role`] by Id from the guild. + /// + /// Also see [`Role::delete`] if you have the `cache` and `methods` features + /// enabled. + /// + /// Requires the [Manage Roles] permission. + /// + /// [`Role`]: struct.Role.html + /// [`Role::delete`]: struct.Role.html#method.delete + /// [Manage Roles]: permissions/constant.MANAGE_ROLES.html + #[inline] + pub fn delete_role<R: Into<RoleId>>(&self, role_id: R) -> Result<()> { + self.id.delete_role(role_id) + } + + /// Edits the current guild with new data where specified. + /// + /// **Note**: Requires the current user to have the [Manage Guild] + /// permission. + /// + /// [`Context::edit_guild`]: ../client/struct.Context.html#method.edit_guild + /// [Manage Guild]: permissions/constant.MANAGE_GUILD.html + pub fn edit<F>(&mut self, f: F) -> Result<()> + where F: FnOnce(EditGuild) -> EditGuild { + match self.id.edit(f) { + Ok(guild) => { + self.afk_channel_id = guild.afk_channel_id; + self.afk_timeout = guild.afk_timeout; + self.default_message_notifications = guild.default_message_notifications; + self.emojis = guild.emojis; + self.features = guild.features; + self.icon = guild.icon; + self.mfa_level = guild.mfa_level; + self.name = guild.name; + self.owner_id = guild.owner_id; + self.region = guild.region; + self.roles = guild.roles; + self.splash = guild.splash; + self.verification_level = guild.verification_level; + + Ok(()) + }, + Err(why) => Err(why), + } + } + + /// Edits an [`Emoji`]'s name in the guild. + /// + /// Also see [`Emoji::edit`] if you have the `cache` and `methods` features + /// enabled. + /// + /// Requires the [Manage Emojis] permission. + /// + /// [`Emoji`]: struct.Emoji.html + /// [`Emoji::edit`]: struct.Emoji.html#method.edit + /// [Manage Emojis]: permissions/constant.MANAGE_EMOJIS.html + #[inline] + pub fn edit_emoji<E: Into<EmojiId>>(&self, emoji_id: E, name: &str) -> Result<Emoji> { + self.id.edit_emoji(emoji_id, name) + } + + /// Edits the properties of member of the guild, such as muting or + /// nicknaming them. + /// + /// Refer to `EditMember`'s documentation for a full list of methods and + /// permission restrictions. + /// + /// # Examples + /// + /// Mute a member and set their roles to just one role with a predefined Id: + /// + /// ```rust,ignore + /// use serenity::model::GuildId; + /// + /// GuildId(7).edit_member(user_id, |m| m.mute(true).roles(&vec![role_id])); + /// ``` + #[inline] + pub fn edit_member<F, U>(&self, user_id: U, f: F) -> Result<()> + where F: FnOnce(EditMember) -> EditMember, U: Into<UserId> { + self.id.edit_member(user_id, f) + } + + /// Edits the current user's nickname for the guild. + /// + /// Pass `None` to reset the nickname. + /// + /// **Note**: Requires the [Change Nickname] permission. + /// + /// # Errors + /// + /// If the `cache` is enabled, returns a [`ClientError::InvalidPermissions`] + /// if the current user does not have permission to change their own + /// nickname. + /// + /// [`ClientError::InvalidPermissions`]: ../client/enum.ClientError.html#variant.InvalidPermissions + /// [Change Nickname]: permissions/constant.CHANGE_NICKNAME.html + #[inline] + pub fn edit_nickname(&self, new_nickname: Option<&str>) -> Result<()> { + self.id.edit_nickname(new_nickname) + } + + /// Gets a partial amount of guild data by its Id. + /// + /// Requires that the current user be in the guild. + #[inline] + pub fn get<G: Into<GuildId>>(guild_id: G) -> Result<PartialGuild> { + guild_id.into().get() + } + + /// Gets a list of the guild's bans. + /// + /// Requires the [Ban Members] permission. + /// + /// [Ban Members]: permissions/constant.BAN_MEMBERS.html + #[inline] + pub fn get_bans(&self) -> Result<Vec<Ban>> { + self.id.get_bans() + } + + /// Gets all of the guild's channels over the REST API. + /// + /// [`Guild`]: struct.Guild.html + #[inline] + pub fn get_channels(&self) -> Result<HashMap<ChannelId, GuildChannel>> { + self.id.get_channels() + } + + /// Gets an emoji in the guild by Id. + /// + /// Requires the [Manage Emojis] permission. + /// + /// [Manage Emojis]: permissions/constant.MANAGE_EMOJIS.html + #[inline] + pub fn get_emoji<E: Into<EmojiId>>(&self, emoji_id: E) -> Result<Emoji> { + self.id.get_emoji(emoji_id) + } + + /// Gets a list of all of the guild's emojis. + /// + /// Requires the [Manage Emojis] permission. + /// + /// [Manage Emojis]: permissions/constant.MANAGE_EMOJIS.html + #[inline] + pub fn get_emojis(&self) -> Result<Vec<Emoji>> { + self.id.get_emojis() + } + + /// Gets all integration of the guild. + /// + /// This performs a request over the REST API. + #[inline] + pub fn get_integrations(&self) -> Result<Vec<Integration>> { + self.id.get_integrations() + } + + /// Gets all of the guild's invites. + /// + /// Requires the [Manage Guild] permission. + /// + /// [Manage Guild]: permissions/constant.MANAGE_GUILD.html + #[inline] + pub fn get_invites(&self) -> Result<Vec<RichInvite>> { + self.id.get_invites() + } + + /// Gets a user's [`Member`] for the guild by Id. + /// + /// [`Guild`]: struct.Guild.html + /// [`Member`]: struct.Member.html + pub fn get_member<U: Into<UserId>>(&self, user_id: U) -> Result<Member> { + self.id.get_member(user_id) + } + + /// Gets a list of the guild's members. + /// + /// Optionally pass in the `limit` to limit the number of results. Maximum + /// value is 1000. Optionally pass in `after` to offset the results by a + /// [`User`]'s Id. + /// + /// [`User`]: struct.User.html + pub fn get_members<U>(&self, limit: Option<u64>, after: Option<U>) + -> Result<Vec<Member>> where U: Into<UserId> { + self.id.get_members(limit, after) + } + + /// Gets the number of [`Member`]s that would be pruned with the given + /// number of days. + /// + /// Requires the [Kick Members] permission. + /// + /// [`Member`]: struct.Member.html + /// [Kick Members]: permissions/constant.KICK_MEMBERS.html + #[inline] + pub fn get_prune_count(&self, days: u16) -> Result<GuildPrune> { + self.id.get_prune_count(days) + } + + /// Retrieves the guild's webhooks. + /// + /// **Note**: Requires the [Manage Webhooks] permission. + /// + /// [Manage Webhooks]: permissions/constant.MANAGE_WEBHOOKS.html + #[inline] + pub fn get_webhooks(&self) -> Result<Vec<Webhook>> { + self.id.get_webhooks() + } + + /// Kicks a [`Member`] from the guild. + /// + /// Requires the [Kick Members] permission. + /// + /// [`Member`]: struct.Member.html + /// [Kick Members]: permissions/constant.KICK_MEMBERS.html + #[inline] + pub fn kick<U: Into<UserId>>(&self, user_id: U) -> Result<()> { + self.id.kick(user_id) + } + + /// Returns a formatted URL of the guild's icon, if the guild has an icon. + pub fn icon_url(&self) -> Option<String> { + self.icon.as_ref().map(|icon| + format!(cdn!("/icons/{}/{}.webp"), self.id, icon)) + } + + /// Leaves the guild. + #[inline] + pub fn leave(&self) -> Result<PartialGuild> { + self.id.leave() + } + + /// Moves a member to a specific voice channel. + /// + /// Requires the [Move Members] permission. + /// + /// [Move Members]: permissions/constant.MOVE_MEMBERS.html + #[inline] + pub fn move_member<C, U>(&self, user_id: U, channel_id: C) -> Result<()> + where C: Into<ChannelId>, U: Into<UserId> { + self.id.move_member(user_id, channel_id) + } + + /// Performs a search request to the API for the guild's [`Message`]s. + /// + /// This will search all of the guild's [`Channel`]s at once, that you have + /// the [Read Message History] permission to. Use [`search_channels`] to + /// specify a list of [channel][`GuildChannel`]s to search, where all other + /// channels will be excluded. + /// + /// Refer to the documentation for the [`Search`] builder for examples and + /// more information. + /// + /// **Note**: Bot users can not search. + /// + /// # Errors + /// + /// If the `cache` is enabled, returns a + /// [`ClientError::InvalidOperationAsBot`] if the current user is a bot. + /// + /// [`ClientError::InvalidOperationAsBot`]: ../client/enum.ClientError.html#variant.InvalidOperationAsBot + /// [`Channel`]: enum.Channel.html + /// [`GuildChannel`]: struct.GuildChannel.html + /// [`Message`]: struct.Message.html + /// [`Search`]: ../utils/builder/struct.Search.html + /// [`search_channels`]: #method.search_channels + /// [Read Message History]: permissions/constant.READ_MESSAGE_HISTORY.html + pub fn search<F: FnOnce(Search) -> Search>(&self, f: F) -> Result<SearchResult> { + #[cfg(feature="cache")] + { + if CACHE.read().unwrap().user.bot { + return Err(Error::Client(ClientError::InvalidOperationAsBot)); + } + } + + self.id.search(f) + } + + /// Performs a search request to the API for the guild's [`Message`]s in + /// given channels. + /// + /// This will search all of the messages in the guild's provided + /// [`Channel`]s by Id that you have the [Read Message History] permission + /// to. Use [`search`] to search all of a guild's [channel][`GuildChannel`]s + /// at once. + /// + /// Refer to the documentation for the [`Search`] builder for examples and + /// more information. + /// + /// **Note**: Bot users can not search. + /// + /// # Errors + /// + /// If the `cache` is enabled, returns a + /// [`ClientError::InvalidOperationAsBot`] if the current user is a bot. + /// + /// [`ClientError::InvalidOperationAsBot`]: ../client/enum.ClientError.html#variant.InvalidOperationAsBot + /// [`Channel`]: enum.Channel.html + /// [`GuildChannel`]: struct.GuildChannel.html + /// [`Message`]: struct.Message.html + /// [`Search`]: ../utils/builder/struct.Search.html + /// [`search`]: #method.search + /// [Read Message History]: permissions/constant.READ_MESSAGE_HISTORY.html + pub fn search_channels<F>(&self, channel_ids: &[ChannelId], f: F) + -> Result<SearchResult> where F: FnOnce(Search) -> Search { + #[cfg(feature="cache")] + { + if CACHE.read().unwrap().user.bot { + return Err(Error::Client(ClientError::InvalidOperationAsBot)); + } + } + + self.id.search_channels(channel_ids, f) + } + + /// Returns the formatted URL of the guild's splash image, if one exists. + pub fn splash_url(&self) -> Option<String> { + self.icon.as_ref().map(|icon| + format!(cdn!("/splashes/{}/{}.webp"), self.id, icon)) + } + + /// Starts an integration sync for the given integration Id. + /// + /// Requires the [Manage Guild] permission. + /// + /// [Manage Guild]: permissions/constant.MANAGE_GUILD.html + #[inline] + pub fn start_integration_sync<I: Into<IntegrationId>>(&self, integration_id: I) -> Result<()> { + self.id.start_integration_sync(integration_id) + } + + /// Unbans a [`User`] from the guild. + /// + /// Requires the [Ban Members] permission. + /// + /// [`User`]: struct.User.html + /// [Ban Members]: permissions/constant.BAN_MEMBERS.html + #[inline] + pub fn unban<U: Into<UserId>>(&self, user_id: U) -> Result<()> { + self.id.unban(user_id) + } +} |