2024-03-13 20:27:16 +11:00
|
|
|
use crate::{
|
|
|
|
Error,
|
2024-08-09 18:19:59 -04:00
|
|
|
GIT_COMMIT_HASH,
|
2024-03-16 09:47:21 +11:00
|
|
|
internals::utils::{
|
2024-08-09 18:19:59 -04:00
|
|
|
BOT_VERSION,
|
2024-03-13 20:27:16 +11:00
|
|
|
format_duration,
|
2024-08-09 18:19:59 -04:00
|
|
|
concat_message
|
2024-03-13 20:27:16 +11:00
|
|
|
}
|
|
|
|
};
|
2024-01-07 14:43:10 +11:00
|
|
|
|
|
|
|
use sysinfo::System;
|
|
|
|
use uptime_lib::get;
|
2024-08-09 18:19:59 -04:00
|
|
|
use std::{
|
|
|
|
fs::File,
|
|
|
|
path::Path,
|
|
|
|
time::{
|
|
|
|
Duration,
|
|
|
|
SystemTime,
|
|
|
|
UNIX_EPOCH
|
|
|
|
},
|
|
|
|
io::{
|
|
|
|
BufRead,
|
|
|
|
BufReader
|
|
|
|
}
|
2024-01-07 14:43:10 +11:00
|
|
|
};
|
|
|
|
|
2024-08-09 18:19:59 -04:00
|
|
|
fn get_os_info() -> String {
|
|
|
|
let path = Path::new("/etc/os-release");
|
|
|
|
let mut name = "BoringOS".to_string();
|
|
|
|
let mut version = "v0.0".to_string();
|
|
|
|
|
|
|
|
if let Ok(file) = File::open(&path) {
|
|
|
|
let reader = BufReader::new(file);
|
|
|
|
for line in reader.lines() {
|
|
|
|
if let Ok(line) = line {
|
|
|
|
if line.starts_with("NAME=") {
|
|
|
|
name = line.split('=').nth(1).unwrap_or_default().trim_matches('"').to_string();
|
|
|
|
} else if line.starts_with("VERSION=") {
|
|
|
|
version = line.split('=').nth(1).unwrap_or_default().trim_matches('"').to_string();
|
|
|
|
} else if line.starts_with("VERSION_ID=") {
|
|
|
|
version = line.split('=').nth(1).unwrap_or_default().trim_matches('"').to_string();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
format!("{} {}", name, version)
|
|
|
|
}
|
|
|
|
|
2024-01-07 14:43:10 +11:00
|
|
|
/// Retrieve host and bot uptimes
|
|
|
|
#[poise::command(slash_command)]
|
|
|
|
pub async fn uptime(ctx: poise::Context<'_, (), Error>) -> Result<(), Error> {
|
2024-03-13 20:27:16 +11:00
|
|
|
let _bot = ctx.http().get_current_user().await.unwrap();
|
2024-01-07 14:43:10 +11:00
|
|
|
let mut sys = System::new_all();
|
|
|
|
sys.refresh_all();
|
|
|
|
|
|
|
|
// Fetch system's uptime
|
|
|
|
let sys_uptime = get().unwrap().as_secs();
|
|
|
|
|
2024-08-09 18:19:59 -04:00
|
|
|
// Fetch system's processor
|
|
|
|
let cpu = sys.cpus();
|
|
|
|
|
2024-01-07 14:43:10 +11:00
|
|
|
// 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 20:27:16 +11:00
|
|
|
let stat_msg = vec![
|
2024-08-09 18:19:59 -04:00
|
|
|
format!("**{} {}** `{}`", _bot.name, BOT_VERSION.as_str(), GIT_COMMIT_HASH),
|
2024-03-13 20:27:16 +11:00
|
|
|
format!(">>> System: `{}`", format_duration(sys_uptime)),
|
2024-08-09 18:19:59 -04:00
|
|
|
format!("Process: `{}`", format_duration(proc_uptime)),
|
|
|
|
format!("CPU: `{}`", format!("{}", cpu[0].brand())),
|
|
|
|
format!("OS: `{}`", get_os_info())
|
2024-03-13 20:27:16 +11:00
|
|
|
];
|
|
|
|
ctx.reply(concat_message(stat_msg)).await?;
|
2024-01-07 14:43:10 +11:00
|
|
|
|
2024-03-13 20:27:16 +11:00
|
|
|
Ok(())
|
2024-01-07 14:43:10 +11:00
|
|
|
}
|