Rustbot/src/commands/uptime.rs

46 lines
1.1 KiB
Rust
Raw Normal View History

2024-03-13 05:27:16 -04:00
use crate::{
Error,
utils::{
format_duration,
concat_message,
BOT_VERSION
}
};
2024-01-06 22:43:10 -05:00
use sysinfo::System;
use uptime_lib::get;
use std::time::{
Duration,
SystemTime,
UNIX_EPOCH
};
/// Retrieve host and bot uptimes
#[poise::command(slash_command)]
pub async fn uptime(ctx: poise::Context<'_, (), Error>) -> Result<(), Error> {
2024-03-13 05:27:16 -04:00
let _bot = ctx.http().get_current_user().await.unwrap();
2024-01-06 22:43:10 -05:00
let mut sys = System::new_all();
sys.refresh_all();
// Fetch system's uptime
let sys_uptime = get().unwrap().as_secs();
// Fetch bot's process uptime
let curr_pid = sysinfo::get_current_pid().unwrap();
let now = SystemTime::now();
let mut proc_uptime = 0;
if let Some(process) = sys.process(curr_pid) {
let time_started = UNIX_EPOCH + Duration::from_secs(process.start_time());
proc_uptime = now.duration_since(time_started).unwrap().as_secs();
}
2024-03-13 05:27:16 -04:00
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?;
2024-01-06 22:43:10 -05:00
2024-03-13 05:27:16 -04:00
Ok(())
2024-01-06 22:43:10 -05:00
}