From cb815052f4c6831cd30041cee4596417f8a9967c Mon Sep 17 00:00:00 2001 From: toast Date: Thu, 21 Mar 2024 08:55:48 +1100 Subject: [PATCH] Refactor PMS command --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/commands/status.rs | 40 ++++++++++++++++++---------------------- src/main.rs | 9 +++++---- 4 files changed, 25 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b06b534..b519bcd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -911,7 +911,7 @@ dependencies = [ [[package]] name = "kon" -version = "0.2.1" +version = "0.2.2" dependencies = [ "cargo_toml", "gamedig", diff --git a/Cargo.toml b/Cargo.toml index 9c31425..c8d4f2d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "kon" -version = "0.2.1" +version = "0.2.2" rust-version = "1.75" edition = "2021" diff --git a/src/commands/status.rs b/src/commands/status.rs index 9fcb226..5017004 100644 --- a/src/commands/status.rs +++ b/src/commands/status.rs @@ -50,6 +50,22 @@ async fn pms_serverstatus(url: &str) -> Result, Error> { Ok(servers) } +fn process_pms_statuses(servers: Vec>) -> Vec<(String, String, bool)> { + let mut statuses = Vec::new(); + for server_list in servers { + for server in server_list { + let name = server["name"].as_str().unwrap(); + let status = match server["availability"].as_str().unwrap() { + "1" => "Online", + "-1" => "Offline", + _ => "Unknown" + }; + statuses.push((name.to_owned(), status.to_owned(), true)); + } + } + statuses +} + async fn gs_query_minecraft(server_ip: &str) -> Result { let client = HttpClient::new(); let req = client.get(&format!("https://api.mcsrvstat.us/2/{}", server_ip)).await?; @@ -78,29 +94,9 @@ pub async fn wg(ctx: poise::Context<'_, (), Error>) -> Result<(), Error> { let embed = CreateEmbed::new().color(EMBED_COLOR); let (servers_asia, servers_eu) = join!(pms_serverstatus(&pms_asia), pms_serverstatus(&pms_eu)); + let pms_servers = process_pms_statuses(vec![servers_eu.unwrap(), servers_asia.unwrap()]); - 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(CreateReply::default().embed(embed.title("World of Tanks Server Status").fields(embed_fields))).await?; + ctx.send(CreateReply::default().embed(embed.title("World of Tanks Server Status").fields(pms_servers))).await?; Ok(()) } diff --git a/src/main.rs b/src/main.rs index a48c09a..c103e62 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,12 +3,11 @@ mod controllers; mod models; mod internals; -use poise::serenity_prelude::{self as serenity}; use std::{ env::var, error }; -use serenity::{ +use poise::serenity_prelude::{ builder::{ CreateMessage, CreateEmbed, @@ -17,6 +16,8 @@ use serenity::{ Context, Ready, ClientBuilder, + ChannelId, + Command, GatewayIntents }; @@ -37,13 +38,13 @@ async fn on_ready( .thumbnail(ready.user.avatar_url().unwrap_or_default()) .author(CreateEmbedAuthor::new(format!("{} is ready!", ready.user.name)).clone()); - serenity::ChannelId::new(BOT_READY_NOTIFY).send_message(&ctx.http, message.add_embed(ready_embed)).await?; + ChannelId::new(BOT_READY_NOTIFY).send_message(&ctx.http, message.add_embed(ready_embed)).await?; let register_commands = var("REGISTER_CMDS").unwrap_or_else(|_| String::from("true")).parse::().unwrap_or(true); if register_commands { let builder = poise::builtins::create_application_commands(&framework.options().commands); - let commands = serenity::Command::set_global_commands(&ctx.http, builder).await; + let commands = Command::set_global_commands(&ctx.http, builder).await; match commands { Ok(cmdmap) => for command in cmdmap.iter() {