From 227db8a74130d104aff9e767fb155f72913e06a8 Mon Sep 17 00:00:00 2001 From: Brendan Golden Date: Sun, 20 Jul 2025 22:54:34 +0100 Subject: [PATCH] feat: moved the update data to the main thread --- Cargo.toml | 4 --- flake.nix | 2 -- src/bin/update_data.rs | 70 ------------------------------------------ src/main.rs | 22 ++++++++++++- 4 files changed, 21 insertions(+), 77 deletions(-) delete mode 100644 src/bin/update_data.rs diff --git a/Cargo.toml b/Cargo.toml index deb6676..ef48675 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,10 +4,6 @@ version = "0.1.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[[bin]] -name = "update_data" - [[bin]] name = "update_minecraft" diff --git a/flake.nix b/flake.nix index ea52e09..31d6434 100644 --- a/flake.nix +++ b/flake.nix @@ -152,8 +152,6 @@ # modify these scripts = { - # every 20 min - "update_data" = "*:0,10,20,30,40,50"; # minecraft stuff is updated at 5am "update_minecraft" = "5:10:00"; # server icon gets updated daily at midnight diff --git a/src/bin/update_data.rs b/src/bin/update_data.rs deleted file mode 100644 index d184ccd..0000000 --- a/src/bin/update_data.rs +++ /dev/null @@ -1,70 +0,0 @@ -use serenity::all::{ChunkGuildFilter, GuildId}; -use serenity::{ - async_trait, - client::{Context, EventHandler}, - model::gateway::{GatewayIntents, Ready}, - Client, -}; -use skynet_discord_bot::common::database::{db_init, DataBase}; -use skynet_discord_bot::common::wolves::cns::get_wolves; -use skynet_discord_bot::common::wolves::committees::get_cns; -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(e) => { - dbg!(e); - 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 {}) - .cache_settings(serenity::cache::Settings::default()) - .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 cache_ready(&self, ctx: Context, guilds: Vec) { - for guild in guilds { - ctx.shard.chunk_guild(guild, Some(2000), false, ChunkGuildFilter::None, None); - } - println!("Cache built successfully!"); - } - - async fn ready(&self, ctx: Context, ready: Ready) { - let ctx = Arc::new(ctx); - println!("{} is connected!", ready.user.name); - - // get the data for each individual club/soc - get_wolves(&ctx).await; - - // get teh data for the clubs/socs committees - get_cns(&ctx).await; - - // finish up - process::exit(0); - } -} diff --git a/src/main.rs b/src/main.rs index ffbd10d..df08f6b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,7 +19,8 @@ use serenity::{ use skynet_discord_bot::common::database::{db_init, get_server_config, get_server_config_bulk, get_server_member, DataBase}; use skynet_discord_bot::common::set_roles::committee::update_committees; use skynet_discord_bot::common::set_roles::{committee, normal}; -use skynet_discord_bot::common::wolves::committees::Committees; +use skynet_discord_bot::common::wolves::cns::get_wolves; +use skynet_discord_bot::common::wolves::committees::{get_cns, Committees}; use skynet_discord_bot::{get_config, Config}; use sqlx::{Pool, Sqlite}; use std::sync::atomic::{AtomicBool, Ordering}; @@ -49,6 +50,25 @@ impl EventHandler for Handler { if !self.is_loop_running.load(Ordering::Relaxed) { // We have to clone the Arc, as it gets moved into the new thread. + { + // This updates all the data, wolves user data and the committees + let ctx_task = Arc::clone(&ctx); + tokio::spawn(async move { + loop { + println!("Update - Data - Start"); + + // get the data for each individual club/soc + get_wolves(&ctx_task).await; + + // get teh data for the clubs/socs committees + get_cns(&ctx_task).await; + + println!("Update - Data - End"); + tokio::time::sleep(Duration::from_secs(60 * 5)).await; + } + }); + } + { // this is to update member roles every 5 min let ctx_task = Arc::clone(&ctx);