From 095ff6f2cea5b3d35880d4a55b60930b60e3efcb Mon Sep 17 00:00:00 2001 From: Brendan Golden Date: Mon, 21 Jul 2025 02:51:33 +0100 Subject: [PATCH 1/8] fix: better handling of returning teh committees --- src/commands/count.rs | 6 ++++-- src/common/set_roles.rs | 26 +++++++++++++++++++------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/commands/count.rs b/src/commands/count.rs index 8a64ee7..2368bca 100644 --- a/src/commands/count.rs +++ b/src/commands/count.rs @@ -102,8 +102,10 @@ pub mod servers { 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()); + if let Some(x) = get_committees(&db).await { + for committee in x { + committees.insert(committee.id, committee.to_owned()); + } } let mut cs = vec![]; diff --git a/src/common/set_roles.rs b/src/common/set_roles.rs index 6352c24..6e71992 100644 --- a/src/common/set_roles.rs +++ b/src/common/set_roles.rs @@ -201,7 +201,12 @@ pub mod committee { pub async fn update_committees(db: &Pool, ctx: &Context, config: &Config, members: &mut Vec) { let server = config.committee_server; let committee_member = config.committee_role; - let committees = get_committees(db).await; + let committees = match get_committees(db).await { + None => { + return; + } + Some(x) => x, + }; let categories = config.committee_category.clone(); // information about the server @@ -328,6 +333,10 @@ pub mod committee { // now we have a map of all users that should get roles time to go through all the folks on teh server for member in members { + // if member.user.id != 136522490632601600 { + // continue; + // } + // let roles_current = member.roles(ctx).unwrap_or_default(); let roles_required = match users_roles.get(&member.user.id) { @@ -494,8 +503,8 @@ pub mod committee { }) } - pub async fn get_committees(db: &Pool) -> Vec { - sqlx::query_as::<_, Committees>( + pub async fn get_committees(db: &Pool) -> Option> { + match sqlx::query_as::<_, Committees>( r#" SELECT * FROM committees @@ -503,10 +512,13 @@ pub mod committee { ) .fetch_all(db) .await - .unwrap_or_else(|e| { - dbg!(e); - vec![] - }) + { + Ok(x) => Some(x), + Err(e) => { + dbg!(e); + None + } + } } async fn get_server_member_discord(db: &Pool, user: &i64) -> Option { From a225c14b4fdaafe390dc8840065946d8c2a2e4b8 Mon Sep 17 00:00:00 2001 From: Brendan Golden Date: Mon, 21 Jul 2025 04:29:03 +0100 Subject: [PATCH 2/8] fix: was ddossing the poor database guild_members_chunk is triggered for each chunk for each server it is on, and the bot is currently in 10 servers so it was runnign teh same thigns 10 times, clogging up conenctions --- src/bin/cleanup_committee.rs | 18 +++++++++++++----- src/bin/update_committee.rs | 18 +++++++++++++----- src/bin/update_users.rs | 27 ++++++++++++++++++++------- 3 files changed, 46 insertions(+), 17 deletions(-) diff --git a/src/bin/cleanup_committee.rs b/src/bin/cleanup_committee.rs index c6485a7..188cc5b 100644 --- a/src/bin/cleanup_committee.rs +++ b/src/bin/cleanup_committee.rs @@ -52,15 +52,23 @@ async fn main() { 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 cache_ready(&self, ctx: Context, _guilds: Vec) { + 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; + + let server = config_global.committee_server; + + ctx.shard.chunk_guild(server, Some(2000), false, ChunkGuildFilter::None, None); + + println!("Cache loaded"); } async fn guild_members_chunk(&self, ctx: Context, chunk: GuildMembersChunkEvent) { if (chunk.chunk_index + 1) == chunk.chunk_count { + println!("Cache built successfully!"); let db_lock = { let data_read = ctx.data.read().await; data_read.get::().expect("Expected Config in TypeMap.").clone() diff --git a/src/bin/update_committee.rs b/src/bin/update_committee.rs index b2792e0..b8cc6c2 100644 --- a/src/bin/update_committee.rs +++ b/src/bin/update_committee.rs @@ -47,15 +47,23 @@ async fn main() { 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 cache_ready(&self, ctx: Context, _guilds: Vec) { + 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; + + let server = config_global.committee_server; + + ctx.shard.chunk_guild(server, Some(2000), false, ChunkGuildFilter::None, None); + + println!("Cache loaded"); } async fn guild_members_chunk(&self, ctx: Context, chunk: GuildMembersChunkEvent) { if (chunk.chunk_index + 1) == chunk.chunk_count { + println!("Cache built successfully!"); // u[date committee server committee::check_committee(&ctx).await; diff --git a/src/bin/update_users.rs b/src/bin/update_users.rs index 6ac9e00..0cb11ac 100644 --- a/src/bin/update_users.rs +++ b/src/bin/update_users.rs @@ -12,6 +12,7 @@ use skynet_discord_bot::{ }, get_config, Config, }; +use std::sync::atomic::{AtomicUsize, Ordering}; use std::{process, sync::Arc}; use tokio::sync::RwLock; @@ -27,7 +28,10 @@ async fn main() { 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 {}) + .event_handler(Handler { + server_count: Default::default(), + server_cached: Default::default(), + }) .cache_settings(serenity::cache::Settings::default()) .await .expect("Error creating client"); @@ -44,23 +48,32 @@ async fn main() { } } -struct Handler; +struct Handler { + server_count: AtomicUsize, + server_cached: AtomicUsize, +} #[async_trait] impl EventHandler for Handler { async fn cache_ready(&self, ctx: Context, guilds: Vec) { + self.server_count.swap(guilds.len(), Ordering::SeqCst); for guild in guilds { ctx.shard.chunk_guild(guild, Some(2000), false, ChunkGuildFilter::None, None); } - println!("Cache built successfully!"); + println!("Cache loaded {}", &self.server_count.load(Ordering::SeqCst)); } async fn guild_members_chunk(&self, ctx: Context, chunk: GuildMembersChunkEvent) { if (chunk.chunk_index + 1) == chunk.chunk_count { - // this goes into each server and sets roles for each wolves member - check_bulk(&ctx).await; + self.server_cached.fetch_add(1, Ordering::SeqCst); + if (self.server_cached.load(Ordering::SeqCst) + 1) == self.server_count.load(Ordering::SeqCst) { + println!("Cache built successfully!"); - // finish up - process::exit(0); + // this goes into each server and sets roles for each wolves member + check_bulk(&ctx).await; + + // finish up + process::exit(0); + } } } } From 061b73378a9484478226559f4a70cdb6721e7e13 Mon Sep 17 00:00:00 2001 From: silver Date: Sun, 31 Aug 2025 11:51:57 +0000 Subject: [PATCH 3/8] Bump the bot --- src/main.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main.rs b/src/main.rs index 6a87f84..aecabec 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,6 +27,7 @@ use sqlx::{Pool, Sqlite}; use std::sync::Arc; use tokio::sync::RwLock; +// Need To Define The Stuct (Committed To Bump The Bot) struct Handler; #[async_trait] From 3149a5f99fc927397fec18840ceb9cbe54d65ce0 Mon Sep 17 00:00:00 2001 From: silver Date: Sun, 31 Aug 2025 11:55:35 +0000 Subject: [PATCH 4/8] Bump 2 --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index aecabec..bbf389f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,7 +27,7 @@ use sqlx::{Pool, Sqlite}; use std::sync::Arc; use tokio::sync::RwLock; -// Need To Define The Stuct (Committed To Bump The Bot) +// Need To Define The Stuct (Committed To Bump The Bot) struct Handler; #[async_trait] From 7526a82bb7c384c16a09992458868b2f669ed279 Mon Sep 17 00:00:00 2001 From: Brendan Golden Date: Thu, 4 Sep 2025 23:11:24 +0100 Subject: [PATCH 5/8] feat: kill the service if it persists longer than 9 min --- flake.nix | 2 ++ 1 file changed, 2 insertions(+) diff --git a/flake.nix b/flake.nix index 81f4490..3a6032f 100644 --- a/flake.nix +++ b/flake.nix @@ -128,6 +128,8 @@ User = "${cfg.user}"; Group = "${cfg.user}"; ExecStart = "${self.defaultPackage."${system}"}/bin/${script}"; + # kill each service if its ran for 9 min + TimeoutStartSec=540; EnvironmentFile = [ "${cfg.env.discord}" "${cfg.env.mail}" From 7e90f451965b0edbd331765ad295a02f31d2bf24 Mon Sep 17 00:00:00 2001 From: Roman Moisieiev Date: Thu, 11 Sep 2025 12:35:50 +0100 Subject: [PATCH 6/8] Fix typos --- .taplo.toml | 2 ++ src/bin/cleanup_committee.rs | 8 ++++---- src/commands/add_server.rs | 2 +- src/commands/count.rs | 2 +- src/commands/minecraft.rs | 2 +- src/commands/role_adder.rs | 4 ++-- src/commands/server_icon.rs | 2 +- src/commands/wolves.rs | 8 ++++---- src/common/minecraft.rs | 4 ++-- src/common/renderer.rs | 5 ++--- src/common/server_icon.rs | 2 +- src/common/set_roles.rs | 12 ++++++------ src/lib.rs | 2 +- src/main.rs | 9 ++++----- 14 files changed, 32 insertions(+), 32 deletions(-) create mode 100644 .taplo.toml diff --git a/.taplo.toml b/.taplo.toml new file mode 100644 index 0000000..3b409e9 --- /dev/null +++ b/.taplo.toml @@ -0,0 +1,2 @@ +[formatting] +column_width = 120 diff --git a/src/bin/cleanup_committee.rs b/src/bin/cleanup_committee.rs index 188cc5b..9a01f4c 100644 --- a/src/bin/cleanup_committee.rs +++ b/src/bin/cleanup_committee.rs @@ -18,8 +18,8 @@ use tokio::sync::RwLock; /// Cleanup teh Committee server /// -/// This removes any invalid roles/channels which ay have been set up accidentally -/// DO NOT run this locally unless you have a fresh copy of teh live database handy. +/// This removes any invalid roles/channels which have been set up accidentally +/// DO NOT run this locally unless you have a fresh copy of the live database handy. #[tokio::main] async fn main() { let config = get_config(); @@ -98,7 +98,7 @@ async fn cleanup(db: &Pool, ctx: &Context, config: &Config) { let name = &channel.name; let committee_tmp = committees.iter().filter(|x| &x.name_channel == name).collect::>(); let committee = match committee_tmp.first() { - // if there are no committees which match then this is not a channelw e care about + // if there are no committees which match then this is not a channel we care about None => { continue; } @@ -120,7 +120,7 @@ async fn cleanup(db: &Pool, ctx: &Context, config: &Config) { let name = &role.name; let committee_tmp = committees.iter().filter(|x| &x.name_role == name).collect::>(); let committee = match committee_tmp.first() { - // if there are no committees which match then this is not a channelw e care about + // if there are no committees which match then this is not a channel we care about None => { continue; } diff --git a/src/commands/add_server.rs b/src/commands/add_server.rs index 0c7dd2f..4511c1b 100644 --- a/src/commands/add_server.rs +++ b/src/commands/add_server.rs @@ -102,7 +102,7 @@ async fn add_server(db: &Pool, ctx: &Context, server: &Servers) -> Resul .fetch_optional(db) .await; - // if the entry does not exist already tehn do a user update + // if the entry does not exist already then do a user update let (update, current_remove, current_role, past_remove, past_role) = match &existing { None => (true, false, None, false, None), Some(x) => { diff --git a/src/commands/count.rs b/src/commands/count.rs index 2368bca..d354db0 100644 --- a/src/commands/count.rs +++ b/src/commands/count.rs @@ -152,7 +152,7 @@ pub mod servers { let length = line.len() + 1; - // +3 is to account for the closing fense + // +3 is to account for the closing fence if length < (limit + 3) { response.push(line); limit -= length; diff --git a/src/commands/minecraft.rs b/src/commands/minecraft.rs index a4d1b1b..61f4280 100644 --- a/src/commands/minecraft.rs +++ b/src/commands/minecraft.rs @@ -196,7 +196,7 @@ pub(crate) mod server { model::id::GuildId, }; use sqlx::Error; - // this is to managfe the server side of commands related to minecraft + // this is to manage the server side of commands related to minecraft use super::*; use skynet_discord_bot::{ common::minecraft::{update_server, Minecraft}, diff --git a/src/commands/role_adder.rs b/src/commands/role_adder.rs index e60ac83..7f59c08 100644 --- a/src/commands/role_adder.rs +++ b/src/commands/role_adder.rs @@ -147,7 +147,7 @@ pub mod tools { use sqlx::{Pool, Sqlite}; pub async fn on_role_change(db: &Pool, ctx: &Context, new_data: Member) { - // check if the role changed is part of the oens for this server + // check if the role changed is part of the ones for this server if let Ok(role_adders) = sqlx::query_as::<_, RoleAdder>( r#" SELECT * @@ -163,7 +163,7 @@ pub mod tools { let mut roles_remove = vec![]; for role_adder in role_adders { - // if the user has both A dnd B give them C + // if the user has both A and B give them C if new_data.roles.contains(&role_adder.role_a) && new_data.roles.contains(&role_adder.role_b) && !new_data.roles.contains(&role_adder.role_c) { roles_add.push(role_adder.role_c); diff --git a/src/commands/server_icon.rs b/src/commands/server_icon.rs index 0cba1b1..2ab087e 100644 --- a/src/commands/server_icon.rs +++ b/src/commands/server_icon.rs @@ -211,7 +211,7 @@ pub(crate) mod user { let length = line.len() + 1; - // +3 is to account for the closing fense + // +3 is to account for the closing fence if length < (limit + 3) { response.push(line); limit -= length; diff --git a/src/commands/wolves.rs b/src/commands/wolves.rs index dc40ad0..0c8ddfc 100644 --- a/src/commands/wolves.rs +++ b/src/commands/wolves.rs @@ -101,7 +101,7 @@ pub mod link { return "Email already verified".to_string(); } - // generate a auth key + // generate an auth key let auth = random_string(20); match send_mail(&config, &details.email, &auth, &command.user.name) { Ok(_) => match save_to_db(&db, &details, &auth, &command.user.id).await { @@ -210,7 +210,7 @@ pub mod link { .subject("Skynet: Link Discord to Wolves.") .multipart( // This is composed of two parts. - // also helps not trip spam settings (uneven number of url's + // also helps not trip spam settings (uneven number of urls) MultiPart::alternative() .singlepart(SinglePart::builder().header(header::ContentType::TEXT_PLAIN).body(body_text)) .singlepart(SinglePart::builder().header(header::ContentType::TEXT_HTML).body(html.into_string())), @@ -460,7 +460,7 @@ pub mod verify { let config = config_lock.read().await; if let Some(x) = get_server_member_discord(db, &discord.id).await { - // if they are a member of one or more committees, and in teh committee server then give the teh general committee role + // if they are a member of one or more committees, and in teh committee server then give them the general committee role // they will get teh more specific vanity role later if !get_committees_id(db, x.id_wolves).await.is_empty() { let server = config.committee_server; @@ -500,7 +500,7 @@ pub mod unlink { }; let db = db_lock.read().await; - // dosent matter if there is one or not, it will be removed regardless + // doesn't matter if there is one or not, it will be removed regardless delete_link(&db, &command.user.id).await; "Discord link removed".to_string() diff --git a/src/common/minecraft.rs b/src/common/minecraft.rs index 60f7820..77cd754 100644 --- a/src/common/minecraft.rs +++ b/src/common/minecraft.rs @@ -24,7 +24,7 @@ impl<'r> FromRow<'r, SqliteRow> for Minecraft { /** loop through all members of server get a list of folks with mc accounts that are members -and a list that arent members +and a list that aren't members */ pub async fn update_server(server_id: &str, db: &Pool, g_id: &GuildId, config: &Config) { let mut usernames = vec![]; @@ -109,7 +109,7 @@ pub async fn whitelist_wipe(server: &str, token: &str) { }; post(&format!("{url_base}/files/delete"), &bearer, &deletion).await; - // recreate teh file, passing in the type here so the compiler knows what type of vec it is + // recreate the file, passing in the type here so the compiler knows what type of Vec it is post::>(&format!("{url_base}/files/write?file=%2Fwhitelist.json"), &bearer, &vec![]).await; // reload the whitelist diff --git a/src/common/renderer.rs b/src/common/renderer.rs index 97e73a0..f2c50cc 100644 --- a/src/common/renderer.rs +++ b/src/common/renderer.rs @@ -1,12 +1,11 @@ // this code is taken from https://github.com/MCorange99/svg2colored-png/tree/main -// I was unable to figure out how to use usvg myself so younked it from here. +// I was unable to figure out how to use usvg myself so yoinked it from here. use std::{ ffi::OsStr, path::{Path, PathBuf}, }; -// use clap::builder::OsStr; use color_eyre::{eyre::bail, Result}; use usvg_text_layout::TreeTextToPath; @@ -17,7 +16,7 @@ pub struct Args { /// Output folder where the PNG's will be placed pub output: PathBuf, - /// Comma seperated colors that will be used in HEX Eg. 000000,ffffff + /// Comma separated colors that will be used in HEX Eg. 000000,ffffff /// Can be like an object: black:000000,white:ffffff pub colors: String, diff --git a/src/common/server_icon.rs b/src/common/server_icon.rs index d99f265..1a7c2ef 100644 --- a/src/common/server_icon.rs +++ b/src/common/server_icon.rs @@ -281,7 +281,7 @@ pub mod update_icon { // check if exists if !path_new.exists() { - // convert if it hasnt been converted already + // convert if it hasn't been converted already match r.render(&path_local, &args) { Ok(_) => {} Err(_e) => { diff --git a/src/common/set_roles.rs b/src/common/set_roles.rs index 6e71992..9115986 100644 --- a/src/common/set_roles.rs +++ b/src/common/set_roles.rs @@ -48,7 +48,7 @@ pub mod normal { if let Ok(x) = server.members(ctx, None, None).await { for member in x { - // members_changed acts as an override to only deal with teh users in it + // members_changed acts as an override to only deal with the users in it if !members_changed.is_empty() && !members_changed.contains(&member.user.id) { continue; } @@ -84,7 +84,7 @@ pub mod normal { if member.roles.contains(role_current) { roles_set.current_rem += 1; - // if theya re not a current member and have the role then remove it + // if they're not a current member and have the role then remove it if let Err(e) = member.remove_role(ctx, role_current).await { println!("{e:?}"); } @@ -196,7 +196,7 @@ pub mod committee { } /** - This function can take a vec of members (or just one) and gives tehm the appropiate roles on teh committee server + This function can take a Vec of members (or just one) and gives them the appropriate roles on teh committee server */ pub async fn update_committees(db: &Pool, ctx: &Context, config: &Config, members: &mut Vec) { let server = config.committee_server; @@ -228,11 +228,11 @@ pub mod committee { let mut channels = server.channels(&ctx).await.unwrap_or_default(); - // a map of users and the roles they are goign to be getting + // a map of users and the roles they are going to be getting let mut users_roles = HashMap::new(); let mut re_order = false; - // we need to create roles and channels if tehy dont already exist + // we need to create roles and channels if they don't already exist let mut category_index = 0; let mut i = 0; loop { @@ -367,7 +367,7 @@ pub mod committee { let has_committee_role = roles_current_id.contains(&committee_member); if on_committee && !has_committee_role { - // if there are committee roles then give the general purporse role + // if there are committee roles then give the general purpose role roles_add.push(committee_member); } if !on_committee && has_committee_role { diff --git a/src/lib.rs b/src/lib.rs index 75c9d78..1a6afb1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -36,7 +36,7 @@ pub struct Config { pub committee_role: RoleId, pub committee_category: Vec, - // items pertaining to compsoc only + // items pertaining to CompSoc only pub compsoc_server: GuildId, } impl TypeMapKey for Config { diff --git a/src/main.rs b/src/main.rs index bbf389f..aeebac7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,7 +27,6 @@ use sqlx::{Pool, Sqlite}; use std::sync::Arc; use tokio::sync::RwLock; -// Need To Define The Stuct (Committed To Bump The Bot) struct Handler; #[async_trait] @@ -117,7 +116,7 @@ Sign up on [UL Wolves]({}) and go to https://discord.com/channels/{}/{} and use let db = db_lock.read().await; - // check if the role changed is part of the oens for this server + // check if the role changed is part of the ones for this server if let Some(x) = new_data { on_role_change(&db, &ctx, x).await; } @@ -159,13 +158,13 @@ Sign up on [UL Wolves]({}) and go to https://discord.com/channels/{}/{} and use } } - // compsoc Server + // CompSoc Server match config .compsoc_server .set_commands( &ctx.http, vec![ - // commands just for the compsoc server + // commands just for the CompSoc server commands::count::servers::register(), commands::server_icon::user::register(), ], @@ -182,7 +181,7 @@ Sign up on [UL Wolves]({}) and go to https://discord.com/channels/{}/{} and use async fn interaction_create(&self, ctx: Context, interaction: Interaction) { if let Interaction::Command(command) = interaction { let _ = command.defer_ephemeral(&ctx.http).await; - //println!("Received command interaction: {:#?}", command); + // println!("Received command interaction: {:#?}", command); let content = match command.data.name.as_str() { // user commands From d70a037057c6209b2340b22a749359a16f7d164a Mon Sep 17 00:00:00 2001 From: Roman Moisieiev Date: Thu, 11 Sep 2025 12:36:48 +0100 Subject: [PATCH 7/8] Blame: ignore Fix Typos commit --- .git-blame-ignore-revs | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .git-blame-ignore-revs diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs new file mode 100644 index 0000000..a338a00 --- /dev/null +++ b/.git-blame-ignore-revs @@ -0,0 +1,2 @@ +# Fix typos +7e90f451965b0edbd331765ad295a02f31d2bf24 From 062f826d28b4d385423656b10cca84b3644c8e60 Mon Sep 17 00:00:00 2001 From: Roman Moisieiev Date: Thu, 11 Sep 2025 12:54:54 +0100 Subject: [PATCH 8/8] Remove RwLock for database --- src/bin/cleanup_committee.rs | 6 ++---- src/bin/update_committee.rs | 2 +- src/bin/update_data.rs | 2 +- src/bin/update_server-icon.rs | 5 ++--- src/bin/update_users.rs | 15 +++++++++------ src/commands/add_server.rs | 3 +-- src/commands/count.rs | 6 ++---- src/commands/minecraft.rs | 16 ++++++---------- src/commands/role_adder.rs | 3 +-- src/commands/server_icon.rs | 9 +++------ src/commands/wolves.rs | 9 +++------ src/common/database.rs | 3 +-- src/common/set_roles.rs | 8 ++------ src/common/wolves.rs | 6 ++---- src/main.rs | 10 +++------- 15 files changed, 39 insertions(+), 64 deletions(-) diff --git a/src/bin/cleanup_committee.rs b/src/bin/cleanup_committee.rs index 9a01f4c..141af89 100644 --- a/src/bin/cleanup_committee.rs +++ b/src/bin/cleanup_committee.rs @@ -41,7 +41,7 @@ async fn main() { let mut data = client.data.write().await; data.insert::(Arc::new(RwLock::new(config))); - data.insert::(Arc::new(RwLock::new(db))); + data.insert::(Arc::new(db)); } if let Err(why) = client.start().await { @@ -69,13 +69,11 @@ impl EventHandler for Handler { async fn guild_members_chunk(&self, ctx: Context, chunk: GuildMembersChunkEvent) { if (chunk.chunk_index + 1) == chunk.chunk_count { println!("Cache built successfully!"); - let db_lock = { + let db = { let data_read = ctx.data.read().await; data_read.get::().expect("Expected Config in TypeMap.").clone() }; - let db = db_lock.read().await; - let config_lock = { let data_read = ctx.data.read().await; data_read.get::().expect("Expected Config in TypeMap.").clone() diff --git a/src/bin/update_committee.rs b/src/bin/update_committee.rs index b8cc6c2..d90cbac 100644 --- a/src/bin/update_committee.rs +++ b/src/bin/update_committee.rs @@ -36,7 +36,7 @@ async fn main() { let mut data = client.data.write().await; data.insert::(Arc::new(RwLock::new(config))); - data.insert::(Arc::new(RwLock::new(db))); + data.insert::(Arc::new(db)); } if let Err(why) = client.start().await { diff --git a/src/bin/update_data.rs b/src/bin/update_data.rs index 8f73ce9..fe4138f 100644 --- a/src/bin/update_data.rs +++ b/src/bin/update_data.rs @@ -38,7 +38,7 @@ async fn main() { let mut data = client.data.write().await; data.insert::(Arc::new(RwLock::new(config))); - data.insert::(Arc::new(RwLock::new(db))); + data.insert::(Arc::new(db)); } if let Err(why) = client.start().await { diff --git a/src/bin/update_server-icon.rs b/src/bin/update_server-icon.rs index 56957da..c4f9eca 100644 --- a/src/bin/update_server-icon.rs +++ b/src/bin/update_server-icon.rs @@ -35,7 +35,7 @@ async fn main() { let mut data = client.data.write().await; data.insert::(Arc::new(RwLock::new(config))); - data.insert::(Arc::new(RwLock::new(db))); + data.insert::(Arc::new(db)); } if let Err(why) = client.start().await { @@ -50,11 +50,10 @@ impl EventHandler for Handler { let ctx = Arc::new(ctx); println!("{} is connected!", ready.user.name); - let db_lock = { + let db = { let data_read = ctx.data.read().await; data_read.get::().expect("Expected Config in TypeMap.").clone() }; - let db = db_lock.read().await; let config_lock = { let data_read = ctx.data.read().await; diff --git a/src/bin/update_users.rs b/src/bin/update_users.rs index 0cb11ac..3150bcf 100644 --- a/src/bin/update_users.rs +++ b/src/bin/update_users.rs @@ -12,8 +12,13 @@ use skynet_discord_bot::{ }, get_config, Config, }; -use std::sync::atomic::{AtomicUsize, Ordering}; -use std::{process, sync::Arc}; +use std::{ + process, + sync::{ + atomic::{AtomicUsize, Ordering}, + Arc, + }, +}; use tokio::sync::RwLock; #[tokio::main] @@ -40,7 +45,7 @@ async fn main() { let mut data = client.data.write().await; data.insert::(Arc::new(RwLock::new(config))); - data.insert::(Arc::new(RwLock::new(db))); + data.insert::(Arc::new(db)); } if let Err(why) = client.start().await { @@ -79,13 +84,11 @@ impl EventHandler for Handler { } async fn check_bulk(ctx: &Context) { - let db_lock = { + let db = { let data_read = ctx.data.read().await; data_read.get::().expect("Expected Config in TypeMap.").clone() }; - let db = db_lock.read().await; - for server_config in get_server_config_bulk(&db).await { normal::update_server(ctx, &server_config, &[], &[]).await; } diff --git a/src/commands/add_server.rs b/src/commands/add_server.rs index 4511c1b..eaf0971 100644 --- a/src/commands/add_server.rs +++ b/src/commands/add_server.rs @@ -56,11 +56,10 @@ pub async fn run(command: &CommandInteraction, ctx: &Context) -> String { return "Please provide a valid channel for ``Bot Channel``".to_string(); }; - let db_lock = { + let db = { let data_read = ctx.data.read().await; data_read.get::().expect("Expected Databse in TypeMap.").clone() }; - let db = db_lock.read().await; let server_data = Servers { server: command.guild_id.unwrap_or_default(), diff --git a/src/commands/count.rs b/src/commands/count.rs index d354db0..41b9d81 100644 --- a/src/commands/count.rs +++ b/src/commands/count.rs @@ -27,11 +27,10 @@ pub mod committee { false }; - let db_lock = { + let db = { 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 @@ -95,11 +94,10 @@ pub mod servers { use std::collections::HashMap; pub async fn run(_command: &CommandInteraction, ctx: &Context) -> String { - let db_lock = { + let db = { 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(); if let Some(x) = get_committees(&db).await { diff --git a/src/commands/minecraft.rs b/src/commands/minecraft.rs index 61f4280..5098f11 100644 --- a/src/commands/minecraft.rs +++ b/src/commands/minecraft.rs @@ -23,11 +23,10 @@ pub(crate) mod user { use sqlx::Error; pub async fn run(command: &CommandInteraction, ctx: &Context) -> String { - let db_lock = { + let db = { let data_read = ctx.data.read().await; data_read.get::().expect("Expected Databse in TypeMap.").clone() }; - let db = db_lock.read().await; let config_lock = { let data_read = ctx.data.read().await; @@ -229,11 +228,10 @@ pub(crate) mod server { return String::from("Expected Server ID"); }; - let db_lock = { + let db = { let data_read = ctx.data.read().await; data_read.get::().expect("Expected Databse in TypeMap.").clone() }; - let db = db_lock.read().await; match add_server(&db, &g_id, &server_minecraft).await { Ok(_) => {} @@ -290,11 +288,10 @@ pub(crate) mod server { Some(x) => x, }; - let db_lock = { + let db = { let data_read = ctx.data.read().await; data_read.get::().expect("Expected Databse in TypeMap.").clone() }; - let db = db_lock.read().await; let servers = get_minecraft_config_server(&db, g_id).await; @@ -366,11 +363,10 @@ pub(crate) mod server { return String::from("Expected Server ID"); }; - let db_lock = { - let data_read = ctx.data.read().await; - data_read.get::().expect("Expected Databse in TypeMap.").clone() + let db = { + let data = ctx.data.read().await; + data.get::().expect("Expected Databse in TypeMap.").clone() }; - let db = db_lock.read().await; match server_remove(&db, &g_id, &server_minecraft).await { Ok(_) => {} diff --git a/src/commands/role_adder.rs b/src/commands/role_adder.rs index 7f59c08..0573b37 100644 --- a/src/commands/role_adder.rs +++ b/src/commands/role_adder.rs @@ -62,11 +62,10 @@ pub mod edit { false }; - let db_lock = { + let db = { let data_read = ctx.data.read().await; data_read.get::().expect("Expected Databse in TypeMap.").clone() }; - let db = db_lock.read().await; let server = command.guild_id.unwrap_or_default(); let server_data = RoleAdder { diff --git a/src/commands/server_icon.rs b/src/commands/server_icon.rs index 2ab087e..d4a78d5 100644 --- a/src/commands/server_icon.rs +++ b/src/commands/server_icon.rs @@ -18,11 +18,10 @@ pub(crate) mod admin { use super::*; pub async fn run(_command: &CommandInteraction, ctx: &Context) -> String { - let db_lock = { + let db = { let data_read = ctx.data.read().await; data_read.get::().expect("Expected Databse in TypeMap.").clone() }; - let db = db_lock.read().await; let config_lock = { let data_read = ctx.data.read().await; @@ -69,11 +68,10 @@ pub(crate) mod user { use sqlx::{Pool, Sqlite}; pub async fn run(command: &CommandInteraction, ctx: &Context) -> String { - let db_lock = { + let db = { let data_read = ctx.data.read().await; data_read.get::().expect("Expected Databse in TypeMap.").clone() }; - let db = db_lock.read().await; let config_toml = get_config_icons::minimal(); @@ -145,11 +143,10 @@ pub(crate) mod user { use sqlx::{Pool, Sqlite}; pub async fn run(_command: &CommandInteraction, ctx: &Context) -> String { - let db_lock = { + let db = { let data_read = ctx.data.read().await; data_read.get::().expect("Expected Databse in TypeMap.").clone() }; - let db = db_lock.read().await; let config_toml = get_config_icons::minimal(); diff --git a/src/commands/wolves.rs b/src/commands/wolves.rs index 0c8ddfc..843ca70 100644 --- a/src/commands/wolves.rs +++ b/src/commands/wolves.rs @@ -21,11 +21,10 @@ pub mod link { use serenity::all::{CommandDataOption, CommandDataOptionValue, CommandInteraction}; pub async fn run(command: &CommandInteraction, ctx: &Context) -> String { - let db_lock = { + let db = { let data_read = ctx.data.read().await; data_read.get::().expect("Expected Databse in TypeMap.").clone() }; - let db = db_lock.read().await; let config_lock = { let data_read = ctx.data.read().await; @@ -314,11 +313,10 @@ pub mod verify { use sqlx::Error; pub async fn run(command: &CommandInteraction, ctx: &Context) -> String { - let db_lock = { + let db = { let data_read = ctx.data.read().await; data_read.get::().expect("Expected Database in TypeMap.").clone() }; - let db = db_lock.read().await; // check if user has used /link_wolves let details = if let Some(x) = get_verify_from_db(&db, &command.user.id).await { @@ -494,11 +492,10 @@ pub mod unlink { use sqlx::{Pool, Sqlite}; pub async fn run(command: &CommandInteraction, ctx: &Context) -> String { - let db_lock = { + let db = { let data_read = ctx.data.read().await; data_read.get::().expect("Expected Databse in TypeMap.").clone() }; - let db = db_lock.read().await; // doesn't matter if there is one or not, it will be removed regardless delete_link(&db, &command.user.id).await; diff --git a/src/common/database.rs b/src/common/database.rs index 0c18668..2eaf5df 100644 --- a/src/common/database.rs +++ b/src/common/database.rs @@ -12,11 +12,10 @@ use sqlx::{ Error, FromRow, Pool, Row, Sqlite, }; use std::{str::FromStr, sync::Arc}; -use tokio::sync::RwLock; pub struct DataBase; impl TypeMapKey for DataBase { - type Value = Arc>>; + type Value = Arc>; } #[derive(Debug, Clone, Deserialize, Serialize)] diff --git a/src/common/set_roles.rs b/src/common/set_roles.rs index 9115986..e11c637 100644 --- a/src/common/set_roles.rs +++ b/src/common/set_roles.rs @@ -17,13 +17,11 @@ pub mod normal { } pub async fn update_server(ctx: &Context, server: &Servers, remove_roles: &[Option], members_changed: &[UserId]) { - let db_lock = { + let db = { let data_read = ctx.data.read().await; data_read.get::().expect("Expected Database in TypeMap.").clone() }; - let db = db_lock.read().await; - let Servers { server, role_past, @@ -174,13 +172,11 @@ pub mod committee { use std::collections::HashMap; pub async fn check_committee(ctx: &Context) { - let db_lock = { + let db = { let data_read = ctx.data.read().await; data_read.get::().expect("Expected Config in TypeMap.").clone() }; - let db = db_lock.read().await; - let config_lock = { let data_read = ctx.data.read().await; data_read.get::().expect("Expected Config in TypeMap.").clone() diff --git a/src/common/wolves.rs b/src/common/wolves.rs index 17305f6..3744558 100644 --- a/src/common/wolves.rs +++ b/src/common/wolves.rs @@ -69,11 +69,10 @@ pub mod cns { } pub async fn get_wolves(ctx: &Context) { - let db_lock = { + let db = { let data_read = ctx.data.read().await; data_read.get::().expect("Expected Database in TypeMap.").clone() }; - let db = db_lock.read().await; let config_lock = { let data_read = ctx.data.read().await; @@ -224,11 +223,10 @@ pub mod committees { } pub async fn get_cns(ctx: &Context) { - let db_lock = { + let db = { let data_read = ctx.data.read().await; data_read.get::().expect("Expected Database in TypeMap.").clone() }; - let db = db_lock.read().await; let config_lock = { let data_read = ctx.data.read().await; diff --git a/src/main.rs b/src/main.rs index aeebac7..8dff675 100644 --- a/src/main.rs +++ b/src/main.rs @@ -41,13 +41,11 @@ impl EventHandler for Handler { // handles previously linked accounts joining the server async fn guild_member_addition(&self, ctx: Context, new_member: Member) { - let db_lock = { + let db = { let data_read = ctx.data.read().await; data_read.get::().expect("Expected Config in TypeMap.").clone() }; - let db = db_lock.read().await; - let config_lock = { let data_read = ctx.data.read().await; data_read.get::().expect("Expected Config in TypeMap.").clone() @@ -109,13 +107,11 @@ Sign up on [UL Wolves]({}) and go to https://discord.com/channels/{}/{} and use // handles role updates async fn guild_member_update(&self, ctx: Context, _old_data: Option, new_data: Option, _: GuildMemberUpdateEvent) { // get config/db - let db_lock = { + let db = { let data_read = ctx.data.read().await; data_read.get::().expect("Expected Config in TypeMap.").clone() }; - let db = db_lock.read().await; - // check if the role changed is part of the ones for this server if let Some(x) = new_data { on_role_change(&db, &ctx, x).await; @@ -304,7 +300,7 @@ async fn main() { let mut data = client.data.write().await; data.insert::(Arc::new(RwLock::new(config))); - data.insert::(Arc::new(RwLock::new(db))); + data.insert::(Arc::new(db)); } // Finally, start a single shard, and start listening to events.