2023-12-08 06:25:44 -05:00
|
|
|
use crate::{Error, COLOR};
|
2023-12-07 22:01:13 -05:00
|
|
|
|
2023-12-29 07:00:44 -05:00
|
|
|
use reqwest::{
|
|
|
|
Client,
|
|
|
|
header::USER_AGENT
|
|
|
|
};
|
|
|
|
use std::{
|
|
|
|
collections::HashMap,
|
|
|
|
env::var
|
|
|
|
};
|
2023-12-11 20:35:57 -05:00
|
|
|
use cargo_toml::Manifest;
|
2023-12-07 22:01:13 -05:00
|
|
|
use serde_json::Value;
|
2023-12-08 06:25:44 -05:00
|
|
|
use tokio::join;
|
2023-12-07 22:01:13 -05:00
|
|
|
|
|
|
|
lazy_static::lazy_static! {
|
2023-12-29 07:00:44 -05:00
|
|
|
static ref PMS_BASE: String = var("WG_PMS").expect("Expected a \"WG_PMS\" in the envvar but none was found");
|
2023-12-07 22:01:13 -05:00
|
|
|
}
|
|
|
|
|
2023-12-08 06:25:44 -05:00
|
|
|
/// Retrieve the server statuses from Wargaming
|
2023-12-07 22:01:13 -05:00
|
|
|
#[poise::command(slash_command)]
|
2023-12-29 07:00:44 -05:00
|
|
|
pub async fn wg_status(ctx: poise::Context<'_, (), Error>) -> Result<(), Error> {
|
2023-12-08 06:25:44 -05:00
|
|
|
let pms_asia = &PMS_BASE;
|
|
|
|
let pms_eu = PMS_BASE.replace("asia", "eu");
|
2023-12-07 22:01:13 -05:00
|
|
|
|
2023-12-08 06:25:44 -05:00
|
|
|
let (servers_asia, servers_eu) = join!(pms_serverstatus(&pms_asia), pms_serverstatus(&pms_eu));
|
2023-12-07 22:01:13 -05:00
|
|
|
|
2023-12-08 06:25:44 -05:00
|
|
|
let mut embed_fields = Vec::new();
|
|
|
|
for server in servers_eu.unwrap() {
|
2023-12-07 22:01:13 -05:00
|
|
|
let name = server["name"].as_str().unwrap().to_owned();
|
|
|
|
let status = match server["availability"].as_str().unwrap() {
|
|
|
|
"1" => "Online",
|
|
|
|
"-1" => "Offline",
|
|
|
|
_ => "Unknown"
|
|
|
|
};
|
2023-12-08 06:25:44 -05:00
|
|
|
embed_fields.push((name, status, true));
|
2023-12-07 22:01:13 -05:00
|
|
|
}
|
|
|
|
|
2023-12-08 06:25:44 -05:00
|
|
|
for server in servers_asia.unwrap() {
|
2023-12-07 22:01:13 -05:00
|
|
|
let name = server["name"].as_str().unwrap().to_owned();
|
|
|
|
let status = match server["availability"].as_str().unwrap() {
|
|
|
|
"1" => "Online",
|
|
|
|
"-1" => "Offline",
|
|
|
|
_ => "Unknown"
|
|
|
|
};
|
2023-12-08 06:25:44 -05:00
|
|
|
embed_fields.push((name, status, true));
|
2023-12-07 22:01:13 -05:00
|
|
|
}
|
|
|
|
|
2023-12-08 06:25:44 -05:00
|
|
|
ctx.send(|m| m.embed(|e|
|
|
|
|
e.color(COLOR)
|
|
|
|
.title("World of Tanks Server Status")
|
|
|
|
.fields(embed_fields)
|
|
|
|
)).await?;
|
2023-12-07 22:01:13 -05:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn pms_serverstatus(url: &str) -> Result<Vec<Value>, Error> {
|
2023-12-11 20:35:57 -05:00
|
|
|
let bot_version = Manifest::from_path("Cargo.toml").unwrap().package.unwrap().version.unwrap();
|
|
|
|
|
|
|
|
let client = Client::new();
|
|
|
|
let req = client.get(url)
|
|
|
|
.header(USER_AGENT, format!("Kon/{}/Rust", bot_version))
|
|
|
|
.send()
|
|
|
|
.await?;
|
|
|
|
let response = req.json::<HashMap<String, Value>>().await?;
|
2023-12-07 22:01:13 -05:00
|
|
|
let servers = response["data"].as_array().unwrap()[0]["servers_statuses"]["data"].as_array().unwrap().clone();
|
|
|
|
|
|
|
|
Ok(servers)
|
|
|
|
}
|