Add role commands
This commit is contained in:
8 files changed
+241
-5
No files matched your search
@@ -0,0 +1,60 @@
|
||||
use crate::error::Error;
|
||||
use j_db::database::Database;
|
||||
use poise::serenity_prelude::RoleId;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ManagedRole {
|
||||
id: Option<u64>,
|
||||
pub role_id: RoleId,
|
||||
pub description: String,
|
||||
}
|
||||
|
||||
impl j_db::model::JdbModel for ManagedRole {
|
||||
fn id(&self) -> Option<u64> {
|
||||
self.id
|
||||
}
|
||||
|
||||
fn set_id(&mut self, id: u64) {
|
||||
self.id = Some(id)
|
||||
}
|
||||
|
||||
fn tree() -> String {
|
||||
"ManagedRoles".to_string()
|
||||
}
|
||||
}
|
||||
|
||||
impl ManagedRole {
|
||||
pub fn new(role_id: RoleId, description: String) -> Self {
|
||||
Self {
|
||||
id: None,
|
||||
role_id,
|
||||
description,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_role(db: &Database, role_id: RoleId, description: String) -> Result<Self, Error> {
|
||||
Ok(db.insert(Self::new(role_id, description))?)
|
||||
}
|
||||
|
||||
pub fn get_role(db: &Database, role_id: RoleId) -> Result<Option<Self>, Error> {
|
||||
Ok(db
|
||||
.filter(|_, role: &Self| role.role_id == role_id)?
|
||||
.next()
|
||||
.clone())
|
||||
}
|
||||
|
||||
pub fn list_roles(db: &Database) -> Result<Vec<Self>, Error> {
|
||||
Ok(db.filter(|_, _role: &Self| true)?.collect())
|
||||
}
|
||||
|
||||
pub fn remove_role(db: &Database, role_id: RoleId) -> Result<(), Error> {
|
||||
let role = Self::get_role(db, role_id)?;
|
||||
|
||||
if let Some(role) = role {
|
||||
db.remove::<Self>(role.id.unwrap())?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ pub mod api_key;
|
||||
pub mod birthday;
|
||||
pub mod insult_compliment;
|
||||
pub mod lil_fren;
|
||||
pub mod managed_roles;
|
||||
pub mod motivation;
|
||||
pub mod race;
|
||||
pub mod random;
|
||||
|
||||
Reference in new issue
Block a user