Add time to date module
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
[package]
|
||||
name = "robbit"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
edition = "2024"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
|
||||
@@ -12,4 +12,4 @@ USER robbit
|
||||
|
||||
RUN cargo build -r
|
||||
|
||||
CMD ./target/release/robbit
|
||||
CMD ["./target/release/robbit"]
|
||||
|
||||
10
src/lib.rs
10
src/lib.rs
@@ -6,14 +6,14 @@ use regex::Regex;
|
||||
//is this the best way to do this? probably not
|
||||
mod modules;
|
||||
|
||||
use modules::{bully, lenny, join_rude, grass, noemo, ttb, help, repo,rtfm, kick, history};
|
||||
use modules::{bully, lenny, join_rude, grass, noemo, ttb, help, repo,rtfm, kick, history, time_to_date};
|
||||
|
||||
type ModuleFunc = fn(regex::Captures, &Message, &VecDeque<Message>)->String;
|
||||
const NUM_MODS:usize = 10;
|
||||
const NUM_MODS:usize = 11;
|
||||
|
||||
|
||||
const MODULES: [(&str, ModuleFunc);NUM_MODS] = [(lenny::PATTERN, lenny::mod_message), (bully::PATTERN, bully::mod_message), (grass::PATTERN, grass::touch_grass), (noemo::PATTERN, noemo::no_emo), (ttb::PATTERN, ttb::time_to_baby), (help::PATTERN, help::help), (repo::PATTERN, repo::link), (rtfm::PATTERN, rtfm::rtfm), (kick::PATTERN, kick::mod_message), (history::PATTERN, history::mod_message)];
|
||||
const MODULE_USAGE: [(&str, &str); NUM_MODS] = [(lenny::NAME, lenny::USAGE), (bully::NAME, bully::USAGE), (grass::NAME, grass::USAGE), (noemo::NAME, noemo::USAGE), (ttb::NAME, ttb::USAGE), (help::NAME, help::USAGE), (repo::NAME, repo::USAGE),(rtfm::NAME, rtfm::USAGE), (kick::NAME, kick::USAGE), (history::NAME, history::USAGE)];
|
||||
const MODULES: [(&str, ModuleFunc);NUM_MODS] = [(lenny::PATTERN, lenny::mod_message), (bully::PATTERN, bully::mod_message), (grass::PATTERN, grass::touch_grass), (noemo::PATTERN, noemo::no_emo), (ttb::PATTERN, ttb::time_to_baby), (help::PATTERN, help::help), (repo::PATTERN, repo::link), (rtfm::PATTERN, rtfm::rtfm), (kick::PATTERN, kick::mod_message), (history::PATTERN, history::mod_message), (time_to_date::PATTERN, time_to_date::time_to_date)];
|
||||
const MODULE_USAGE: [(&str, &str); NUM_MODS] = [(lenny::NAME, lenny::USAGE), (bully::NAME, bully::USAGE), (grass::NAME, grass::USAGE), (noemo::NAME, noemo::USAGE), (ttb::NAME, ttb::USAGE), (help::NAME, help::USAGE), (repo::NAME, repo::USAGE),(rtfm::NAME, rtfm::USAGE), (kick::NAME, kick::USAGE), (history::NAME, history::USAGE), (time_to_date::NAME, time_to_date::USAGE)];
|
||||
|
||||
pub fn build_modules() -> Result<Vec<(Regex, ModuleFunc)>, regex::Error> {
|
||||
let mut regex_array: Vec<(Regex, ModuleFunc)> = Vec::with_capacity(NUM_MODS);
|
||||
@@ -39,7 +39,7 @@ pub fn handle(modules: &Vec<(Regex, ModuleFunc)>, message: &Message, message_buf
|
||||
}
|
||||
}
|
||||
},
|
||||
JOIN(ref channel,_,_) => return kick::bad_user(message.source_nickname().unwrap_or("unknown user"), channel.as_str()),
|
||||
JOIN(channel,_,_) => return kick::bad_user(message.source_nickname().unwrap_or("unknown user"), channel.as_str()),
|
||||
_ => ()
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ use robbit::{build_modules, handle};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Error>{
|
||||
let max_len = 100;
|
||||
let max_len = 1000;
|
||||
|
||||
let config = Config::load("config.toml")?;
|
||||
let mut client = Client::from_config(config).await?;
|
||||
|
||||
@@ -9,3 +9,4 @@ pub mod repo;
|
||||
pub mod rtfm;
|
||||
pub mod kick;
|
||||
pub mod history;
|
||||
pub mod time_to_date;
|
||||
|
||||
110
src/modules/time_to_date.rs
Normal file
110
src/modules/time_to_date.rs
Normal file
@@ -0,0 +1,110 @@
|
||||
use irc::proto::Message;
|
||||
use std::collections::VecDeque;
|
||||
use std::time::Duration;
|
||||
use chrono::prelude::*;
|
||||
use humantime::{self, parse_rfc3339_weak};
|
||||
|
||||
pub const PATTERN: &str = "^\\$ttd\\s*(.*)$";
|
||||
pub const NAME: &str = "ttd";
|
||||
pub const USAGE: &str = "Usage $ttd <datetime>\r\nThis prints the amount of time until the specified datetime";
|
||||
|
||||
pub fn time_to_date(captures: regex::Captures, _: &Message, _: &VecDeque<Message>) -> String {
|
||||
let time_string = captures.get(1).unwrap().as_str();
|
||||
//Dates + Times (numeric)
|
||||
let requested_time = if let Ok(date_time) = NaiveDateTime::parse_from_str(time_string, "%D %R") {
|
||||
date_time
|
||||
}
|
||||
else if let Ok(date_time) = NaiveDateTime::parse_from_str(time_string, "%D %I:%M %P") {
|
||||
date_time
|
||||
}
|
||||
else if let Ok(date_time) = NaiveDateTime::parse_from_str(time_string, "%D %I:%M %p") {
|
||||
date_time
|
||||
}
|
||||
else if let Ok(date_time) = NaiveDateTime::parse_from_str(time_string, "%m/%d/%Y %R") {
|
||||
date_time
|
||||
}
|
||||
else if let Ok(date_time) = NaiveDateTime::parse_from_str(time_string, "%m/%d/%Y %I:%M %P") {
|
||||
date_time
|
||||
}
|
||||
else if let Ok(date_time) = NaiveDateTime::parse_from_str(time_string, "%m/%d/%Y %I:%M %p") {
|
||||
date_time
|
||||
}
|
||||
//Dates (numeric)
|
||||
else if let Ok(date) = NaiveDate::parse_from_str(time_string, "%D") {
|
||||
NaiveDateTime::from(date)
|
||||
}
|
||||
else if let Ok(date) = NaiveDate::parse_from_str(time_string, "%m/%d/%Y") {
|
||||
NaiveDateTime::from(date)
|
||||
}
|
||||
else if let Ok(date) = NaiveDate::parse_from_str(time_string, "%F") {
|
||||
NaiveDateTime::from(date)
|
||||
}
|
||||
//Time (24hour)
|
||||
else if let Ok(time) = NaiveTime::parse_from_str(time_string, "%R") {
|
||||
Local::now().naive_local().date().and_time(time)
|
||||
}
|
||||
//Time (12hour)
|
||||
else if let Ok(time) = NaiveTime::parse_from_str(time_string, "%I:%M %P") {
|
||||
Local::now().naive_local().date().and_time(time)
|
||||
}
|
||||
else if let Ok(time) = NaiveTime::parse_from_str(time_string, "%I:%M %p") {
|
||||
Local::now().naive_local().date().and_time(time)
|
||||
}
|
||||
//Date + Time (written)
|
||||
else if let Ok(date_time) = NaiveDateTime::parse_from_str(time_string, "%v %R") {
|
||||
date_time
|
||||
}
|
||||
else if let Ok(date_time) = NaiveDateTime::parse_from_str(time_string, "%v %I:%M %P") {
|
||||
date_time
|
||||
}
|
||||
else if let Ok(date_time) = NaiveDateTime::parse_from_str(time_string, "%v %I:%M %p") {
|
||||
date_time
|
||||
}
|
||||
else if let Ok(date_time) = NaiveDateTime::parse_from_str(time_string, "%B %d, %y %R") {
|
||||
date_time
|
||||
}
|
||||
else if let Ok(date_time) = NaiveDateTime::parse_from_str(time_string, "%B %d, %y %I:%M %p") {
|
||||
date_time
|
||||
}
|
||||
else if let Ok(date_time) = NaiveDateTime::parse_from_str(time_string, "%B %d, %y %I:%M %P") {
|
||||
date_time
|
||||
}
|
||||
else if let Ok(date_time) = NaiveDateTime::parse_from_str(time_string, "%B %d, %Y %R") {
|
||||
date_time
|
||||
}
|
||||
else if let Ok(date_time) = NaiveDateTime::parse_from_str(time_string, "%B %d, %Y %I:%M %p") {
|
||||
date_time
|
||||
}
|
||||
else if let Ok(date_time) = NaiveDateTime::parse_from_str(time_string, "%B %d, %Y %I:%M %P") {
|
||||
date_time
|
||||
}
|
||||
//Date (written)
|
||||
else if let Ok(date) = NaiveDate::parse_from_str(time_string, "%v") {
|
||||
NaiveDateTime::from(date)
|
||||
}
|
||||
else if let Ok(date) = NaiveDate::parse_from_str(time_string, "%B %d, %y") {
|
||||
NaiveDateTime::from(date)
|
||||
}
|
||||
else if let Ok(date) = NaiveDate::parse_from_str(time_string, "%B %d, %Y") {
|
||||
NaiveDateTime::from(date)
|
||||
}
|
||||
//RFC3339 datetime
|
||||
else if let Ok(date_time) = parse_rfc3339_weak(time_string) {
|
||||
let tmp: DateTime<Local> = date_time.into();
|
||||
tmp.naive_local()
|
||||
}
|
||||
else {
|
||||
return "Invalid date given".to_string();
|
||||
};
|
||||
|
||||
let current_time = Local::now().naive_local();
|
||||
|
||||
let difference = requested_time - current_time;
|
||||
let human_difference = humantime::format_duration(Duration::from_secs(difference.num_seconds().abs() as u64));
|
||||
if difference.num_seconds() < 0 {
|
||||
format!("Was {} ago",human_difference.to_string())
|
||||
}
|
||||
else {
|
||||
format!("Is in {}", human_difference.to_string())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user