From 0eba54b6f247e472d188837a2daaa0b44d90637e Mon Sep 17 00:00:00 2001 From: Brendan Golden Date: Wed, 19 Feb 2025 12:29:53 +0000 Subject: [PATCH] feat: split up the committee refresh from teh regular user refresh --- Cargo.toml | 3 +++ flake.nix | 2 ++ src/bin/update_committee.rs | 54 +++++++++++++++++++++++++++++++++++++ src/bin/update_users.rs | 3 --- src/common/set_roles.rs | 2 +- 5 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 src/bin/update_committee.rs diff --git a/Cargo.toml b/Cargo.toml index aa0cd6b..d999267 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,9 @@ name = "update_data" [[bin]] name = "update_users" +[[bin]] +name = "update_committee" + [[bin]] name = "update_minecraft" diff --git a/flake.nix b/flake.nix index 12ae36e..92eac43 100644 --- a/flake.nix +++ b/flake.nix @@ -122,6 +122,8 @@ "update_data" = "*:0,10,20,30,40,50"; # groups are updated every hour, offset from teh ldap "update_users" = "*:05:00"; + # Committee server has its own timer + "update_committee" = "*:15:00"; # minecraft stuff is updated at 5am "update_minecraft" = "5:10:00"; }; diff --git a/src/bin/update_committee.rs b/src/bin/update_committee.rs new file mode 100644 index 0000000..eae4eec --- /dev/null +++ b/src/bin/update_committee.rs @@ -0,0 +1,54 @@ +use serenity::{ + async_trait, + client::{Context, EventHandler}, + model::gateway::{GatewayIntents, Ready}, + Client, +}; +use skynet_discord_bot::common::database::{db_init, get_server_config_bulk, DataBase}; +use skynet_discord_bot::common::set_roles::{committee, normal}; +use skynet_discord_bot::{get_config, Config}; +use std::{process, sync::Arc}; +use tokio::sync::RwLock; + +#[tokio::main] +async fn main() { + let config = get_config(); + let db = match db_init(&config).await { + Ok(x) => x, + Err(_) => return, + }; + + // Intents are a bitflag, bitwise operations can be used to dictate which intents to use + let intents = GatewayIntents::GUILDS | GatewayIntents::GUILD_MESSAGES | GatewayIntents::MESSAGE_CONTENT | GatewayIntents::GUILD_MEMBERS; + // Build our client. + let mut client = Client::builder(&config.discord_token, intents) + .event_handler(Handler {}) + .await + .expect("Error creating client"); + + { + let mut data = client.data.write().await; + + data.insert::(Arc::new(RwLock::new(config))); + data.insert::(Arc::new(RwLock::new(db))); + } + + if let Err(why) = client.start().await { + println!("Client error: {:?}", why); + } +} + +struct Handler; +#[async_trait] +impl EventHandler for Handler { + async fn ready(&self, ctx: Context, ready: Ready) { + let ctx = Arc::new(ctx); + println!("{} is connected!", ready.user.name); + + // u[date committee server + committee::check_committee(Arc::clone(&ctx)).await; + + // finish up + process::exit(0); + } +} diff --git a/src/bin/update_users.rs b/src/bin/update_users.rs index e3170eb..301fcc4 100644 --- a/src/bin/update_users.rs +++ b/src/bin/update_users.rs @@ -48,9 +48,6 @@ impl EventHandler for Handler { // this goes into each server and sets roles for each wolves member check_bulk(Arc::clone(&ctx)).await; - // u[date committee server - committee::check_committee(Arc::clone(&ctx)).await; - // finish up process::exit(0); } diff --git a/src/common/set_roles.rs b/src/common/set_roles.rs index e47b95c..5710fb3 100644 --- a/src/common/set_roles.rs +++ b/src/common/set_roles.rs @@ -273,7 +273,7 @@ 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![committee_member]); + let values = users_roles.entry(member_tmp).or_insert(vec![]); values.push(r.id); } }