From d70de0ea76249959d875453049e50fd9b65388c2 Mon Sep 17 00:00:00 2001 From: toast Date: Tue, 12 Mar 2024 22:39:17 +1100 Subject: [PATCH] Remove memory and move bot_version to utils --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/commands/status.rs | 12 ++++-------- src/commands/uptime.rs | 16 ++++++---------- src/main.rs | 2 +- src/utils.rs | 11 +++++++++-- 6 files changed, 22 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f913f39..91a6e3c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -833,7 +833,7 @@ dependencies = [ [[package]] name = "kon" -version = "0.1.18" +version = "0.1.19" dependencies = [ "cargo_toml", "gamedig", diff --git a/Cargo.toml b/Cargo.toml index 9792704..ccb4d6c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "kon" -version = "0.1.18" +version = "0.1.19" rust-version = "1.74" edition = "2021" diff --git a/src/commands/status.rs b/src/commands/status.rs index e3c9176..fbbba09 100644 --- a/src/commands/status.rs +++ b/src/commands/status.rs @@ -2,7 +2,8 @@ use crate::{ Error, EMBED_COLOR, models::gameservers::Gameservers, - commands::gameserver::ac_server_name + commands::gameserver::ac_server_name, + utils::BOT_VERSION }; use std::{ @@ -17,7 +18,6 @@ use tokio::join; use poise::CreateReply; use serenity::builder::CreateEmbed; use once_cell::sync::Lazy; -use cargo_toml::Manifest; use serde::Deserialize; use serde_json::Value; @@ -45,11 +45,9 @@ struct MinecraftPlayers { } async fn pms_serverstatus(url: &str) -> Result, Error> { - 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)) + .header(USER_AGENT, format!("Kon/{}/Rust", &**BOT_VERSION)) .send() .await?; let response = req.json::>().await?; @@ -59,11 +57,9 @@ async fn pms_serverstatus(url: &str) -> Result, Error> { } async fn gs_query_minecraft(server_ip: &str) -> Result { - let bot_version = Manifest::from_path("Cargo.toml").unwrap().package.unwrap().version.unwrap(); - let client = Client::new(); let req = client.get(format!("https://api.mcsrvstat.us/2/{}", server_ip)) - .header(USER_AGENT, format!("Kon/{}/Rust", bot_version)) + .header(USER_AGENT, format!("Kon/{}/Rust", &**BOT_VERSION)) .send() .await?; diff --git a/src/commands/uptime.rs b/src/commands/uptime.rs index 3773455..3eeb0f4 100644 --- a/src/commands/uptime.rs +++ b/src/commands/uptime.rs @@ -2,8 +2,8 @@ use crate::{ Error, utils::{ format_duration, - format_memory, - concat_message + concat_message, + BOT_VERSION } }; @@ -18,14 +18,10 @@ use std::time::{ /// Retrieve host and bot uptimes #[poise::command(slash_command)] pub async fn uptime(ctx: poise::Context<'_, (), Error>) -> Result<(), Error> { + let _bot = ctx.http().get_current_user().await.unwrap(); let mut sys = System::new_all(); sys.refresh_all(); - // Fetch system's memory usage - let memory_used = System::used_memory(&sys); - let memory_free = System::free_memory(&sys); - let memory_total = System::total_memory(&sys); - // Fetch system's uptime let sys_uptime = get().unwrap().as_secs(); @@ -39,9 +35,9 @@ pub async fn uptime(ctx: poise::Context<'_, (), Error>) -> Result<(), Error> { } let stat_msg = vec![ - format!("System: `{}`", format_duration(sys_uptime)), - format!("Process: `{}`", format_duration(proc_uptime)), - format!("Memory: `{} / {} / {}`", format_memory(memory_free), format_memory(memory_used), format_memory(memory_total)) + format!("**{} {}**", _bot.name, &**BOT_VERSION), + format!(">>> System: `{}`", format_duration(sys_uptime)), + format!("Process: `{}`", format_duration(proc_uptime)) ]; ctx.reply(concat_message(stat_msg)).await?; diff --git a/src/main.rs b/src/main.rs index 14b42a5..4c3763a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,7 +36,7 @@ async fn on_ready( let ready_embed = CreateEmbed::new() .color(EMBED_COLOR) .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, &**utils::BOT_VERSION)).clone()); serenity::ChannelId::new(BOT_READY_NOTIFY).send_message(&ctx.http, message.add_embed(ready_embed)).await?; diff --git a/src/utils.rs b/src/utils.rs index 6dfb32e..5157fd5 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,3 +1,10 @@ +use once_cell::sync::Lazy; + +pub static BOT_VERSION: Lazy = Lazy::new(|| { + let cargo_version = cargo_toml::Manifest::from_path("Cargo.toml").unwrap().package.unwrap().version.unwrap(); + format!("v{}", cargo_version) +}); + pub fn concat_message(messages: Vec) -> String { messages.join("\n") } @@ -23,7 +30,7 @@ pub fn format_duration(secs: u64) -> String { formatted_string } -pub fn format_memory(bytes: u64) -> String { +/* pub fn format_memory(bytes: u64) -> String { let kb = 1024; let mb = 1024 * 1024; let gb = 1024 * 1024 * 1024; @@ -34,4 +41,4 @@ pub fn format_memory(bytes: u64) -> String { b if b >= kb => format!("{:.0} KB", (b as f64 / 1024.0).ceil()), _ => format!("{:.0} B", bytes), } -} +} */