From d0b63190b32d7455d35bb7f4144e4c441f8e72b8 Mon Sep 17 00:00:00 2001 From: Brendan Golden Date: Mon, 6 May 2024 02:09:02 +0100 Subject: [PATCH] fix: better handling of multiple cns sharing minecraft servers --- src/bin/update_minecraft.rs | 16 ++++-- src/commands/add_minecraft.rs | 6 +-- src/lib.rs | 93 ++++++++++++++++++----------------- 3 files changed, 64 insertions(+), 51 deletions(-) diff --git a/src/bin/update_minecraft.rs b/src/bin/update_minecraft.rs index 37aaee9..b63d8dd 100644 --- a/src/bin/update_minecraft.rs +++ b/src/bin/update_minecraft.rs @@ -1,4 +1,5 @@ -use skynet_discord_bot::{db_init, get_config, get_server_config_bulk, update_server}; +use skynet_discord_bot::set_roles::get_server_member_bulk; +use skynet_discord_bot::{db_init, get_config, get_server_config_bulk, update_server, whitelist_update, whitelist_wipe}; #[tokio::main] async fn main() { @@ -8,7 +9,16 @@ async fn main() { Err(_) => return, }; - for server_config in get_server_config_bulk(&db).await { - update_server(server_config.server_minecraft, &db, &server_config.server, &config).await; + let servers = get_server_config_bulk(&db).await; + + // wipe whitelist first + for server_config in &servers { + if let Some(server_id) = &server_config.server_minecraft { + whitelist_wipe(server_id, &config.discord_minecraft).await; + } + } + + for server_config in &servers { + update_server(&server_config.server_minecraft, &db, &server_config.server, &config).await; } } diff --git a/src/commands/add_minecraft.rs b/src/commands/add_minecraft.rs index bd84f63..d0115b5 100644 --- a/src/commands/add_minecraft.rs +++ b/src/commands/add_minecraft.rs @@ -14,7 +14,7 @@ pub(crate) mod user { use super::*; use crate::commands::link_email::link::get_server_member_discord; use serenity::model::id::UserId; - use skynet_discord_bot::{update_whitelist, Config, Wolves}; + use skynet_discord_bot::{whitelist_update, Config, Wolves}; use sqlx::Error; pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand { @@ -73,7 +73,7 @@ pub(crate) mod user { for server in servers { if let Some(server_minecraft) = server.server_minecraft { // activate the user on all linked servers - update_whitelist(&vec![username.to_string()], &server_minecraft, &config.discord_minecraft, false).await; + whitelist_update(&vec![username.to_string()], &server_minecraft, &config.discord_minecraft).await; } } } @@ -188,7 +188,7 @@ pub(crate) mod server { }; let config = config_lock.read().await; - update_server(server_minecraft, &db, &g_id, &config).await; + update_server(&server_minecraft, &db, &g_id, &config).await; "Added/Updated minecraft_server info".to_string() } diff --git a/src/lib.rs b/src/lib.rs index 003ac34..c08f453 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -686,7 +686,7 @@ loop through all members of server get a list of folks with mc accounts that are members and a list that arent members */ -pub async fn update_server(server_minecraft: Option, db: &Pool, g_id: &GuildId, config: &Config) { +pub async fn update_server(server_minecraft: &Option, db: &Pool, g_id: &GuildId, config: &Config) { if let Some(server_id) = server_minecraft { let mut usernames = vec![]; for member in get_server_member_bulk(db, g_id).await { @@ -695,59 +695,41 @@ pub async fn update_server(server_minecraft: Option, db: &Pool, } } if !usernames.is_empty() { - update_whitelist(&usernames, &server_id, &config.discord_minecraft, true).await; + whitelist_update(&usernames, server_id, &config.discord_minecraft).await; } } } -pub async fn update_whitelist(add: &Vec, server: &str, token: &str, wipe_reset: bool) { - let url_base = format!("http://panel.games.skynet.ie/api/client/servers/{server}"); - let bearer = format!("Bearer {token}"); - - async fn post(url: &str, bearer: &str, data: &T) { - match surf::post(url) - .header("Authorization", bearer) - .header("Content-Type", "application/json") - .header("Accept", "Application/vnd.pterodactyl.v1+json") - .body_json(&data) - { - Ok(req) => { - req.await.ok(); - } - Err(e) => { - dbg!(e); - } +async fn post(url: &str, bearer: &str, data: &T) { + match surf::post(url) + .header("Authorization", bearer) + .header("Content-Type", "application/json") + .header("Accept", "Application/vnd.pterodactyl.v1+json") + .body_json(&data) + { + Ok(req) => { + req.await.ok(); + } + Err(e) => { + dbg!(e); } } +} - #[derive(Deserialize, Serialize, Debug)] - struct BodyCommand { - command: String, - } +#[derive(Deserialize, Serialize, Debug)] +struct BodyCommand { + command: String, +} - #[derive(Deserialize, Serialize, Debug)] - struct BodyDelete { - root: String, - files: Vec, - } +#[derive(Deserialize, Serialize, Debug)] +struct BodyDelete { + root: String, + files: Vec, +} - if wipe_reset { - // delete whitelist - let deletion = BodyDelete { - root: "/".to_string(), - files: vec!["whitelist.json".to_string()], - }; - 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 - post::>(&format!("{url_base}/files/write?file=%2Fwhitelist.json"), &bearer, &vec![]).await; - - // reload the whitelist - let data = BodyCommand { - command: "whitelist reload".to_string(), - }; - post(&format!("{url_base}/command"), &bearer, &data).await; - } +pub async fn whitelist_update(add: &Vec, server: &str, token: &str) { + let url_base = format!("http://panel.games.skynet.ie/api/client/servers/{server}"); + let bearer = format!("Bearer {token}"); for name in add { let data = BodyCommand { @@ -756,3 +738,24 @@ pub async fn update_whitelist(add: &Vec, server: &str, token: &str, wipe post(&format!("{url_base}/command"), &bearer, &data).await; } } + +pub async fn whitelist_wipe(server: &str, token: &str) { + let url_base = format!("http://panel.games.skynet.ie/api/client/servers/{server}"); + let bearer = format!("Bearer {token}"); + + // delete whitelist + let deletion = BodyDelete { + root: "/".to_string(), + files: vec!["whitelist.json".to_string()], + }; + 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 + post::>(&format!("{url_base}/files/write?file=%2Fwhitelist.json"), &bearer, &vec![]).await; + + // reload the whitelist + let data = BodyCommand { + command: "whitelist reload".to_string(), + }; + post(&format!("{url_base}/command"), &bearer, &data).await; +}