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
use model::prelude::*;
#[cfg(all(feature = "builder", feature = "model"))]
use builder::EditChannel;
#[cfg(all(feature = "builder", feature = "model"))]
use http;
#[cfg(all(feature = "model", feature = "utils"))]
use utils::{self as serenity_utils, VecMap};
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ChannelCategory {
pub id: ChannelId,
#[serde(rename = "parent_id")]
pub category_id: Option<ChannelId>,
pub position: i64,
#[serde(rename = "type")]
pub kind: ChannelType,
pub name: String,
#[serde(default)]
pub nsfw: bool,
pub permission_overwrites: Vec<PermissionOverwrite>,
}
#[cfg(feature = "model")]
impl ChannelCategory {
#[inline]
pub fn create_permission(&self, target: &PermissionOverwrite) -> Result<()> {
self.id.create_permission(target)
}
#[inline]
pub fn delete_permission(&self, permission_type: PermissionOverwriteType) -> Result<()> {
self.id.delete_permission(permission_type)
}
#[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(|_| ())
}
#[cfg(all(feature = "builder", feature = "model", feature = "utils"))]
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 = VecMap::new();
map.insert("name", Value::String(self.name.clone()));
map.insert("position", Value::Number(Number::from(self.position)));
map.insert("type", Value::String(self.kind.name().to_string()));
let map = serenity_utils::vecmap_to_json_map(f(EditChannel(map)).0);
http::edit_channel(self.id.0, &map).map(|channel| {
let GuildChannel {
id,
category_id,
permission_overwrites,
nsfw,
name,
position,
kind,
..
} = channel;
*self = ChannelCategory {
id,
category_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))
}
pub fn name(&self) -> &str { &self.name }
}