use crate::{Error, COLOR}; use reqwest::get; use std::collections::HashMap; use serde_json::Value; use tokio::join; lazy_static::lazy_static! { static ref PMS_BASE: String = std::env::var("WG_PMS").expect("Expected a \"WG_PMS\" in the envvar but none was found"); } /// Retrieve the server statuses from Wargaming #[poise::command(slash_command)] pub async fn status(ctx: poise::Context<'_, (), Error>) -> Result<(), Error> { let pms_asia = &PMS_BASE; let pms_eu = PMS_BASE.replace("asia", "eu"); let (servers_asia, servers_eu) = join!(pms_serverstatus(&pms_asia), pms_serverstatus(&pms_eu)); let mut embed_fields = Vec::new(); for server in servers_eu.unwrap() { let name = server["name"].as_str().unwrap().to_owned(); let status = match server["availability"].as_str().unwrap() { "1" => "Online", "-1" => "Offline", _ => "Unknown" }; embed_fields.push((name, status, true)); } for server in servers_asia.unwrap() { let name = server["name"].as_str().unwrap().to_owned(); let status = match server["availability"].as_str().unwrap() { "1" => "Online", "-1" => "Offline", _ => "Unknown" }; embed_fields.push((name, status, true)); } ctx.send(|m| m.embed(|e| e.color(COLOR) .title("World of Tanks Server Status") .fields(embed_fields) )).await?; Ok(()) } async fn pms_serverstatus(url: &str) -> Result, Error> { let response = get(url).await?.json::>().await?; let servers = response["data"].as_array().unwrap()[0]["servers_statuses"]["data"].as_array().unwrap().clone(); Ok(servers) }