From 7d7afcd00cd8f0993b26cef88c3009f0df44315d Mon Sep 17 00:00:00 2001 From: Brendan Golden Date: Thu, 27 Feb 2025 23:56:48 +0000 Subject: [PATCH] feat: new command to count the server info, available on the computer soc server --- src/commands/committee.rs | 80 ------------------- src/commands/count.rs | 156 ++++++++++++++++++++++++++++++++++++++ src/commands/mod.rs | 2 +- src/common/set_roles.rs | 2 +- src/main.rs | 26 ++++++- 5 files changed, 180 insertions(+), 86 deletions(-) delete mode 100644 src/commands/committee.rs create mode 100644 src/commands/count.rs diff --git a/src/commands/committee.rs b/src/commands/committee.rs deleted file mode 100644 index 4a4aa16..0000000 --- a/src/commands/committee.rs +++ /dev/null @@ -1,80 +0,0 @@ -pub mod count { - - // get the list of all the current clubs/socs members - - use serenity::all::{ - CommandDataOption, CommandDataOptionValue, CommandInteraction, CommandOptionType, Context, CreateCommand, CreateCommandOption, - }; - use skynet_discord_bot::common::database::DataBase; - use skynet_discord_bot::common::set_roles::committee::db_roles_get; - - pub async fn run(command: &CommandInteraction, ctx: &Context) -> String { - let sub_options = if let Some(CommandDataOption { - value: CommandDataOptionValue::SubCommand(key), - .. - }) = command.data.options.first() - { - key - } else { - return "Please provide a wolves API key".to_string(); - }; - - let all = if let Some(x) = sub_options.first() { - match x.value { - CommandDataOptionValue::Boolean(y) => y, - _ => false, - } - } else { - false - }; - - let db_lock = { - let data_read = ctx.data.read().await; - data_read.get::().expect("Expected Databse in TypeMap.").clone() - }; - let db = db_lock.read().await; - - let mut cs = vec![]; - // pull it from a DB - for committee in db_roles_get(&db).await { - if !all && committee.count == 0 { - continue; - } - cs.push((committee.count, committee.name_role.to_owned())); - } - - cs.sort_by_key(|(count, _)| *count); - cs.reverse(); - - // msg can be a max 2000 chars long - let mut limit = 2000; - - let mut response = vec![]; - for (count, name) in cs { - let leading = if count < 10 { " " } else { "" }; - - let line = format!("{}{} {}", leading, count, name); - - let length = line.len() + 1; - - if length < limit { - response.push(line); - limit -= length; - } else { - break; - } - } - - response.join("\n") - } - - pub fn register() -> CreateCommand { - CreateCommand::new("committee") - .description("Commands related to teh committees") - //.default_member_permissions(serenity::model::Permissions::MANAGE_GUILD) - .add_option( - CreateCommandOption::new(CommandOptionType::SubCommand, "list", "List out the Committee Roles Numbers") - .add_sub_option(CreateCommandOption::new(CommandOptionType::Boolean, "all", "List out all the Committee Roles Numbers").required(false)), - ) - } -} diff --git a/src/commands/count.rs b/src/commands/count.rs new file mode 100644 index 0000000..bb64f68 --- /dev/null +++ b/src/commands/count.rs @@ -0,0 +1,156 @@ +pub mod committee { + + // get the list of all the current clubs/socs members + + use serenity::all::{ + CommandDataOption, CommandDataOptionValue, CommandInteraction, CommandOptionType, Context, CreateCommand, CreateCommandOption, + }; + use skynet_discord_bot::common::database::DataBase; + use skynet_discord_bot::common::set_roles::committee::db_roles_get; + + pub async fn run(command: &CommandInteraction, ctx: &Context) -> String { + let sub_options = if let Some(CommandDataOption { + value: CommandDataOptionValue::SubCommand(key), + .. + }) = command.data.options.first() + { + key + } else { + return "Please provide a wolves API key".to_string(); + }; + + let all = if let Some(x) = sub_options.first() { + match x.value { + CommandDataOptionValue::Boolean(y) => y, + _ => false, + } + } else { + false + }; + + let db_lock = { + let data_read = ctx.data.read().await; + data_read.get::().expect("Expected Databse in TypeMap.").clone() + }; + let db = db_lock.read().await; + + let mut cs = vec![]; + // pull it from a DB + for committee in db_roles_get(&db).await { + if !all && committee.count == 0 { + continue; + } + cs.push((committee.count, committee.name_role.to_owned())); + } + + cs.sort_by_key(|(count, _)| *count); + cs.reverse(); + + // msg can be a max 2000 chars long + let mut limit = 2000 - 3; + + let mut response = vec!["```".to_string()]; + for (count, name) in cs { + let leading = if count < 10 { " " } else { "" }; + + let line = format!("{}{} {}", leading, count, name); + + let length = line.len() + 1; + + if length < (limit + 3) { + response.push(line); + limit -= length; + } else { + break; + } + } + response.push("```".to_string()); + + response.join("\n") + } + + pub fn register() -> CreateCommand { + CreateCommand::new("count") + .description("Commands related to teh committees") + //.default_member_permissions(serenity::model::Permissions::MANAGE_GUILD) + .add_option( + CreateCommandOption::new(CommandOptionType::SubCommand, "committee", "List out the Committee Roles Numbers") + .add_sub_option(CreateCommandOption::new(CommandOptionType::Boolean, "all", "List out all the Committee Roles Numbers").required(false)), + ) + } +} + +pub mod servers { + // get the list of all the current clubs/socs + 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 std::collections::HashMap; + + pub async fn run(_command: &CommandInteraction, ctx: &Context) -> String { + let db_lock = { + let data_read = ctx.data.read().await; + data_read.get::().expect("Expected Databse in TypeMap.").clone() + }; + let db = db_lock.read().await; + + let mut committees = HashMap::new(); + for committee in get_committees(&db).await { + committees.insert(committee.id, committee.to_owned()); + } + + let mut cs = vec![]; + // pull it from a DB + for server_config in get_server_config_bulk(&db).await { + if let Some(x) = committees.get(&server_config.wolves_id) { + cs.push((server_config.member_current, server_config.member_past, x.name_full.to_owned())); + } + } + + cs.sort_by_key(|(count, _, _)| *count); + cs.reverse(); + + // msg can be a max 2000 chars long + let mut limit = 2000 - 3; + + let mut response = vec!["```".to_string()]; + for (current, past, name) in cs { + let current_leading = if current < 10 { + " " + } else if current < 100 { + " " + } else { + "" + }; + let past_leading = if past < 10 { + " " + } else if past < 100 { + " " + } else { + "" + }; + + let line = format!("{}{} {}{} {}", current_leading, current, past_leading, past, name); + + let length = line.len() + 1; + + // +3 is to account for the closing fense + if length < (limit + 3) { + response.push(line); + limit -= length; + } else { + break; + } + } + response.push("```".to_string()); + + response.join("\n") + } + + pub fn register() -> CreateCommand { + CreateCommand::new("count") + .description("Commands related to all servers") + .default_member_permissions(serenity::model::Permissions::MANAGE_GUILD) + .add_option(CreateCommandOption::new(CommandOptionType::SubCommand, "servers", "List out all servers using the skynet bot")) + } +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index a11ffd5..76b43cb 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,5 +1,5 @@ pub mod add_server; -pub mod committee; +pub mod count; pub mod link_email; pub mod minecraft; pub mod role_adder; diff --git a/src/common/set_roles.rs b/src/common/set_roles.rs index 52836e1..323677f 100644 --- a/src/common/set_roles.rs +++ b/src/common/set_roles.rs @@ -469,7 +469,7 @@ pub mod committee { }) } - async fn get_committees(db: &Pool) -> Vec { + pub async fn get_committees(db: &Pool) -> Vec { sqlx::query_as::<_, Committees>( r#" SELECT * diff --git a/src/main.rs b/src/main.rs index 43ad82a..1efaed7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ pub mod commands; use crate::commands::role_adder::tools::on_role_change; -use serenity::all::{ActivityData, Command, CreateMessage, EditInteractionResponse, GuildMemberUpdateEvent, Interaction}; +use serenity::all::{ActivityData, Command, CreateMessage, EditInteractionResponse, GuildId, GuildMemberUpdateEvent, Interaction}; use serenity::model::guild::Member; use serenity::{ async_trait, @@ -139,7 +139,17 @@ Sign up on [UL Wolves]({}) and go to https://discord.com/channels/{}/{} and use match &config_global .committee_server - .set_commands(&ctx.http, vec![commands::committee::count::register()]) + .set_commands(&ctx.http, vec![commands::count::committee::register()]) + .await + { + Ok(_) => {} + Err(e) => { + println!("{:?}", e) + } + } + + match GuildId::new(689189992417067052) + .set_commands(&ctx.http, vec![commands::count::servers::register()]) .await { Ok(_) => {} @@ -165,8 +175,16 @@ Sign up on [UL Wolves]({}) and go to https://discord.com/channels/{}/{} and use "minecraft_add" => commands::minecraft::server::add::run(&command, &ctx).await, "minecraft_list" => commands::minecraft::server::list::run(&command, &ctx).await, "minecraft_delete" => commands::minecraft::server::delete::run(&command, &ctx).await, - "committee" => commands::committee::count::run(&command, &ctx).await, - _ => "not implemented :(".to_string(), + // sub command + "count" => match command.data.options.first() { + None => "Invalid Command".to_string(), + Some(x) => match x.name.as_str() { + "committee" => commands::count::committee::run(&command, &ctx).await, + "servers" => commands::count::servers::run(&command, &ctx).await, + &_ => format!("not implemented :( count {}", x.name.as_str()), + }, + }, + _ => format!("not implemented :( {}", command.data.name.as_str()), }; if let Err(why) = command.edit_response(&ctx.http, EditInteractionResponse::new().content(content)).await {