From 143483d3b3f8360631ea32fb9e66ddd8ee2ff736 Mon Sep 17 00:00:00 2001 From: Brendan Golden Date: Thu, 27 Feb 2025 03:02:35 +0000 Subject: [PATCH] feat: new command to see how many of each club/soc are on teh server --- src/commands/committee.rs | 80 +++++++++++++++++++++++++++++++++++++++ src/commands/mod.rs | 1 + src/common/set_roles.rs | 20 +++++----- src/main.rs | 18 +++++++++ 4 files changed, 110 insertions(+), 9 deletions(-) create mode 100644 src/commands/committee.rs diff --git a/src/commands/committee.rs b/src/commands/committee.rs new file mode 100644 index 0000000..4a4aa16 --- /dev/null +++ b/src/commands/committee.rs @@ -0,0 +1,80 @@ +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/mod.rs b/src/commands/mod.rs index 9e4170a..a11ffd5 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,4 +1,5 @@ pub mod add_server; +pub mod committee; 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 654e669..908e6d6 100644 --- a/src/common/set_roles.rs +++ b/src/common/set_roles.rs @@ -221,7 +221,7 @@ pub mod committee { x.name_role = committee.name_full.to_owned(); x.name_channel = committee.name_profile.to_owned(); } - + // handle new clubs/socs if let std::collections::hash_map::Entry::Vacant(e) = roles_db.entry(committee.id) { // create channel @@ -293,11 +293,13 @@ pub mod committee { // ID in this is the wolves ID, so we need to get a matching discord ID (if one exists) if let Some(x) = get_server_member_discord(db, id_wolves).await { if let Some(member_tmp) = x.discord { - let values = users_roles.entry(member_tmp).or_insert(vec![]); - values.push(r); + if server.member(ctx, &member_tmp).await.is_ok() { + let values = users_roles.entry(member_tmp).or_insert(vec![]); + values.push(r); - if let Some(x) = roles_db.get_mut(&committee.id) { - x.count += 1; + if let Some(x) = roles_db.get_mut(&committee.id) { + x.count += 1; + } } } } @@ -399,13 +401,13 @@ pub mod committee { } #[derive(Debug, Clone, Deserialize, Serialize)] - struct CommitteeRoles { + pub struct CommitteeRoles { id_wolves: i64, id_role: RoleId, id_channel: ChannelId, - name_role: String, + pub name_role: String, name_channel: String, - count: i64, + pub count: i64, } impl<'r> FromRow<'r, SqliteRow> for CommitteeRoles { @@ -447,7 +449,7 @@ pub mod committee { } } - async fn db_roles_get(db: &Pool) -> Vec { + pub async fn db_roles_get(db: &Pool) -> Vec { // expiry sqlx::query_as::<_, CommitteeRoles>( " diff --git a/src/main.rs b/src/main.rs index 5dab0f0..43ad82a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -130,6 +130,23 @@ Sign up on [UL Wolves]({}) and go to https://discord.com/channels/{}/{} and use println!("{:?}", e) } } + + let config_lock = { + let data_read = ctx.data.read().await; + data_read.get::().expect("Expected Config in TypeMap.").clone() + }; + let config_global = config_lock.read().await; + + match &config_global + .committee_server + .set_commands(&ctx.http, vec![commands::committee::count::register()]) + .await + { + Ok(_) => {} + Err(e) => { + println!("{:?}", e) + } + } } async fn interaction_create(&self, ctx: Context, interaction: Interaction) { @@ -148,6 +165,7 @@ 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(), };