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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use std::collections::HashMap;
use bytes::buf::Buf;
use reqwest::Url;
use tokio::{fs::File, io::AsyncReadExt};
use crate::http::{AttachmentType, Http};
use crate::internal::prelude::*;
use crate::model::{guild::Role, Permissions};
#[derive(Clone, Debug, Default)]
pub struct EditRole(pub HashMap<&'static str, Value>);
impl EditRole {
pub fn new(role: &Role) -> Self {
let mut map = HashMap::with_capacity(9);
#[cfg(feature = "utils")]
{
map.insert("color", Value::Number(Number::from(role.colour.0)));
}
#[cfg(not(feature = "utils"))]
{
map.insert("color", Value::Number(Number::from(role.colour)));
}
map.insert("hoist", Value::Bool(role.hoist));
map.insert("managed", Value::Bool(role.managed));
map.insert("mentionable", Value::Bool(role.mentionable));
map.insert("name", Value::String(role.name.clone()));
map.insert("permissions", Value::Number(Number::from(role.permissions.bits())));
map.insert("position", Value::Number(Number::from(role.position)));
if let Some(unicode_emoji) = &role.unicode_emoji {
map.insert("unicode_emoji", Value::String(unicode_emoji.clone()));
}
if let Some(icon) = &role.icon {
map.insert("icon", Value::String(icon.clone()));
}
EditRole(map)
}
pub fn colour(&mut self, colour: u64) -> &mut Self {
self.0.insert("color", Value::Number(Number::from(colour)));
self
}
pub fn hoist(&mut self, hoist: bool) -> &mut Self {
self.0.insert("hoist", Value::Bool(hoist));
self
}
pub fn mentionable(&mut self, mentionable: bool) -> &mut Self {
self.0.insert("mentionable", Value::Bool(mentionable));
self
}
pub fn name<S: ToString>(&mut self, name: S) -> &mut Self {
self.0.insert("name", Value::String(name.to_string()));
self
}
pub fn permissions(&mut self, permissions: Permissions) -> &mut Self {
self.0.insert("permissions", Value::Number(Number::from(permissions.bits())));
self
}
pub fn position(&mut self, position: u8) -> &mut Self {
self.0.insert("position", Value::Number(Number::from(position)));
self
}
pub fn unicode_emoji<S: ToString>(&mut self, unicode_emoji: S) -> &mut Self {
self.0.remove("icon");
self.0.insert("unicode_emoji", Value::String(unicode_emoji.to_string()));
self
}
pub async fn icon<'a>(
&mut self,
http: impl AsRef<Http>,
icon: impl Into<AttachmentType<'a>>,
) -> Result<&mut Self> {
let icon = match icon.into() {
AttachmentType::Bytes {
data,
filename: _,
} => "data:image/png;base64,".to_string() + &base64::encode(&data.into_owned()),
AttachmentType::File {
file,
filename: _,
} => {
let mut buf = Vec::new();
file.try_clone().await?.read_to_end(&mut buf).await?;
"data:image/png;base64,".to_string() + &base64::encode(&buf)
},
AttachmentType::Path(path) => {
let mut file = File::open(path).await?;
let mut buf = vec![];
file.read_to_end(&mut buf).await?;
"data:image/png;base64,".to_string() + &base64::encode(&buf)
},
AttachmentType::Image(url) => {
let url = Url::parse(url).map_err(|_| Error::Url(url.to_string()))?;
let response = http.as_ref().client.get(url).send().await?;
let mut bytes = response.bytes().await?;
let mut picture: Vec<u8> = vec![0; bytes.len()];
bytes.copy_to_slice(&mut picture[..]);
"data:image/png;base64,".to_string() + &base64::encode(&picture)
},
};
self.0.remove("unicode_emoji");
self.0.insert("icon", Value::String(icon));
Ok(self)
}
}