Refactor PMS command
All checks were successful
Build and push container image / build (push) Successful in 10m48s
All checks were successful
Build and push container image / build (push) Successful in 10m48s
This commit is contained in:
parent
423a881f86
commit
cb815052f4
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -911,7 +911,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "kon"
|
name = "kon"
|
||||||
version = "0.2.1"
|
version = "0.2.2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cargo_toml",
|
"cargo_toml",
|
||||||
"gamedig",
|
"gamedig",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "kon"
|
name = "kon"
|
||||||
version = "0.2.1"
|
version = "0.2.2"
|
||||||
rust-version = "1.75"
|
rust-version = "1.75"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
|
@ -50,6 +50,22 @@ async fn pms_serverstatus(url: &str) -> Result<Vec<Value>, Error> {
|
|||||||
Ok(servers)
|
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> {
|
async fn gs_query_minecraft(server_ip: &str) -> Result<MinecraftQueryData, Error> {
|
||||||
let client = HttpClient::new();
|
let client = HttpClient::new();
|
||||||
let req = client.get(&format!("https://api.mcsrvstat.us/2/{}", server_ip)).await?;
|
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 embed = CreateEmbed::new().color(EMBED_COLOR);
|
||||||
|
|
||||||
let (servers_asia, servers_eu) = join!(pms_serverstatus(&pms_asia), pms_serverstatus(&pms_eu));
|
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();
|
ctx.send(CreateReply::default().embed(embed.title("World of Tanks Server Status").fields(pms_servers))).await?;
|
||||||
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?;
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -3,12 +3,11 @@ mod controllers;
|
|||||||
mod models;
|
mod models;
|
||||||
mod internals;
|
mod internals;
|
||||||
|
|
||||||
use poise::serenity_prelude::{self as serenity};
|
|
||||||
use std::{
|
use std::{
|
||||||
env::var,
|
env::var,
|
||||||
error
|
error
|
||||||
};
|
};
|
||||||
use serenity::{
|
use poise::serenity_prelude::{
|
||||||
builder::{
|
builder::{
|
||||||
CreateMessage,
|
CreateMessage,
|
||||||
CreateEmbed,
|
CreateEmbed,
|
||||||
@ -17,6 +16,8 @@ use serenity::{
|
|||||||
Context,
|
Context,
|
||||||
Ready,
|
Ready,
|
||||||
ClientBuilder,
|
ClientBuilder,
|
||||||
|
ChannelId,
|
||||||
|
Command,
|
||||||
GatewayIntents
|
GatewayIntents
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -37,13 +38,13 @@ async fn on_ready(
|
|||||||
.thumbnail(ready.user.avatar_url().unwrap_or_default())
|
.thumbnail(ready.user.avatar_url().unwrap_or_default())
|
||||||
.author(CreateEmbedAuthor::new(format!("{} is ready!", ready.user.name)).clone());
|
.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);
|
let register_commands = var("REGISTER_CMDS").unwrap_or_else(|_| String::from("true")).parse::<bool>().unwrap_or(true);
|
||||||
|
|
||||||
if register_commands {
|
if register_commands {
|
||||||
let builder = poise::builtins::create_application_commands(&framework.options().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 {
|
match commands {
|
||||||
Ok(cmdmap) => for command in cmdmap.iter() {
|
Ok(cmdmap) => for command in cmdmap.iter() {
|
||||||
|
Loading…
Reference in New Issue
Block a user