use crate::models::{HasId, Snowflake};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Role {
pub id: Snowflake,
pub name: String,
pub color: u32,
#[serde(default)]
pub hoist: bool,
pub position: i32,
pub permissions: String,
#[serde(default)]
pub managed: bool,
#[serde(default)]
pub mentionable: bool,
}
impl Role {
pub fn new(id: impl Into<Snowflake>, name: impl Into<String>) -> Self {
Self {
id: id.into(),
name: name.into(),
color: 0,
hoist: false,
position: 0,
permissions: "0".to_string(),
managed: false,
mentionable: false,
}
}
pub fn mention(&self) -> String {
format!("<@&{}>", self.id)
}
pub fn rgb(&self) -> (u8, u8, u8) {
let r = ((self.color >> 16) & 0xFF) as u8;
let g = ((self.color >> 8) & 0xFF) as u8;
let b = (self.color & 0xFF) as u8;
(r, g, b)
}
pub fn hex_color(&self) -> String {
format!("#{:06X}", self.color)
}
}
impl HasId for Role {
fn id(&self) -> Option<&Snowflake> {
Some(&self.id)
}
}
impl crate::models::HasName for Role {
fn name(&self) -> &str {
&self.name
}
}