From c7ebbf4c057181b40aa8d28e6f0885a58683c690 Mon Sep 17 00:00:00 2001 From: toast Date: Wed, 6 Dec 2023 21:49:55 +1100 Subject: [PATCH] I seem to be very pleased with Rust so far. --- Cargo.lock | 149 +++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 + src/commands/data.rs | 55 ++++++++++++++++ src/commands/mod.rs | 1 + src/main.rs | 3 +- 5 files changed, 209 insertions(+), 1 deletion(-) create mode 100644 src/commands/data.rs diff --git a/Cargo.lock b/Cargo.lock index b3f0caa..2c1c2a1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -430,6 +430,21 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "foreign-types" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" +dependencies = [ + "foreign-types-shared", +] + +[[package]] +name = "foreign-types-shared" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" + [[package]] name = "form_urlencoded" version = "1.2.1" @@ -661,6 +676,19 @@ dependencies = [ "tokio-rustls 0.24.1", ] +[[package]] +name = "hyper-tls" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905" +dependencies = [ + "bytes", + "hyper", + "native-tls", + "tokio", + "tokio-native-tls", +] + [[package]] name = "iana-time-zone" version = "0.1.58" @@ -828,6 +856,24 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "native-tls" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07226173c32f2926027b63cce4bcd8076c3552846cbe7925f3aaffeac0a3b92e" +dependencies = [ + "lazy_static", + "libc", + "log", + "openssl", + "openssl-probe", + "openssl-sys", + "schannel", + "security-framework", + "security-framework-sys", + "tempfile", +] + [[package]] name = "nu-ansi-term" version = "0.46.0" @@ -872,6 +918,50 @@ version = "1.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" +[[package]] +name = "openssl" +version = "0.10.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b8419dc8cc6d866deb801274bba2e6f8f6108c1bb7fcc10ee5ab864931dbb45" +dependencies = [ + "bitflags 2.4.1", + "cfg-if", + "foreign-types", + "libc", + "once_cell", + "openssl-macros", + "openssl-sys", +] + +[[package]] +name = "openssl-macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.39", +] + +[[package]] +name = "openssl-probe" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" + +[[package]] +name = "openssl-sys" +version = "0.9.97" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3eaad34cdd97d81de97964fc7f29e2d104f483840d906ef56daa1912338460b" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + [[package]] name = "ordered-float" version = "2.10.1" @@ -928,6 +1018,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "pkg-config" +version = "0.3.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964" + [[package]] name = "poise" version = "0.5.7" @@ -1084,11 +1180,13 @@ dependencies = [ "http-body", "hyper", "hyper-rustls", + "hyper-tls", "ipnet", "js-sys", "log", "mime", "mime_guess", + "native-tls", "once_cell", "percent-encoding", "pin-project-lite", @@ -1099,6 +1197,7 @@ dependencies = [ "serde_urlencoded", "system-configuration", "tokio", + "tokio-native-tls", "tokio-rustls 0.24.1", "tokio-util", "tower-service", @@ -1145,6 +1244,8 @@ name = "rustbot" version = "0.1.0" dependencies = [ "poise", + "reqwest", + "serde_json", "serenity 0.12.0", "tempfile", "tokio", @@ -1235,6 +1336,15 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "schannel" +version = "0.1.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c3733bf4cf7ea0880754e19cb5a462007c4a8c1914bff372ccc95b464f1df88" +dependencies = [ + "windows-sys 0.48.0", +] + [[package]] name = "scopeguard" version = "1.2.0" @@ -1261,6 +1371,29 @@ dependencies = [ "zeroize", ] +[[package]] +name = "security-framework" +version = "2.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05b64fb303737d99b81884b2c63433e9ae28abebe5eb5045dcdd175dc2ecf4de" +dependencies = [ + "bitflags 1.3.2", + "core-foundation", + "core-foundation-sys", + "libc", + "security-framework-sys", +] + +[[package]] +name = "security-framework-sys" +version = "2.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e932934257d3b408ed8f30db49d85ea163bfe74961f017f405b025af298f0c7a" +dependencies = [ + "core-foundation-sys", + "libc", +] + [[package]] name = "semver" version = "1.0.20" @@ -1670,6 +1803,16 @@ dependencies = [ "syn 2.0.39", ] +[[package]] +name = "tokio-native-tls" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbae76ab933c85776efabc971569dd6119c580d8f5d448769dec1764bf796ef2" +dependencies = [ + "native-tls", + "tokio", +] + [[package]] name = "tokio-rustls" version = "0.23.4" @@ -1956,6 +2099,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + [[package]] name = "version_check" version = "0.9.4" diff --git a/Cargo.toml b/Cargo.toml index 4f74436..6bac8fd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,8 @@ edition = "2021" [dependencies] poise = "0.5.7" +reqwest = "0.11.22" +serde_json = "1.0.108" serenity = "0.12.0" tempfile = "3.8.1" tokio = { version = "1.34.0", features = ["macros", "signal", "rt-multi-thread"] } diff --git a/src/commands/data.rs b/src/commands/data.rs new file mode 100644 index 0000000..ee0c1a6 --- /dev/null +++ b/src/commands/data.rs @@ -0,0 +1,55 @@ +use crate::Error; + +use reqwest::get; +use std::collections::HashMap; +use serde_json::Value; + +/// Retrieve the data from FS-Server +#[poise::command(slash_command)] +pub async fn data( + ctx: poise::Context<'_, (), Error>, + #[description = "DSS url to extract"] url: String +) -> Result<(), Error> { + // Find .xml and replace it with .json before passing to reqwest. + let url = url.replace(".xml", ".json"); + + // Send a GET request to the provided URL + let response = get(&url).await?.json::>().await?; + // println!("{:?}", response); + + // Extract the required values from the parsed JSON + let server = &response["server"]; + let slots = &response["slots"]; + + // Variable list of JSON values + let name = server["name"].as_str().unwrap(); + let ver = server["version"].as_str().unwrap(); + let map = server["mapName"].as_str().unwrap(); + let slot_cap = slots["capacity"].as_i64().unwrap(); + let slot_cur = slots["used"].as_i64().unwrap(); + + ctx.send(|m| { + m.embed(|e| { + e.color(0xf1d63c) + .author(|a| { + a.name(format!("{}/{}", slot_cur, slot_cap)) + }) + .title(name) + .description("*Nobody is playing*") + .fields(vec![ + ("Map", map, true), + ("Version", ver, true), + ("Time", "xx:xx", true), + ("Slot usage", "xx/xx", true), + ("Autosave", "xx", true), + ("Timescale", "0x", true) + ]) + .footer(|f| { + f.text("Last updated") + }) + .timestamp(poise::serenity_prelude::Timestamp::now()) + }) + }).await?; + + Ok(()) +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 85dd8f6..a9156ea 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,2 +1,3 @@ pub mod ping; pub mod eval; +pub mod data; diff --git a/src/main.rs b/src/main.rs index 461db72..f2e1945 100644 --- a/src/main.rs +++ b/src/main.rs @@ -52,7 +52,8 @@ async fn main() { .options(poise::FrameworkOptions { commands: vec![ commands::ping::ping(), - commands::eval::eval() + commands::eval::eval(), + commands::data::data() ], pre_command: |ctx| { Box::pin(async move {