fix: better handling of multiple cns sharing minecraft servers
This commit is contained in:
parent
3d925fcfff
commit
d0b63190b3
3 changed files with 64 additions and 51 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
|
93
src/lib.rs
93
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<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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue