fix: better handling of multiple cns sharing minecraft servers

This commit is contained in:
silver 2024-05-06 02:09:02 +01:00
parent 3d925fcfff
commit d0b63190b3
3 changed files with 64 additions and 51 deletions

View file

@ -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] #[tokio::main]
async fn main() { async fn main() {
@ -8,7 +9,16 @@ async fn main() {
Err(_) => return, Err(_) => return,
}; };
for server_config in get_server_config_bulk(&db).await { let servers = get_server_config_bulk(&db).await;
update_server(server_config.server_minecraft, &db, &server_config.server, &config).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;
} }
} }

View file

@ -14,7 +14,7 @@ pub(crate) mod user {
use super::*; use super::*;
use crate::commands::link_email::link::get_server_member_discord; use crate::commands::link_email::link::get_server_member_discord;
use serenity::model::id::UserId; use serenity::model::id::UserId;
use skynet_discord_bot::{update_whitelist, Config, Wolves}; use skynet_discord_bot::{whitelist_update, Config, Wolves};
use sqlx::Error; use sqlx::Error;
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand { pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
@ -73,7 +73,7 @@ pub(crate) mod user {
for server in servers { for server in servers {
if let Some(server_minecraft) = server.server_minecraft { if let Some(server_minecraft) = server.server_minecraft {
// activate the user on all linked servers // 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; 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() "Added/Updated minecraft_server info".to_string()
} }

View file

@ -686,7 +686,7 @@ loop through all members of server
get a list of folks with mc accounts that are members get a list of folks with mc accounts that are members
and a list that arent members and a list that arent members
*/ */
pub async fn update_server(server_minecraft: Option<String>, db: &Pool<Sqlite>, g_id: &GuildId, config: &Config) { pub async fn update_server(server_minecraft: &Option<String>, db: &Pool<Sqlite>, g_id: &GuildId, config: &Config) {
if let Some(server_id) = server_minecraft { if let Some(server_id) = server_minecraft {
let mut usernames = vec![]; let mut usernames = vec![];
for member in get_server_member_bulk(db, g_id).await { for member in get_server_member_bulk(db, g_id).await {
@ -695,59 +695,41 @@ pub async fn update_server(server_minecraft: Option<String>, db: &Pool<Sqlite>,
} }
} }
if !usernames.is_empty() { 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<String>, server: &str, token: &str, wipe_reset: bool) { async fn post<T: Serialize>(url: &str, bearer: &str, data: &T) {
let url_base = format!("http://panel.games.skynet.ie/api/client/servers/{server}"); match surf::post(url)
let bearer = format!("Bearer {token}"); .header("Authorization", bearer)
.header("Content-Type", "application/json")
async fn post<T: Serialize>(url: &str, bearer: &str, data: &T) { .header("Accept", "Application/vnd.pterodactyl.v1+json")
match surf::post(url) .body_json(&data)
.header("Authorization", bearer) {
.header("Content-Type", "application/json") Ok(req) => {
.header("Accept", "Application/vnd.pterodactyl.v1+json") req.await.ok();
.body_json(&data) }
{ Err(e) => {
Ok(req) => { dbg!(e);
req.await.ok();
}
Err(e) => {
dbg!(e);
}
} }
} }
}
#[derive(Deserialize, Serialize, Debug)] #[derive(Deserialize, Serialize, Debug)]
struct BodyCommand { struct BodyCommand {
command: String, command: String,
} }
#[derive(Deserialize, Serialize, Debug)] #[derive(Deserialize, Serialize, Debug)]
struct BodyDelete { struct BodyDelete {
root: String, root: String,
files: Vec<String>, files: Vec<String>,
} }
if wipe_reset { pub async fn whitelist_update(add: &Vec<String>, server: &str, token: &str) {
// delete whitelist let url_base = format!("http://panel.games.skynet.ie/api/client/servers/{server}");
let deletion = BodyDelete { let bearer = format!("Bearer {token}");
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::<Vec<&str>>(&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;
}
for name in add { for name in add {
let data = BodyCommand { let data = BodyCommand {
@ -756,3 +738,24 @@ pub async fn update_whitelist(add: &Vec<String>, server: &str, token: &str, wipe
post(&format!("{url_base}/command"), &bearer, &data).await; 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::<Vec<&str>>(&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;
}