time parser struct

This commit is contained in:
jude
2020-09-01 15:34:50 +01:00
parent c1a4092e3c
commit 74617d50a5
5 changed files with 166 additions and 42 deletions

View File

@ -72,7 +72,12 @@ async fn clock(ctx: &Context, msg: &Message, args: String) -> CommandResult {
let now = Utc::now().with_timezone(&tz);
let _ = msg.channel_id.say(&ctx, format!("Current time: **{}**", now.format("%H:%M:%S"))).await;
if args == "12".to_string() {
let _ = msg.channel_id.say(&ctx, format!("Current time: **{}**", now.format("%I:%M:%S %p"))).await;
}
else {
let _ = msg.channel_id.say(&ctx, format!("Current time: **{}**", now.format("%H:%M:%S"))).await;
}
Ok(())
}

View File

@ -22,6 +22,7 @@ use crate::{
},
SQLPool,
framework::SendFromDb,
time_parser::TimeParser,
};
lazy_static! {
@ -143,15 +144,3 @@ async fn prefix(ctx: &Context, msg: &Message, args: String) -> CommandResult {
Ok(())
}
#[command]
async fn pause(ctx: &Context, msg: &Message, args: String) -> CommandResult {
let pool = ctx.data.read().await
.get::<SQLPool>().cloned().expect("Could not get SQLPool from data");
let channel = ChannelData::from_channel(msg.channel(&ctx).await.unwrap(), pool.clone()).await.unwrap();
channel.commit_changes(pool).await;
Ok(())
}

View File

@ -0,0 +1,73 @@
use regex_command_attr::command;
use serenity::{
client::Context,
model::{
channel::{
Message,
},
},
framework::standard::CommandResult,
};
use chrono_tz::{
Tz,
Etc::UTC,
};
use crate::{
models::{
ChannelData,
UserData,
},
SQLPool,
framework::SendFromDb,
time_parser::TimeParser,
};
use chrono::NaiveDateTime;
#[command]
async fn pause(ctx: &Context, msg: &Message, args: String) -> CommandResult {
let pool = ctx.data.read().await
.get::<SQLPool>().cloned().expect("Could not get SQLPool from data");
let user_data = UserData::from_id(&msg.author, &ctx, pool.clone()).await.unwrap();
let mut channel = ChannelData::from_channel(msg.channel(&ctx).await.unwrap(), pool.clone()).await.unwrap();
if args.len() == 0 {
channel.paused = !channel.paused;
channel.paused_until = None;
channel.commit_changes(pool).await;
if channel.paused {
let _ = msg.channel_id.say_named(&ctx, user_data.language, "paused/paused_indefinite").await;
}
else {
let _ = msg.channel_id.say_named(&ctx, user_data.language, "paused/unpaused").await;
}
}
else {
let parser = TimeParser::new(args, user_data.timezone.parse().unwrap());
let pause_until = parser.timestamp();
match pause_until {
Ok(timestamp) => {
channel.paused = true;
channel.paused_until = Some(NaiveDateTime::from_timestamp(timestamp, 0));
channel.commit_changes(pool).await;
let _ = msg.channel_id.say_named(&ctx, user_data.language, "paused/paused_until").await;
},
Err(_) => {
let _ = msg.channel_id.say_named(&ctx, user_data.language, "paused/invalid_time").await;
},
}
}
Ok(())
}