diff --git a/Cargo.lock b/Cargo.lock index ac18533..82932c1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -164,6 +164,16 @@ dependencies = [ "serde_json", ] +[[package]] +name = "cargo_toml" +version = "0.19.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a98356df42a2eb1bd8f1793ae4ee4de48e384dd974ce5eac8eee802edb7492be" +dependencies = [ + "serde", + "toml", +] + [[package]] name = "cc" version = "1.0.83" @@ -930,9 +940,9 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.18.0" +version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" +checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" [[package]] name = "openssl" @@ -1248,6 +1258,8 @@ dependencies = [ name = "rustbot" version = "0.1.0" dependencies = [ + "cargo_toml", + "once_cell", "poise", "reqwest", "serde", @@ -1453,6 +1465,15 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_spanned" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb3622f419d1296904700073ea6cc23ad690adbd66f13ea683df73298736f0c1" +dependencies = [ + "serde", +] + [[package]] name = "serde_urlencoded" version = "0.7.1" @@ -1831,6 +1852,40 @@ dependencies = [ "tracing", ] +[[package]] +name = "toml" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af06656561d28735e9c1cd63dfd57132c8155426aa6af24f36a00a351f88c48e" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit", +] + +[[package]] +name = "toml_datetime" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3550f4e9685620ac18a50ed434eb3aec30db8ba93b0287467bca5826ea25baf1" +dependencies = [ + "serde", +] + +[[package]] +name = "toml_edit" +version = "0.22.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "18769cd1cec395d70860ceb4d932812a0b4d06b1a4bb336745a4d21b9496e992" +dependencies = [ + "indexmap", + "serde", + "serde_spanned", + "toml_datetime", + "winnow", +] + [[package]] name = "tower-service" version = "0.3.2" @@ -2346,6 +2401,15 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" +[[package]] +name = "winnow" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dffa400e67ed5a4dd237983829e66475f0a4a26938c4b04c21baede6262215b8" +dependencies = [ + "memchr", +] + [[package]] name = "winreg" version = "0.50.0" diff --git a/Cargo.toml b/Cargo.toml index d9922c9..5ca85a8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,15 +4,17 @@ version = "0.1.0" edition = "2021" [dependencies] +cargo_toml = "0.19.2" +once_cell = "1.19.0" poise = "0.6.1" reqwest = { version = "0.11.26", features = ["json"] } +serde = "1.0.197" serde_json = "1.0.114" serenity = "0.12.1" -tempfile = "3.10.1" sysinfo = "0.30.7" +tempfile = "3.10.1" tokio = { version = "1.36.0", features = ["macros", "signal", "rt-multi-thread"] } uptime_lib = "0.3.0" -serde = "1.0.197" [[bin]] name = "rustbot" @@ -23,6 +25,6 @@ opt-level = 0 debug = true [profile.release] -opt-level = 3 +opt-level = 2 debug = false strip = true diff --git a/src/commands/uptime.rs b/src/commands/uptime.rs index a321bd0..3eeb0f4 100644 --- a/src/commands/uptime.rs +++ b/src/commands/uptime.rs @@ -1,4 +1,11 @@ -use crate::Error; +use crate::{ + Error, + utils::{ + format_duration, + concat_message, + BOT_VERSION + } +}; use sysinfo::System; use uptime_lib::get; @@ -11,6 +18,7 @@ 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(); @@ -26,27 +34,12 @@ pub async fn uptime(ctx: poise::Context<'_, (), Error>) -> Result<(), Error> { proc_uptime = now.duration_since(time_started).unwrap().as_secs(); } - ctx.reply(format!("System: `{}`\nProcess: `{}`", format_duration(sys_uptime), format_duration(proc_uptime))).await?; + let stat_msg = vec![ + format!("**{} {}**", _bot.name, &**BOT_VERSION), + format!(">>> System: `{}`", format_duration(sys_uptime)), + format!("Process: `{}`", format_duration(proc_uptime)) + ]; + ctx.reply(concat_message(stat_msg)).await?; + Ok(()) } - -fn format_duration(secs: u64) -> String { - let days = secs / 86400; - let hours = (secs % 86400) / 3600; - let minutes = (secs % 3600) / 60; - let seconds = secs % 60; - - let mut formatted_string = String::new(); - if days > 0 { - formatted_string.push_str(&format!("{}d, ", days)); - } - if hours > 0 || days > 0 { - formatted_string.push_str(&format!("{}h, ", hours)); - } - if minutes > 0 || hours > 0 { - formatted_string.push_str(&format!("{}m, ", minutes)); - } - formatted_string.push_str(&format!("{}s", seconds)); - - formatted_string -} diff --git a/src/main.rs b/src/main.rs index 9efa315..b8a9a4d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,11 @@ mod commands; +mod utils; -use std::env::var; use poise::serenity_prelude::{self as serenity}; +use std::{ + env::var, + error +}; use serenity::{ builder::{ CreateMessage, @@ -14,9 +18,8 @@ use serenity::{ GatewayIntents }; -type Error = Box; +type Error = Box; -pub static EMBED_COLOR: i32 = 0xf1d63c; static BOT_READY_NOTIFY: u64 = 865673694184996888; async fn on_ready( @@ -28,7 +31,7 @@ async fn on_ready( let message = CreateMessage::new(); let ready_embed = CreateEmbed::new() - .color(EMBED_COLOR) + .color(utils::EMBED_COLOR) .thumbnail(ready.user.avatar_url().unwrap_or_default()) .author(CreateEmbedAuthor::new(format!("{} is ready!", ready.user.name)).clone()); diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..aba9a37 --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,46 @@ +use once_cell::sync::Lazy; + +pub static EMBED_COLOR: i32 = 0xf1d63c; + +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") +} + +pub fn format_duration(secs: u64) -> String { + let days = secs / 86400; + let hours = (secs % 86400) / 3600; + let minutes = (secs % 3600) / 60; + let seconds = secs % 60; + + let mut formatted_string = String::new(); + if days > 0 { + formatted_string.push_str(&format!("{}d, ", days)); + } + if hours > 0 || days > 0 { + formatted_string.push_str(&format!("{}h, ", hours)); + } + if minutes > 0 || hours > 0 { + formatted_string.push_str(&format!("{}m, ", minutes)); + } + formatted_string.push_str(&format!("{}s", seconds)); + + 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), + } +} */ diff --git a/tomlfmt.toml b/tomlfmt.toml new file mode 100644 index 0000000..4cb665f --- /dev/null +++ b/tomlfmt.toml @@ -0,0 +1,19 @@ +# trailing comma in arrays +always_trailing_comma = true + +# trailing comma when multi-line +multiline_trailing_comma = true + +# remove all the spacing inside the array +compact_arrays = false + +# remove all the spacing inside the object +compact_inline_tables = false + +# Windows EOF style +crlf = false + +trailing_newline = false +space_around_eq = true +allowed_blank_lines = 1 +