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

@ -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<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 {
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<String>, db: &Pool<Sqlite>,
}
}
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) {
let url_base = format!("http://panel.games.skynet.ie/api/client/servers/{server}");
let bearer = format!("Bearer {token}");
async fn post<T: Serialize>(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<T: Serialize>(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<String>,
}
#[derive(Deserialize, Serialize, Debug)]
struct BodyDelete {
root: String,
files: Vec<String>,
}
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::<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;
}
pub async fn whitelist_update(add: &Vec<String>, 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<String>, 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::<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;
}