Some formatting changes

This commit is contained in:
toast 2024-03-13 20:27:16 +11:00
parent 665d386bc9
commit 32e3240b1c
6 changed files with 159 additions and 32 deletions

68
Cargo.lock generated
View File

@ -164,6 +164,16 @@ dependencies = [
"serde_json", "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]] [[package]]
name = "cc" name = "cc"
version = "1.0.83" version = "1.0.83"
@ -930,9 +940,9 @@ dependencies = [
[[package]] [[package]]
name = "once_cell" name = "once_cell"
version = "1.18.0" version = "1.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
[[package]] [[package]]
name = "openssl" name = "openssl"
@ -1248,6 +1258,8 @@ dependencies = [
name = "rustbot" name = "rustbot"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"cargo_toml",
"once_cell",
"poise", "poise",
"reqwest", "reqwest",
"serde", "serde",
@ -1453,6 +1465,15 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "serde_spanned"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb3622f419d1296904700073ea6cc23ad690adbd66f13ea683df73298736f0c1"
dependencies = [
"serde",
]
[[package]] [[package]]
name = "serde_urlencoded" name = "serde_urlencoded"
version = "0.7.1" version = "0.7.1"
@ -1831,6 +1852,40 @@ dependencies = [
"tracing", "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]] [[package]]
name = "tower-service" name = "tower-service"
version = "0.3.2" version = "0.3.2"
@ -2346,6 +2401,15 @@ version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04"
[[package]]
name = "winnow"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dffa400e67ed5a4dd237983829e66475f0a4a26938c4b04c21baede6262215b8"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "winreg" name = "winreg"
version = "0.50.0" version = "0.50.0"

View File

@ -4,15 +4,17 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
cargo_toml = "0.19.2"
once_cell = "1.19.0"
poise = "0.6.1" poise = "0.6.1"
reqwest = { version = "0.11.26", features = ["json"] } reqwest = { version = "0.11.26", features = ["json"] }
serde = "1.0.197"
serde_json = "1.0.114" serde_json = "1.0.114"
serenity = "0.12.1" serenity = "0.12.1"
tempfile = "3.10.1"
sysinfo = "0.30.7" sysinfo = "0.30.7"
tempfile = "3.10.1"
tokio = { version = "1.36.0", features = ["macros", "signal", "rt-multi-thread"] } tokio = { version = "1.36.0", features = ["macros", "signal", "rt-multi-thread"] }
uptime_lib = "0.3.0" uptime_lib = "0.3.0"
serde = "1.0.197"
[[bin]] [[bin]]
name = "rustbot" name = "rustbot"
@ -23,6 +25,6 @@ opt-level = 0
debug = true debug = true
[profile.release] [profile.release]
opt-level = 3 opt-level = 2
debug = false debug = false
strip = true strip = true

View File

@ -1,4 +1,11 @@
use crate::Error; use crate::{
Error,
utils::{
format_duration,
concat_message,
BOT_VERSION
}
};
use sysinfo::System; use sysinfo::System;
use uptime_lib::get; use uptime_lib::get;
@ -11,6 +18,7 @@ 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();
@ -26,27 +34,12 @@ pub async fn uptime(ctx: poise::Context<'_, (), Error>) -> Result<(), Error> {
proc_uptime = now.duration_since(time_started).unwrap().as_secs(); 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(()) 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
}

View File

@ -1,7 +1,11 @@
mod commands; mod commands;
mod utils;
use std::env::var;
use poise::serenity_prelude::{self as serenity}; use poise::serenity_prelude::{self as serenity};
use std::{
env::var,
error
};
use serenity::{ use serenity::{
builder::{ builder::{
CreateMessage, CreateMessage,
@ -14,9 +18,8 @@ use serenity::{
GatewayIntents GatewayIntents
}; };
type Error = Box<dyn std::error::Error + Send + Sync>; type Error = Box<dyn error::Error + Send + Sync>;
pub static EMBED_COLOR: i32 = 0xf1d63c;
static BOT_READY_NOTIFY: u64 = 865673694184996888; static BOT_READY_NOTIFY: u64 = 865673694184996888;
async fn on_ready( async fn on_ready(
@ -28,7 +31,7 @@ async fn on_ready(
let message = CreateMessage::new(); let message = CreateMessage::new();
let ready_embed = CreateEmbed::new() let ready_embed = CreateEmbed::new()
.color(EMBED_COLOR) .color(utils::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)).clone());

46
src/utils.rs Normal file
View File

@ -0,0 +1,46 @@
use once_cell::sync::Lazy;
pub static EMBED_COLOR: i32 = 0xf1d63c;
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 {
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),
}
} */

19
tomlfmt.toml Normal file
View File

@ -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