Remove memory and move bot_version to utils
All checks were successful
Build and push container image / build (push) Successful in 10m58s

This commit is contained in:
toast 2024-03-12 22:39:17 +11:00
parent 13863b937f
commit d70de0ea76
6 changed files with 22 additions and 23 deletions

2
Cargo.lock generated
View File

@ -833,7 +833,7 @@ dependencies = [
[[package]] [[package]]
name = "kon" name = "kon"
version = "0.1.18" version = "0.1.19"
dependencies = [ dependencies = [
"cargo_toml", "cargo_toml",
"gamedig", "gamedig",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "kon" name = "kon"
version = "0.1.18" version = "0.1.19"
rust-version = "1.74" rust-version = "1.74"
edition = "2021" edition = "2021"

View File

@ -2,7 +2,8 @@ use crate::{
Error, Error,
EMBED_COLOR, EMBED_COLOR,
models::gameservers::Gameservers, models::gameservers::Gameservers,
commands::gameserver::ac_server_name commands::gameserver::ac_server_name,
utils::BOT_VERSION
}; };
use std::{ use std::{
@ -17,7 +18,6 @@ use tokio::join;
use poise::CreateReply; use poise::CreateReply;
use serenity::builder::CreateEmbed; use serenity::builder::CreateEmbed;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use cargo_toml::Manifest;
use serde::Deserialize; use serde::Deserialize;
use serde_json::Value; use serde_json::Value;
@ -45,11 +45,9 @@ struct MinecraftPlayers {
} }
async fn pms_serverstatus(url: &str) -> Result<Vec<Value>, Error> { async fn pms_serverstatus(url: &str) -> Result<Vec<Value>, Error> {
let bot_version = Manifest::from_path("Cargo.toml").unwrap().package.unwrap().version.unwrap();
let client = Client::new(); let client = Client::new();
let req = client.get(url) let req = client.get(url)
.header(USER_AGENT, format!("Kon/{}/Rust", bot_version)) .header(USER_AGENT, format!("Kon/{}/Rust", &**BOT_VERSION))
.send() .send()
.await?; .await?;
let response = req.json::<HashMap<String, Value>>().await?; let response = req.json::<HashMap<String, Value>>().await?;
@ -59,11 +57,9 @@ async fn pms_serverstatus(url: &str) -> Result<Vec<Value>, Error> {
} }
async fn gs_query_minecraft(server_ip: &str) -> Result<MinecraftQueryData, Error> { async fn gs_query_minecraft(server_ip: &str) -> Result<MinecraftQueryData, Error> {
let bot_version = Manifest::from_path("Cargo.toml").unwrap().package.unwrap().version.unwrap();
let client = Client::new(); let client = Client::new();
let req = client.get(format!("https://api.mcsrvstat.us/2/{}", server_ip)) 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() .send()
.await?; .await?;

View File

@ -2,8 +2,8 @@ use crate::{
Error, Error,
utils::{ utils::{
format_duration, format_duration,
format_memory, concat_message,
concat_message BOT_VERSION
} }
}; };
@ -18,14 +18,10 @@ use std::time::{
/// Retrieve host and bot uptimes /// Retrieve host and bot uptimes
#[poise::command(slash_command)] #[poise::command(slash_command)]
pub async fn uptime(ctx: poise::Context<'_, (), Error>) -> Result<(), Error> { 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(); let mut sys = System::new_all();
sys.refresh_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 // Fetch system's uptime
let sys_uptime = get().unwrap().as_secs(); 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![ let stat_msg = vec![
format!("System: `{}`", format_duration(sys_uptime)), format!("**{} {}**", _bot.name, &**BOT_VERSION),
format!("Process: `{}`", format_duration(proc_uptime)), format!(">>> System: `{}`", format_duration(sys_uptime)),
format!("Memory: `{} / {} / {}`", format_memory(memory_free), format_memory(memory_used), format_memory(memory_total)) format!("Process: `{}`", format_duration(proc_uptime))
]; ];
ctx.reply(concat_message(stat_msg)).await?; ctx.reply(concat_message(stat_msg)).await?;

View File

@ -36,7 +36,7 @@ async fn on_ready(
let ready_embed = CreateEmbed::new() let ready_embed = CreateEmbed::new()
.color(EMBED_COLOR) .color(EMBED_COLOR)
.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, &**utils::BOT_VERSION)).clone());
serenity::ChannelId::new(BOT_READY_NOTIFY).send_message(&ctx.http, message.add_embed(ready_embed)).await?; serenity::ChannelId::new(BOT_READY_NOTIFY).send_message(&ctx.http, message.add_embed(ready_embed)).await?;

View File

@ -1,3 +1,10 @@
use once_cell::sync::Lazy;
pub static BOT_VERSION: Lazy<String> = 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>) -> String { pub fn concat_message(messages: Vec<String>) -> String {
messages.join("\n") messages.join("\n")
} }
@ -23,7 +30,7 @@ pub fn format_duration(secs: u64) -> String {
formatted_string formatted_string
} }
pub fn format_memory(bytes: u64) -> String { /* pub fn format_memory(bytes: u64) -> String {
let kb = 1024; let kb = 1024;
let mb = 1024 * 1024; let mb = 1024 * 1024;
let gb = 1024 * 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()), b if b >= kb => format!("{:.0} KB", (b as f64 / 1024.0).ceil()),
_ => format!("{:.0} B", bytes), _ => format!("{:.0} B", bytes),
} }
} } */