Refactor PMS command
All checks were successful
Build and push container image / build (push) Successful in 10m48s

This commit is contained in:
toast 2024-03-21 08:55:48 +11:00
parent 423a881f86
commit cb815052f4
4 changed files with 25 additions and 28 deletions

2
Cargo.lock generated
View File

@ -911,7 +911,7 @@ dependencies = [
[[package]]
name = "kon"
version = "0.2.1"
version = "0.2.2"
dependencies = [
"cargo_toml",
"gamedig",

View File

@ -1,6 +1,6 @@
[package]
name = "kon"
version = "0.2.1"
version = "0.2.2"
rust-version = "1.75"
edition = "2021"

View File

@ -50,6 +50,22 @@ async fn pms_serverstatus(url: &str) -> Result<Vec<Value>, Error> {
Ok(servers)
}
fn process_pms_statuses(servers: Vec<Vec<Value>>) -> 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<MinecraftQueryData, Error> {
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(())
}

View File

@ -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::<bool>().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() {