feat: added command to automatically update the mc whitelist

This commit is contained in:
silver 2024-03-03 14:40:37 +00:00
parent f417b9993a
commit 2c28f3edcc
5 changed files with 88 additions and 84 deletions

View file

@ -8,6 +8,7 @@ use serenity::{
prelude::TypeMapKey,
};
use crate::set_roles::get_server_member_bulk;
use chrono::{Datelike, SecondsFormat, Utc};
use rand::{distributions::Alphanumeric, thread_rng, Rng};
use serenity::client::Context;
@ -679,3 +680,76 @@ pub async fn is_admin(command: &ApplicationCommandInteraction, ctx: &Context) ->
None
}
}
/**
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) {
if let Some(server_id) = server_minecraft {
let mut usernames = vec![];
for member in get_server_member_bulk(db, g_id).await {
if let Some(x) = member.minecraft {
usernames.push(x);
}
}
if !usernames.is_empty() {
update_whitelist(&usernames, &server_id, &config.discord_minecraft, true).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);
}
}
}
#[derive(Deserialize, Serialize, Debug)]
struct BodyCommand {
command: 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;
// reload the whitelist
let data = BodyCommand {
command: "whitelist reload".to_string(),
};
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;
}
}