Switch to jiff from time
This commit is contained in:
parent
0256329f65
commit
4314a24e78
9 changed files with 51 additions and 141 deletions
|
|
@ -12,8 +12,8 @@ use euphoxide::bot::commands::Commands;
|
|||
use euphoxide::bot::instance::{Event, ServerConfig};
|
||||
use euphoxide::bot::instances::Instances;
|
||||
use euphoxide::conn;
|
||||
use jiff::Timestamp;
|
||||
use log::error;
|
||||
use time::OffsetDateTime;
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
const HELP: &str = "I'm an example bot for https://github.com/Garmelon/euphoxide";
|
||||
|
|
@ -74,7 +74,7 @@ impl ClapCommand<Bot, conn::Error> for Test {
|
|||
|
||||
struct Bot {
|
||||
commands: Arc<Commands<Self, conn::Error>>,
|
||||
start_time: OffsetDateTime,
|
||||
start_time: Timestamp,
|
||||
stop: bool,
|
||||
}
|
||||
|
||||
|
|
@ -85,7 +85,7 @@ impl HasDescriptions for Bot {
|
|||
}
|
||||
|
||||
impl HasStartTime for Bot {
|
||||
fn start_time(&self) -> OffsetDateTime {
|
||||
fn start_time(&self) -> Timestamp {
|
||||
self.start_time
|
||||
}
|
||||
}
|
||||
|
|
@ -107,7 +107,7 @@ async fn main() {
|
|||
|
||||
let mut bot = Bot {
|
||||
commands: cmds.clone(),
|
||||
start_time: OffsetDateTime::now_utc(),
|
||||
start_time: Timestamp::now(),
|
||||
stop: false,
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
|
|
|
|||
|
|
@ -3,47 +3,15 @@
|
|||
|
||||
use euphoxide::api::packet::ParsedPacket;
|
||||
use euphoxide::api::{Data, Nick, Send};
|
||||
use euphoxide::bot::botrulez;
|
||||
use euphoxide::bot::instance::{ConnSnapshot, Event, ServerConfig};
|
||||
use euphoxide::bot::instances::Instances;
|
||||
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,
|
||||
|
|
@ -108,8 +76,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());
|
||||
|
|
|
|||
|
|
@ -6,8 +6,9 @@ use std::time::Duration;
|
|||
|
||||
use euphoxide::api::packet::ParsedPacket;
|
||||
use euphoxide::api::{Data, Nick, Send};
|
||||
use euphoxide::bot::botrulez;
|
||||
use euphoxide::conn::{Conn, ConnTx, State};
|
||||
use time::OffsetDateTime;
|
||||
use jiff::Timestamp;
|
||||
|
||||
const TIMEOUT: Duration = Duration::from_secs(10);
|
||||
const DOMAIN: &str = "euphoria.leet.nu";
|
||||
|
|
@ -15,39 +16,6 @@ const ROOM: &str = "test";
|
|||
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, conn_tx: &ConnTx, state: &State) -> Result<(), ()> {
|
||||
let data = match packet.content {
|
||||
Ok(data) => data,
|
||||
|
|
@ -112,8 +80,11 @@ async fn on_packet(packet: ParsedPacket, conn_tx: &ConnTx, state: &State) -> Res
|
|||
reply = Some(HELP.to_string());
|
||||
} else if content == format!("!uptime @{NICK}") {
|
||||
if let Some(joined) = 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());
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue