diff --git a/src/commands/count.rs b/src/commands/count.rs index d51669e..b5713ea 100644 --- a/src/commands/count.rs +++ b/src/commands/count.rs @@ -83,9 +83,12 @@ pub mod committee { pub mod servers { // get the list of all the current clubs/socs + use serde::{Deserialize, Serialize}; use serenity::all::{CommandInteraction, CommandOptionType, Context, CreateCommand, CreateCommandOption}; use skynet_discord_bot::common::database::{get_server_config_bulk, DataBase}; use skynet_discord_bot::common::set_roles::committee::get_committees; + use skynet_discord_bot::get_now_iso; + use sqlx::{Pool, Sqlite}; use std::collections::HashMap; pub async fn run(_command: &CommandInteraction, ctx: &Context) -> String { @@ -108,7 +111,15 @@ pub mod servers { } } - cs.sort_by_key(|(count, _, _)| *count); + // get all members + let (wolves_current, wolves_past) = get_wolves_total(&db).await; + cs.push((wolves_current, wolves_past, String::from("Skynet Network"))); + + // treat teh committee server as its own thing + let committee_current = get_wolves_committee(&db).await; + cs.push((committee_current, 0, String::from("Committee"))); + + cs.sort_by_key(|(current, _, _)| *current); cs.reverse(); // msg can be a max 2000 chars long @@ -148,6 +159,74 @@ pub mod servers { response.join("\n") } + #[derive(Debug, Clone, Deserialize, Serialize, sqlx::FromRow)] + pub struct Count { + pub count: i64, + } + + async fn get_wolves_total(db: &Pool) -> (i64, i64) { + let total = match sqlx::query_as::<_, Count>( + r#" + SELECT COUNT(DISTINCT id_wolves) as count + FROM wolves + WHERE discord IS NOT NULL + "#, + ) + .fetch_one(db) + .await + { + Ok(res) => res.count, + Err(e) => { + dbg!(e); + 0 + } + }; + + let current = match sqlx::query_as::<_, Count>( + r#" + SELECT COUNT(DISTINCT id_wolves) as count + FROM server_members + JOIN wolves USING (id_wolves) + WHERE ( + wolves.discord IS NOT NULL + AND server_members.expiry > ? + ) + "#, + ) + .bind(get_now_iso(true)) + .fetch_one(db) + .await + { + Ok(res) => res.count, + Err(e) => { + dbg!(e); + 0 + } + }; + + (current, total) + } + + async fn get_wolves_committee(db: &Pool) -> i64 { + // expiry + match sqlx::query_as::<_, Count>( + " + SELECT count + FROM committee_roles + WHERE id_wolves = '0' + ", + ) + .fetch_one(db) + .await + { + Ok(res) => res.count, + Err(e) => { + dbg!(e); + 0 + } + } + } + pub fn register() -> CreateCommand { CreateCommand::new("count") .description("Count the servers")