From 1694c4eebfaeba5507826cd2614c04177986ec37 Mon Sep 17 00:00:00 2001 From: Brendan Golden Date: Sun, 27 Aug 2023 16:51:55 +0100 Subject: [PATCH] feat: config is now shared --- src/main.rs | 110 +++++++++++++++++++++++++--------------------------- 1 file changed, 52 insertions(+), 58 deletions(-) diff --git a/src/main.rs b/src/main.rs index d408620..d7c1535 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,18 +16,18 @@ struct Handler { #[async_trait] impl EventHandler for Handler { async fn guild_member_addition(&self, ctx: Context, mut new_member: Member) { - let config = Config { - server: GuildId(957961810147938334), - member_role_current: RoleId::from(1144760670995370094), - member_role_past: RoleId::from(1144760548072886353), + let config_lock = { + let data_read = ctx.data.read().await; + data_read.get::().expect("Expected Config in TypeMap.").clone() }; + let config = config_lock.read().await; let members_lock = { let data_read = ctx.data.read().await; data_read.get::().expect("Expected Members in TypeMap.").clone() }; - let members = members_lock.read().await; + if members.contains(&new_member.user.name) { let mut roles = vec![]; @@ -48,11 +48,13 @@ impl EventHandler for Handler { let ctx = Arc::new(ctx); println!("{} is connected!", ready.user.name); - let config = Config { - server: GuildId(957961810147938334), - member_role_current: RoleId::from(1144760670995370094), - member_role_past: RoleId::from(1144760548072886353), + let config_lock = { + let data_read = ctx.data.read().await; + data_read.get::().expect("Expected Config in TypeMap.").clone() }; + let config = config_lock.read().await; + let timing_update = config.timing_update; + let timing_fetch = config.timing_fetch; if !self.is_loop_running.load(Ordering::Relaxed) { // We have to clone the Arc, as it gets moved into the new thread. @@ -63,29 +65,19 @@ impl EventHandler for Handler { loop { // We clone Context again here, because Arc is owned, so it moves to the // new function. - bulk_check(Arc::clone(&ctx1), &config).await; - tokio::time::sleep(Duration::from_secs(30)).await; + bulk_check(Arc::clone(&ctx1)).await; + tokio::time::sleep(Duration::from_secs(timing_update)).await; } }); - - // And of course, we can run more than one thread at different timings. + let ctx2 = Arc::clone(&ctx); tokio::spawn(async move { loop { - bulk_check2(Arc::clone(&ctx2)).await; - tokio::time::sleep(Duration::from_secs(15)).await; + fetch_accounts(Arc::clone(&ctx2)).await; + tokio::time::sleep(Duration::from_secs(timing_fetch)).await; } }); - let ctx3 = Arc::clone(&ctx); - tokio::spawn(async move { - loop { - fetch_accounts(Arc::clone(&ctx3)).await; - tokio::time::sleep(Duration::from_secs(50)).await; - } - }); - // fetch_accounts - // Now that the loop is running, we set the bool to true self.is_loop_running.swap(true, Ordering::Relaxed); } @@ -96,6 +88,12 @@ struct Config { server: GuildId, member_role_current: RoleId, member_role_past: RoleId, + auth: String, + timing_update: u64, + timing_fetch: u64, +} +impl TypeMapKey for Config { + type Value = Arc>; } #[derive(Default, Debug)] @@ -112,11 +110,19 @@ struct Members; impl TypeMapKey for Members { type Value = Arc>>; } -async fn bulk_check(ctx: Arc, config: &Config){ +async fn bulk_check(ctx: Arc){ + let config_lock = { + let data_read = ctx.data.read().await; + data_read.get::().expect("Expected Config in TypeMap.").clone() + }; + let config = config_lock.read().await; + let members_lock = { let data_read = ctx.data.read().await; data_read.get::().expect("Expected Members in TypeMap.").clone() }; + let members = members_lock.read().await; + let mut roles_set = [0,0,0]; let mut res = MembersCount { members: 0, @@ -124,7 +130,7 @@ async fn bulk_check(ctx: Arc, config: &Config){ }; if let Ok(x) = config.server.members(&ctx, None, None).await { for mut member in x { - if members_lock.read().await.contains(&member.user.name) { + if members.contains(&member.user.name) { let mut roles = vec![]; if !member.roles.contains(&config.member_role_past){ @@ -158,11 +164,9 @@ async fn bulk_check(ctx: Arc, config: &Config){ // small bit of logging to note changes over time println!("Changes: New: +{}, Current: +{}/-{}", roles_set[0], roles_set[1], roles_set[2]); - let counter_lock = { - let data_read = ctx.data.read().await; - data_read.get::().expect("Expected MemberCounter in TypeMap.").clone() - }; { + let data_read = ctx.data.read().await; + let counter_lock = data_read.get::().expect("Expected MemberCounter in TypeMap.").clone(); // The HashMap of CommandCounter is wrapped in an RwLock; since we want to write to it, we will // open the lock in write mode. let mut counter = counter_lock.write().await; @@ -170,11 +174,16 @@ async fn bulk_check(ctx: Arc, config: &Config){ // And we write the amount of times the command has been called to it. counter.members_current = res.members_current; counter.members = res.members; - } + }; } async fn fetch_accounts(ctx: Arc){ - let auth = "abcdef"; + let config_lock = { + let data_read = ctx.data.read().await; + data_read.get::().expect("Expected Config in TypeMap.").clone() + }; + let config = config_lock.read().await; + let auth = &config.auth; let url = format!("http://127.0.0.1:8087/ldap/discord?auth={}", auth); if let Ok(result) = surf::get(url).recv_json::>().await { let members_lock = { @@ -186,32 +195,6 @@ async fn fetch_accounts(ctx: Arc){ } } -async fn bulk_check2(ctx: Arc){ - let counter_lock = { - let data_read = ctx.data.read().await; - data_read.get::().expect("Expected MemberCounter in TypeMap.").clone() - }; - { - // The HashMap of CommandCounter is wrapped in an RwLock; since we want to write to it, we will - // open the lock in write mode. - let counter = counter_lock.read().await; - println!("Members: {:?}", counter); - - } - - let members_lock = { - let data_read = ctx.data.read().await; - data_read.get::().expect("Expected Members in TypeMap.").clone() - }; - { - // The HashMap of CommandCounter is wrapped in an RwLock; since we want to write to it, we will - // open the lock in write mode. - let counter = members_lock.read().await; - println!("Members: {:?}", counter); - - } -} - #[tokio::main] async fn main() { // Configure the client with your Discord bot token in the environment. @@ -234,6 +217,17 @@ async fn main() { // a list of all current members data.insert::(Arc::new(RwLock::new(vec![]))); + + + let config = Config { + server: GuildId(957961810147938334), + member_role_current: RoleId::from(1144760670995370094), + member_role_past: RoleId::from(1144760548072886353), + auth: String::from("abcdef"), + timing_update: 30, + timing_fetch: 50, + }; + data.insert::(Arc::new(RwLock::new(config))); }