forked from Skynet/discord-bot
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()
|
||||
}
|
||||
|
|
33
src/lib.rs
33
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,15 +695,11 @@ 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)
|
||||
|
@ -731,7 +727,22 @@ pub async fn update_whitelist(add: &Vec<String>, server: &str, token: &str, wipe
|
|||
files: Vec<String>,
|
||||
}
|
||||
|
||||
if wipe_reset {
|
||||
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 {
|
||||
command: format!("whitelist add {name}"),
|
||||
};
|
||||
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(),
|
||||
|
@ -748,11 +759,3 @@ pub async fn update_whitelist(add: &Vec<String>, server: &str, token: &str, wipe
|
|||
};
|
||||
post(&format!("{url_base}/command"), &bearer, &data).await;
|
||||
}
|
||||
|
||||
for name in add {
|
||||
let data = BodyCommand {
|
||||
command: format!("whitelist add {name}"),
|
||||
};
|
||||
post(&format!("{url_base}/command"), &bearer, &data).await;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue