time parser struct
This commit is contained in:
@ -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(())
|
||||
}
|
||||
|
@ -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(())
|
||||
}
|
||||
|
@ -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(())
|
||||
}
|
||||
|
Reference in New Issue
Block a user