#[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::*;
#[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")]
#[serde(rename = "colors")]
colours: Option<CreateRoleColours>,
#[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> {
pub fn new() -> Self {
Self::default()
}
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,
colours: Some(role.colours.into()),
icon: None,
}
}
pub fn colour(mut self, colour: impl Into<Colour>) -> Self {
self.colour = Some(colour.into());
self
}
pub fn colours(mut self, colours: impl Into<CreateRoleColours>) -> Self {
self.colours = Some(colours.into());
self
}
pub fn hoist(mut self, hoist: bool) -> Self {
self.hoist = Some(hoist);
self
}
pub fn mentionable(mut self, mentionable: bool) -> Self {
self.mentionable = Some(mentionable);
self
}
pub fn name(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
pub fn permissions(mut self, permissions: Permissions) -> Self {
self.permissions = Some(permissions.bits());
self
}
pub fn position(mut self, position: u16) -> Self {
self.position = Some(position);
self
}
pub fn unicode_emoji(mut self, unicode_emoji: Option<String>) -> Self {
self.unicode_emoji = Some(unicode_emoji);
self.icon = Some(None);
self
}
pub fn icon(mut self, icon: Option<&CreateAttachment>) -> Self {
self.icon = Some(icon.map(CreateAttachment::to_base64));
self.unicode_emoji = Some(None);
self
}
pub fn audit_log_reason(mut self, reason: &'a str) -> Self {
self.audit_log_reason = Some(reason);
self
}
}
#[derive(Clone, Debug, Default, Serialize)]
#[must_use]
#[allow(clippy::struct_field_names)]
pub struct CreateRoleColours {
primary_color: Colour,
#[serde(skip_serializing_if = "Option::is_none")]
secondary_color: Option<Colour>,
#[serde(skip_serializing_if = "Option::is_none")]
tertiary_color: Option<Colour>,
}
impl CreateRoleColours {
pub fn new(primary_colour: Colour) -> Self {
Self {
primary_color: primary_colour,
secondary_color: None,
tertiary_color: None,
}
}
pub fn secondary_colour(mut self, secondary_colour: Colour) -> Self {
self.secondary_color = Some(secondary_colour);
self
}
pub fn tertiary_colour(mut self, tertiary_colour: Colour) -> Self {
self.tertiary_color = Some(tertiary_colour);
self
}
}
impl From<RoleColours> for CreateRoleColours {
fn from(c: RoleColours) -> CreateRoleColours {
CreateRoleColours {
primary_color: c.primary_colour,
secondary_color: c.secondary_colour,
tertiary_color: c.tertiary_colour,
}
}
}
#[cfg(feature = "http")]
#[async_trait::async_trait]
impl Builder for EditRole<'_> {
type Context<'ctx> = (GuildId, Option<RoleId>);
type Built = Role;
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)
}
}