reduced ai timeout, impl uptime

This commit is contained in:
2024-08-04 21:26:45 +02:00
parent d0af34833b
commit f224ccfbdf
6 changed files with 52 additions and 28 deletions

View File

@@ -10,7 +10,7 @@ pub mod posix;
pub mod qr;
pub mod register;
pub mod taf;
// pub mod uptime;
pub mod uptime;
pub mod verse;
pub mod weather;
@@ -26,6 +26,6 @@ pub use posix::posix;
pub use qr::qr;
pub use register::register;
pub use taf::taf;
// pub use uptime::uptime;
pub use uptime::uptime;
pub use verse::verse;
pub use weather::weather;

View File

@@ -35,7 +35,7 @@ pub async fn ai(
response = rng.gen_range(0..iamsorry.len());
};
sleep(Duration::from_secs(3));
sleep(Duration::from_secs(1));
ctx.send(
CreateReply::default().embed(

View File

@@ -1,35 +1,48 @@
use once_cell::sync::Lazy;
use poise::CreateReply;
use std::sync::Mutex;
use crate::{commands::embeds::embed, Context, Error};
// Currently unable to get information on how long the thread was running.
const PROCESS_UPTIME: i64 = 1000;
pub static PROCESS_UPTIME: Lazy<Mutex<std::time::SystemTime>> =
Lazy::new(|| Mutex::new(std::time::SystemTime::now()));
/// Checks how long the bot has been running
#[poise::command(prefix_command, slash_command, category = "Tools")]
pub async fn uptime(ctx: Context<'_>) -> Result<(), Error> {
let uptime = PROCESS_UPTIME;
let days = uptime / (24 * 60 * 60);
let hours = (uptime % (24 * 60 * 60)) / 3600;
let minutes = (uptime % 60 * 60) / 60;
let seconds = uptime % 60;
let start = PROCESS_UPTIME.lock().unwrap().clone();
let uptime = std::time::SystemTime::now().duration_since(start).unwrap();
ctx.send(
CreateReply::default().embed(
embed(
ctx,
"I have been up and awake for",
&format!("{} seconds", uptime),
&format!(
"{} days, {} hours, {} minutes and {} seconds",
days, hours, minutes, seconds
),
)
.await
.unwrap(),
),
)
.await?;
let (days, hours, minutes, seconds) = (
uptime.as_secs() / 86400,
(uptime.as_secs() / 3600) % 24,
(uptime.as_secs() / 60) % 60,
uptime.as_secs() % 60,
);
let mut message = format!(
"I have been awake for {} days, {} hours, {} minutes and {} seconds!",
days, hours, minutes, seconds
);
if days != 0 {
message = format!("I have been awake for {} days!", days);
}
if days == 0 && hours != 0 {
message = format!("I have been awake for {} hours!", hours);
}
if days == 0 && hours == 0 && minutes != 0 {
message = format!("I have been awake for {} minutes!", minutes);
}
if days == 0 && hours == 0 && minutes == 0 && seconds != 0 {
message = format!("I have been awake for {} seconds!", seconds);
}
ctx.send(CreateReply::default().embed(embed(ctx, "Uptime", "", &message).await.unwrap()))
.await?;
Ok(())
}