Switch to jiff from time

This commit is contained in:
Joscha 2024-12-04 17:08:52 +01:00
parent 0256329f65
commit 4314a24e78
9 changed files with 51 additions and 141 deletions

View file

@ -3,46 +3,14 @@
use euphoxide::api::packet::ParsedPacket;
use euphoxide::api::{Data, Nick, Send};
use euphoxide::bot::botrulez;
use euphoxide::bot::instance::{ConnSnapshot, Event, ServerConfig};
use time::OffsetDateTime;
use jiff::Timestamp;
use tokio::sync::mpsc;
const NICK: &str = "TestBot";
const HELP: &str = "I'm an example bot for https://github.com/Garmelon/euphoxide";
fn format_delta(delta: time::Duration) -> String {
const MINUTE: u64 = 60;
const HOUR: u64 = MINUTE * 60;
const DAY: u64 = HOUR * 24;
let mut seconds: u64 = delta.whole_seconds().try_into().unwrap();
let mut parts = vec![];
let days = seconds / DAY;
if days > 0 {
parts.push(format!("{days}d"));
seconds -= days * DAY;
}
let hours = seconds / HOUR;
if hours > 0 {
parts.push(format!("{hours}h"));
seconds -= hours * HOUR;
}
let mins = seconds / MINUTE;
if mins > 0 {
parts.push(format!("{mins}m"));
seconds -= mins * MINUTE;
}
if parts.is_empty() || seconds > 0 {
parts.push(format!("{seconds}s"));
}
parts.join(" ")
}
async fn on_packet(packet: ParsedPacket, snapshot: ConnSnapshot) -> Result<(), ()> {
let data = match packet.content {
Ok(data) => data,
@ -107,8 +75,11 @@ async fn on_packet(packet: ParsedPacket, snapshot: ConnSnapshot) -> Result<(), (
reply = Some(HELP.to_string());
} else if content == format!("!uptime @{NICK}") {
if let Some(joined) = snapshot.state.joined() {
let delta = OffsetDateTime::now_utc() - joined.since;
reply = Some(format!("/me has been up for {}", format_delta(delta)));
let delta = Timestamp::now() - joined.since;
reply = Some(format!(
"/me has been up for {}",
botrulez::format_duration(delta)
));
}
} else if content == "!test" {
reply = Some("Test successful!".to_string());