aboutsummaryrefslogtreecommitdiff
path: root/src/model/channel/channel_category.rs
diff options
context:
space:
mode:
authoracdenisSK <[email protected]>2017-09-09 10:53:44 +0200
committeracdenisSK <[email protected]>2017-09-09 10:56:29 +0200
commit4be6b9d5008ff8bb3d1fdddff5647a6bb307513c (patch)
tree516b811875ae4cb2b98c308aabb69bc6e7a94fd2 /src/model/channel/channel_category.rs
parentChange order to avoid subtraction overflow error (#160) (diff)
downloadserenity-4be6b9d5008ff8bb3d1fdddff5647a6bb307513c.tar.xz
serenity-4be6b9d5008ff8bb3d1fdddff5647a6bb307513c.zip
Implement categories
Diffstat (limited to 'src/model/channel/channel_category.rs')
-rw-r--r--src/model/channel/channel_category.rs121
1 files changed, 121 insertions, 0 deletions
diff --git a/src/model/channel/channel_category.rs b/src/model/channel/channel_category.rs
new file mode 100644
index 0000000..f8374fc
--- /dev/null
+++ b/src/model/channel/channel_category.rs
@@ -0,0 +1,121 @@
+use model::*;
+use builder::EditChannel;
+use http;
+use utils as serenity_utils;
+
+/// A category of [`GuildChannel`]s.
+///
+/// [`GuildChannel`]: struct.GuildChannel.html
+#[derive(Clone, Debug, Deserialize)]
+pub struct ChannelCategory {
+ /// Id of this category.
+ pub id: ChannelId,
+ /// If this category belongs to another category.
+ pub parent_id: Option<ChannelId>,
+ /// The position of this category.
+ pub position: i64,
+ /// Indicator of the type of channel this is.
+ ///
+ /// This should always be [`ChannelType::Category`].
+ ///
+ /// [`ChannelType::Category`]: enum.ChannelType.html#variant.Category
+ pub kind: ChannelType,
+ /// The name of the category.
+ pub name: String,
+ /// Whether this category is nsfw. (This'll be inherited by all channels in this category)
+ #[serde(default)]
+ pub nsfw: bool,
+ /// Permission overwrites for the [`GuildChannel`]s.
+ ///
+ /// [`GuildChannel`]: struct.GuildChannel.html
+ pub permission_overwrites: Vec<PermissionOverwrite>,
+}
+
+impl ChannelCategory {
+ /// Adds a permission overwrite to the category's channels.
+ #[inline]
+ pub fn create_permission(&self, target: &PermissionOverwrite) -> Result<()> {
+ self.id.create_permission(target)
+ }
+
+ /// Deletes all permission overrides in the category from the channels.
+ ///
+ /// **Note**: Requires the [Manage Channel] permission.
+ ///
+ /// [Manage Channel]: permissions/constant.MANAGE_CHANNELS.html
+ #[inline]
+ pub fn delete_permission(&self, permission_type: PermissionOverwriteType) -> Result<()> {
+ self.id.delete_permission(permission_type)
+ }
+
+ /// Deletes this category.
+ #[inline]
+ pub fn delete(&self) -> Result<()> {
+ #[cfg(feature = "cache")]
+ {
+ let req = permissions::MANAGE_CHANNELS;
+
+ if !utils::user_has_perms(self.id, req)? {
+ return Err(Error::Model(ModelError::InvalidPermissions(req)));
+ }
+ }
+
+ self.id.delete().map(|_| ())
+ }
+
+ /// Modifies the category's settings, such as its position or name.
+ ///
+ /// Refer to `EditChannel`s documentation for a full list of methods.
+ ///
+ /// # Examples
+ ///
+ /// Change a voice channels name and bitrate:
+ ///
+ /// ```rust,ignore
+ /// category.edit(|c| c.name("test").bitrate(86400));
+ /// ```
+ #[cfg(feature = "model")]
+ pub fn edit<F>(&mut self, f: F) -> Result<()>
+ where F: FnOnce(EditChannel) -> EditChannel {
+ #[cfg(feature = "cache")]
+ {
+ let req = permissions::MANAGE_CHANNELS;
+
+ if !utils::user_has_perms(self.id, req)? {
+ return Err(Error::Model(ModelError::InvalidPermissions(req)));
+ }
+ }
+
+ let mut map = Map::new();
+ map.insert(
+ "name".to_owned(),
+ Value::String(self.name.clone()),
+ );
+ map.insert(
+ "position".to_owned(),
+ Value::Number(Number::from(self.position)),
+ );
+ map.insert(
+ "type".to_owned(),
+ Value::String(self.kind.name().to_owned()),
+ );
+
+ let edited = f(EditChannel(map)).0;
+
+ http::edit_channel(self.id.0, &edited).map(|channel| {
+ let GuildChannel { id, parent_id, permission_overwrites, nsfw, name, position, kind, .. } = channel;
+
+ *self = ChannelCategory { id, parent_id, permission_overwrites, nsfw, name, position, kind };
+ ()
+ })
+ }
+
+ #[cfg(feature = "utils")]
+ #[inline]
+ pub fn is_nsfw(&self) -> bool {
+ self.kind == ChannelType::Text && (self.nsfw || serenity_utils::is_nsfw(&self.name))
+ }
+
+ /// Returns the name of the category.
+ pub fn name(&self) -> &str { &self.name }
+} \ No newline at end of file