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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#[cfg(feature = "http")]
use super::Builder;
use super::CreateAttachment;
#[cfg(feature = "http")]
use crate::http::CacheHttp;
#[cfg(feature = "http")]
use crate::internal::prelude::*;
use crate::model::prelude::*;
/// A builder to create or edit a [`Role`] for use via a number of model methods.
///
/// These are:
///
/// - [`PartialGuild::create_role`]
/// - [`PartialGuild::edit_role`]
/// - [`Guild::create_role`]
/// - [`Guild::edit_role`]
/// - [`GuildId::create_role`]
/// - [`GuildId::edit_role`]
/// - [`Role::edit`]
///
/// Defaults are provided for each parameter on role creation.
///
/// # Examples
///
/// Create a hoisted, mentionable role named `"a test role"`:
///
/// ```rust,no_run
/// # use serenity_self::builder::EditRole;
/// # use serenity_self::http::Http;
/// # use serenity_self::model::id::GuildId;
/// # use std::sync::Arc;
/// #
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// # let http: Arc<Http> = unimplemented!();
/// # let guild_id: GuildId = unimplemented!();
/// #
/// // assuming a `guild_id` has been bound
/// let builder = EditRole::new().name("a test role").hoist(true).mentionable(true);
/// let role = guild_id.create_role(&http, builder).await?;
/// # Ok(())
/// # }
/// ```
///
/// [Discord docs](https://discord.com/developers/docs/resources/guild#modify-guild-role)
#[derive(Clone, Debug, Default, Serialize)]
#[must_use]
pub struct EditRole<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
permissions: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "color")]
colour: Option<Colour>,
#[serde(skip_serializing_if = "Option::is_none")]
hoist: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
icon: Option<Option<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
unicode_emoji: Option<Option<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
mentionable: Option<bool>,
#[serde(skip)]
position: Option<u16>,
#[serde(skip)]
audit_log_reason: Option<&'a str>,
}
impl<'a> EditRole<'a> {
/// Equivalent to [`Self::default`].
pub fn new() -> Self {
Self::default()
}
/// Creates a new builder with the values of the given [`Role`].
pub fn from_role(role: &Role) -> Self {
EditRole {
hoist: Some(role.hoist),
mentionable: Some(role.mentionable),
name: Some(role.name.clone()),
permissions: Some(role.permissions.bits()),
position: Some(role.position),
colour: Some(role.colour),
unicode_emoji: role.unicode_emoji.as_ref().map(|v| Some(v.clone())),
audit_log_reason: None,
// TODO: Do we want to download role.icon?
icon: None,
}
}
/// Set the colour of the role.
pub fn colour(mut self, colour: impl Into<Colour>) -> Self {
self.colour = Some(colour.into());
self
}
/// Whether or not to hoist the role above lower-positioned roles in the user list.
pub fn hoist(mut self, hoist: bool) -> Self {
self.hoist = Some(hoist);
self
}
/// Whether or not to make the role mentionable, upon which users with that role will be
/// notified.
pub fn mentionable(mut self, mentionable: bool) -> Self {
self.mentionable = Some(mentionable);
self
}
/// Set the role's name.
pub fn name(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
/// Set the role's permissions.
pub fn permissions(mut self, permissions: Permissions) -> Self {
self.permissions = Some(permissions.bits());
self
}
/// Set the role's position in the role list. This correlates to the role's position in the
/// user list.
pub fn position(mut self, position: u16) -> Self {
self.position = Some(position);
self
}
/// Set the role icon to a unicode emoji.
pub fn unicode_emoji(mut self, unicode_emoji: Option<String>) -> Self {
self.unicode_emoji = Some(unicode_emoji);
self.icon = Some(None);
self
}
/// Set the role icon to a custom image.
pub fn icon(mut self, icon: Option<&CreateAttachment>) -> Self {
self.icon = Some(icon.map(CreateAttachment::to_base64));
self.unicode_emoji = Some(None);
self
}
/// Sets the request's audit log reason.
pub fn audit_log_reason(mut self, reason: &'a str) -> Self {
self.audit_log_reason = Some(reason);
self
}
}
#[cfg(feature = "http")]
#[async_trait::async_trait]
impl Builder for EditRole<'_> {
type Context<'ctx> = (GuildId, Option<RoleId>);
type Built = Role;
/// Edits the role.
///
/// **Note**: Requires the [Manage Roles] permission.
///
/// # Errors
///
/// If the `cache` is enabled, returns a [`ModelError::InvalidPermissions`] if the current user
/// lacks permission. Otherwise returns [`Error::Http`], as well as if invalid data is given.
///
/// [Manage Roles]: Permissions::MANAGE_ROLES
async fn execute(
self,
cache_http: impl CacheHttp,
ctx: Self::Context<'_>,
) -> Result<Self::Built> {
let (guild_id, role_id) = ctx;
#[cfg(feature = "cache")]
crate::utils::user_has_guild_perms(&cache_http, guild_id, Permissions::MANAGE_ROLES)?;
let http = cache_http.http();
let role = match role_id {
Some(role_id) => {
http.edit_role(guild_id, role_id, &self, self.audit_log_reason).await?
},
None => http.create_role(guild_id, &self, self.audit_log_reason).await?,
};
if let Some(position) = self.position {
http.edit_role_position(guild_id, role.id, position, self.audit_log_reason).await?;
}
Ok(role)
}
}