forked from Skynet/discord-bot
feat: added command to automatically update the mc whitelist
This commit is contained in:
parent
f417b9993a
commit
2c28f3edcc
5 changed files with 88 additions and 84 deletions
|
@ -12,10 +12,9 @@ use sqlx::{Pool, Sqlite};
|
|||
|
||||
pub(crate) mod user {
|
||||
use super::*;
|
||||
use crate::commands::add_minecraft::server::update_whitelist;
|
||||
use crate::commands::link_email::link::get_server_member_discord;
|
||||
use serenity::model::id::UserId;
|
||||
use skynet_discord_bot::{Config, Wolves};
|
||||
use skynet_discord_bot::{update_whitelist, Config, Wolves};
|
||||
use sqlx::Error;
|
||||
|
||||
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
|
||||
|
@ -117,13 +116,10 @@ pub(crate) mod user {
|
|||
}
|
||||
|
||||
pub(crate) mod server {
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serenity::model::id::GuildId;
|
||||
use sqlx::Error;
|
||||
// this is to managfe the server side of commands related to minecraft
|
||||
use super::*;
|
||||
use skynet_discord_bot::set_roles::get_server_member_bulk;
|
||||
use skynet_discord_bot::{is_admin, Config};
|
||||
use skynet_discord_bot::{is_admin, update_server, Config};
|
||||
|
||||
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
|
||||
command
|
||||
|
@ -210,77 +206,4 @@ pub(crate) mod server {
|
|||
.fetch_optional(db)
|
||||
.await
|
||||
}
|
||||
|
||||
/**
|
||||
loop through all members of server
|
||||
get a list of folks with mc accounts that are members
|
||||
and a list that arent members
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@ use serenity::{
|
|||
};
|
||||
use skynet_discord_bot::{get_now_iso, random_string, Config, DataBase, Wolves, WolvesVerify};
|
||||
use sqlx::{Pool, Sqlite};
|
||||
pub(crate) mod link {
|
||||
pub mod link {
|
||||
use super::*;
|
||||
|
||||
pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> String {
|
||||
|
@ -234,7 +234,7 @@ pub(crate) mod link {
|
|||
}
|
||||
}
|
||||
|
||||
pub(crate) mod verify {
|
||||
pub mod verify {
|
||||
use super::*;
|
||||
use crate::commands::link_email::link::{db_pending_clear_expired, get_verify_from_db};
|
||||
use serenity::model::user::User;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue