From c2a6407ef0564f2325bb0e0c0c9a2efeb3588a63 Mon Sep 17 00:00:00 2001 From: Brendan Golden Date: Mon, 3 Jun 2024 03:04:12 +0100 Subject: [PATCH] feat: add a command to list minecraft servers --- src/commands/minecraft.rs | 49 +++++++++++++++++++++++++++++++++++++++ src/lib.rs | 39 +++++++++++++++++++++++++++++-- src/main.rs | 2 ++ 3 files changed, 88 insertions(+), 2 deletions(-) diff --git a/src/commands/minecraft.rs b/src/commands/minecraft.rs index b4d08f1..caf4817 100644 --- a/src/commands/minecraft.rs +++ b/src/commands/minecraft.rs @@ -207,4 +207,53 @@ pub(crate) mod server { .await } } + + pub(crate) mod list { + use serenity::builder::CreateApplicationCommand; + use serenity::client::Context; + use serenity::model::prelude::application_command::ApplicationCommandInteraction; + use skynet_discord_bot::{Config, DataBase, get_minecraft_config_server, is_admin, server_information}; + + pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand { + command + .name("minecraft_list") + .description("List your minecraft servers") + } + + pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> String { + if let Some(msg) = is_admin(command, ctx).await { + return msg; + } + let g_id = match command.guild_id { + None => return "Not in a server".to_string(), + Some(x) => x, + }; + + let db_lock = { + let data_read = ctx.data.read().await; + data_read.get::().expect("Expected Databse in TypeMap.").clone() + }; + let db = db_lock.read().await; + + let servers = get_minecraft_config_server(&db, g_id).await; + + if servers.is_empty() { + return "No minecraft servers, use /minecraft_add to add one".to_string(); + } + + let config_lock = { + let data_read = ctx.data.read().await; + data_read.get::().expect("Expected Config in TypeMap.").clone() + }; + let config = config_lock.read().await; + + let mut result = "|ID|Online|Name|Description|\n|:---|:---|:---|:---|".to_string(); + for server in get_minecraft_config_server(&db, g_id).await { + if let Some(x) = server_information(&server.minecraft, &config.discord_minecraft).await { + write!(result, "\n|{}|{}|{}|{}|", &x.attributes.identifier, !x.attributes.is_suspended, &x.attributes.name, &x.attributes.description).unwrap(); + } + } + result + } + } } diff --git a/src/lib.rs b/src/lib.rs index f2857b7..c5de643 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -725,6 +725,35 @@ async fn post(url: &str, bearer: &str, data: &T) { } } +#[derive(Deserialize, Serialize, Debug)] +pub struct ServerDetailsResSub { + pub identifier: String, + pub name: String, + pub description: String, + pub is_suspended: String, +} +#[derive(Deserialize, Serialize, Debug)] +pub struct ServerDetailsRes { + pub attributes: ServerDetailsResSub, +} + +async fn get(url: &str, bearer: &str) -> Option { + match surf::get(url) + .header("Authorization", bearer) + .header("Content-Type", "application/json") + .header("Accept", "Application/vnd.pterodactyl.v1+json").recv_json().await + { + Ok(res) => { + Some(res) + } + Err(e) => { + dbg!(e); + + None + } + } +} + #[derive(Deserialize, Serialize, Debug)] struct BodyCommand { command: String, @@ -769,6 +798,12 @@ pub async fn whitelist_wipe(server: &str, token: &str) { post(&format!("{url_base}/command"), &bearer, &data).await; } +pub async fn server_information(server: &str, token: &str) -> Option { + let url_base = format!("http://panel.games.skynet.ie/api/client/servers/{server}"); + let bearer = format!("Bearer {token}"); + get::(&format!("{url_base}/"), &bearer).await +} + pub async fn get_minecraft_config(db: &Pool) -> Vec { sqlx::query_as::<_, Minecraft>( @@ -782,7 +817,7 @@ pub async fn get_minecraft_config(db: &Pool) -> Vec { .unwrap_or_default() } -pub async fn get_minecraft_config_server(db: &Pool, g_id: GuildId) -> Option { +pub async fn get_minecraft_config_server(db: &Pool, g_id: GuildId) -> Vec { sqlx::query_as::<_, Minecraft>( r#" SELECT * @@ -791,7 +826,7 @@ pub async fn get_minecraft_config_server(db: &Pool, g_id: GuildId) -> O "#, ) .bind(*g_id.as_u64() as i64) - .fetch_optional(db) + .fetch_all(db) .await .unwrap_or_default() } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index e4613e5..c972037 100644 --- a/src/main.rs +++ b/src/main.rs @@ -61,6 +61,7 @@ impl EventHandler for Handler { .create_application_command(|command| commands::link_email::link::register(command)) .create_application_command(|command| commands::link_email::verify::register(command)) .create_application_command(|command| commands::minecraft::server::add::register(command)) + .create_application_command(|command| commands::minecraft::server::list::register(command)) .create_application_command(|command| commands::minecraft::user::add::register(command)) }) .await @@ -85,6 +86,7 @@ impl EventHandler for Handler { // admin commands "add" => commands::add_server::run(&command, &ctx).await, "minecraft_add" => commands::minecraft::server::add::run(&command, &ctx).await, + "minecraft_list" => commands::minecraft::server::list::run(&command, &ctx).await, _ => "not implemented :(".to_string(), };