Compare commits

..

2 Commits

Author SHA1 Message Date
cb815052f4 Refactor PMS command
All checks were successful
Build and push container image / build (push) Successful in 10m48s
2024-03-21 08:55:48 +11:00
423a881f86 Use Serenity from Poise package directly 2024-03-21 08:35:44 +11:00
7 changed files with 210 additions and 95 deletions

234
Cargo.lock generated
View File

@ -212,17 +212,6 @@ dependencies = [
"windows-targets 0.48.5",
]
[[package]]
name = "command_attr"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32f08c85a02e066b7b4f7dcb60eee6ae0793ef7d6452a3547d1f19665df070a9"
dependencies = [
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]]
name = "core-foundation"
version = "0.9.4"
@ -648,6 +637,25 @@ dependencies = [
"tracing",
]
[[package]]
name = "h2"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51ee2dd2e4f378392eeff5d51618cd9a63166a2513846bbc55f21cfacd9199d4"
dependencies = [
"bytes",
"fnv",
"futures-core",
"futures-sink",
"futures-util",
"http 1.0.0",
"indexmap",
"slab",
"tokio",
"tokio-util",
"tracing",
]
[[package]]
name = "hashbrown"
version = "0.14.3"
@ -702,6 +710,29 @@ dependencies = [
"pin-project-lite",
]
[[package]]
name = "http-body"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1cac85db508abc24a2e48553ba12a996e87244a0395ce011e62b37158745d643"
dependencies = [
"bytes",
"http 1.0.0",
]
[[package]]
name = "http-body-util"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0475f8b2ac86659c21b64320d5d653f9efe42acd2a4e560073ec61a155a34f1d"
dependencies = [
"bytes",
"futures-core",
"http 1.0.0",
"http-body 1.0.0",
"pin-project-lite",
]
[[package]]
name = "httparse"
version = "1.8.0"
@ -724,9 +755,9 @@ dependencies = [
"futures-channel",
"futures-core",
"futures-util",
"h2",
"h2 0.3.22",
"http 0.2.11",
"http-body",
"http-body 0.4.5",
"httparse",
"httpdate",
"itoa",
@ -738,6 +769,26 @@ dependencies = [
"want",
]
[[package]]
name = "hyper"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "186548d73ac615b32a73aafe38fb4f56c0d340e110e5a200bcadbaf2e199263a"
dependencies = [
"bytes",
"futures-channel",
"futures-util",
"h2 0.4.3",
"http 1.0.0",
"http-body 1.0.0",
"httparse",
"itoa",
"pin-project-lite",
"smallvec",
"tokio",
"want",
]
[[package]]
name = "hyper-rustls"
version = "0.24.2"
@ -746,7 +797,7 @@ checksum = "ec3efd23720e2049821a693cbc7e65ea87c72f1c58ff2f9522ff332b1491e590"
dependencies = [
"futures-util",
"http 0.2.11",
"hyper",
"hyper 0.14.27",
"rustls 0.21.9",
"tokio",
"tokio-rustls 0.24.1",
@ -754,15 +805,38 @@ dependencies = [
[[package]]
name = "hyper-tls"
version = "0.5.0"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905"
checksum = "70206fc6890eaca9fde8a0bf71caa2ddfc9fe045ac9e5c70df101a7dbde866e0"
dependencies = [
"bytes",
"hyper",
"http-body-util",
"hyper 1.2.0",
"hyper-util",
"native-tls",
"tokio",
"tokio-native-tls",
"tower-service",
]
[[package]]
name = "hyper-util"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca38ef113da30126bbff9cd1705f9273e15d45498615d138b0c20279ac7a76aa"
dependencies = [
"bytes",
"futures-channel",
"futures-util",
"http 1.0.0",
"http-body 1.0.0",
"hyper 1.2.0",
"pin-project-lite",
"socket2 0.5.5",
"tokio",
"tower",
"tower-service",
"tracing",
]
[[package]]
@ -837,16 +911,15 @@ dependencies = [
[[package]]
name = "kon"
version = "0.2.0"
version = "0.2.2"
dependencies = [
"cargo_toml",
"gamedig",
"once_cell",
"poise",
"reqwest",
"reqwest 0.12.0",
"serde",
"serde_json",
"serenity",
"sysinfo",
"tokio",
"tokio-postgres",
@ -859,12 +932,6 @@ version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "levenshtein"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db13adb97ab515a3691f56e4dbab09283d0b86cb45abd991d8634a9d6f501760"
[[package]]
name = "libc"
version = "0.2.153"
@ -1136,6 +1203,26 @@ dependencies = [
"siphasher",
]
[[package]]
name = "pin-project"
version = "1.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6bf43b791c5b9e34c3d182969b4abb522f9343702850a2e57f460d00d09b4b3"
dependencies = [
"pin-project-internal",
]
[[package]]
name = "pin-project-internal"
version = "1.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.46",
]
[[package]]
name = "pin-project-lite"
version = "0.2.13"
@ -1352,18 +1439,16 @@ dependencies = [
"encoding_rs",
"futures-core",
"futures-util",
"h2",
"h2 0.3.22",
"http 0.2.11",
"http-body",
"hyper",
"http-body 0.4.5",
"hyper 0.14.27",
"hyper-rustls",
"hyper-tls",
"ipnet",
"js-sys",
"log",
"mime",
"mime_guess",
"native-tls",
"once_cell",
"percent-encoding",
"pin-project-lite",
@ -1375,7 +1460,6 @@ dependencies = [
"sync_wrapper",
"system-configuration",
"tokio",
"tokio-native-tls",
"tokio-rustls 0.24.1",
"tokio-util",
"tower-service",
@ -1388,6 +1472,48 @@ dependencies = [
"winreg",
]
[[package]]
name = "reqwest"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "58b48d98d932f4ee75e541614d32a7f44c889b72bd9c2e04d95edd135989df88"
dependencies = [
"base64",
"bytes",
"encoding_rs",
"futures-core",
"futures-util",
"h2 0.4.3",
"http 1.0.0",
"http-body 1.0.0",
"http-body-util",
"hyper 1.2.0",
"hyper-tls",
"hyper-util",
"ipnet",
"js-sys",
"log",
"mime",
"native-tls",
"once_cell",
"percent-encoding",
"pin-project-lite",
"rustls-pemfile",
"serde",
"serde_json",
"serde_urlencoded",
"sync_wrapper",
"system-configuration",
"tokio",
"tokio-native-tls",
"tower-service",
"url",
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
"winreg",
]
[[package]]
name = "ring"
version = "0.17.6"
@ -1629,20 +1755,17 @@ dependencies = [
"bitflags 2.4.2",
"bytes",
"chrono",
"command_attr",
"dashmap",
"flate2",
"futures",
"fxhash",
"levenshtein",
"mime_guess",
"parking_lot",
"percent-encoding",
"reqwest",
"reqwest 0.11.27",
"secrecy",
"serde",
"serde_json",
"static_assertions",
"time",
"tokio",
"tokio-tungstenite",
@ -1650,7 +1773,6 @@ dependencies = [
"typemap_rev",
"typesize",
"url",
"uwl",
]
[[package]]
@ -1716,9 +1838,9 @@ dependencies = [
[[package]]
name = "smallvec"
version = "1.11.2"
version = "1.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4dccd0940a2dcdf68d092b8cbab7dc0ad8fa938bf95787e1b916b0e3d0e8e970"
checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"
[[package]]
name = "socket2"
@ -1746,12 +1868,6 @@ version = "0.9.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67"
[[package]]
name = "static_assertions"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
[[package]]
name = "stringprep"
version = "0.1.4"
@ -2072,6 +2188,28 @@ dependencies = [
"winnow",
]
[[package]]
name = "tower"
version = "0.4.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8fa9be0de6cf49e536ce1851f987bd21a43b771b09473c3549a6c853db37c1c"
dependencies = [
"futures-core",
"futures-util",
"pin-project",
"pin-project-lite",
"tokio",
"tower-layer",
"tower-service",
"tracing",
]
[[package]]
name = "tower-layer"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c20c8dbed6283a09604c3e69b4b7eeb54e298b8a600d4d5ecb5ad39de609f1d0"
[[package]]
name = "tower-service"
version = "0.3.2"
@ -2264,12 +2402,6 @@ version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9"
[[package]]
name = "uwl"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4bf03e0ca70d626ecc4ba6b0763b934b6f2976e8c744088bb3c1d646fbb1ad0"
[[package]]
name = "vcpkg"
version = "0.2.15"

View File

@ -1,6 +1,6 @@
[package]
name = "kon"
version = "0.2.0"
version = "0.2.2"
rust-version = "1.75"
edition = "2021"
@ -12,7 +12,6 @@ poise = "0.6.1"
reqwest = { version = "0.12.0", features = ["json"] }
serde = "1.0.197"
serde_json = "1.0.114"
serenity = "0.12.1"
sysinfo = "0.30.7"
tokio = { version = "1.36.0", features = ["macros", "signal", "rt-multi-thread"] }
tokio-postgres = "0.7.10"

View File

@ -4,7 +4,7 @@ use crate::{
models::gameservers::Gameservers
};
use serenity::{
use poise::serenity_prelude::{
futures::{
stream::iter,
future::ready,

View File

@ -12,7 +12,7 @@ use std::{
};
use tokio::join;
use poise::CreateReply;
use serenity::builder::CreateEmbed;
use poise::serenity_prelude::builder::CreateEmbed;
use once_cell::sync::Lazy;
use serde::Deserialize;
use serde_json::Value;
@ -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

@ -1,4 +1,4 @@
use serenity::prelude::TypeMapKey;
use poise::serenity_prelude::prelude::TypeMapKey;
use tokio_postgres::{Client, NoTls, Error};
pub struct DatabaseController {

View File

@ -31,16 +31,3 @@ pub fn format_duration(secs: u64) -> String {
formatted_string
}
/* pub fn format_memory(bytes: u64) -> String {
let kb = 1024;
let mb = 1024 * 1024;
let gb = 1024 * 1024 * 1024;
match bytes {
b if b >= gb => format!("{:.0} GB", (b as f64 / (1024.0 * 1024.0 * 1024.0)).ceil()),
b if b >= mb => format!("{:.0} MB", (b as f64 / (1024.0 * 1024.0)).ceil()),
b if b >= kb => format!("{:.0} KB", (b as f64 / 1024.0).ceil()),
_ => format!("{:.0} B", bytes),
}
} */

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() {