From e88e4cc4f1ba40584a22d43edd8b43a2955d0ca5 Mon Sep 17 00:00:00 2001 From: jude Date: Thu, 27 Aug 2020 21:37:44 +0100 Subject: [PATCH] clock and timezone cmd --- src/commands/info_cmds.rs | 29 ++++++++++++++++++++++++++++- src/commands/moderation_cmds.rs | 30 +++++++++++++++++++++++++++++- src/main.rs | 2 ++ 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/src/commands/info_cmds.rs b/src/commands/info_cmds.rs index 7d2f93e..44f14c3 100644 --- a/src/commands/info_cmds.rs +++ b/src/commands/info_cmds.rs @@ -10,7 +10,18 @@ use serenity::{ framework::standard::CommandResult, }; -use crate::THEME_COLOR; +use chrono_tz::Tz; + +use chrono::{ + DateTime, + offset::Utc, +}; + +use crate::{ + THEME_COLOR, + SQLPool, + models::UserData, +}; #[command] @@ -52,3 +63,19 @@ async fn donate(ctx: &Context, msg: &Message, _args: String) -> CommandResult { Ok(()) } + +#[command] +async fn clock(ctx: &Context, msg: &Message, args: String) -> CommandResult { + let pool = ctx.data.read().await + .get::().cloned().expect("Could not get SQLPool from data"); + + let user_data = UserData::from_id(&msg.author, &ctx, pool).await.unwrap(); + + let tz: Tz = user_data.timezone.parse().unwrap(); + + let now = Utc::now().with_timezone(&tz); + + let _ = msg.channel_id.say(&ctx, format!("Current time: **{}**", now.format("%H:%M:%S"))).await; + + Ok(()) +} diff --git a/src/commands/moderation_cmds.rs b/src/commands/moderation_cmds.rs index afdaab7..d4612a7 100644 --- a/src/commands/moderation_cmds.rs +++ b/src/commands/moderation_cmds.rs @@ -12,6 +12,8 @@ use serenity::{ use regex::Regex; +use chrono_tz::Tz; + use crate::{ models::{ ChannelData, @@ -60,7 +62,33 @@ async fn timezone(ctx: &Context, msg: &Message, args: String) -> CommandResult { let pool = ctx.data.read().await .get::().cloned().expect("Could not get SQLPool from data"); - let user_data = UserData::from_id(&msg.author, &ctx, pool.clone()).await.unwrap(); + match args.parse::() { + Ok(_) => { + let mut user_data = UserData::from_id(&msg.author, &ctx, pool.clone()).await.unwrap(); + + user_data.timezone = args; + + user_data.commit_changes(pool).await; + + let _ = msg.channel_id.say(&ctx, "Timezone changed").await; + } + + Err(_) => { + let _ = msg.channel_id.say(&ctx, "Unrecognised timezone").await; + } + } + + Ok(()) +} + +#[command] +async fn language(ctx: &Context, msg: &Message, args: String) -> CommandResult { + let pool = ctx.data.read().await + .get::().cloned().expect("Could not get SQLPool from data"); + + let mut user_data = UserData::from_id(&msg.author, &ctx, pool.clone()).await.unwrap(); + + user_data.commit_changes(pool).await; Ok(()) } diff --git a/src/main.rs b/src/main.rs index d1c46dc..1478f3e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -60,8 +60,10 @@ async fn main() -> Result<(), Box> { .add_command("help", &info_cmds::HELP_COMMAND) .add_command("info", &info_cmds::INFO_COMMAND) .add_command("donate", &info_cmds::DONATE_COMMAND) + .add_command("clock", &info_cmds::CLOCK_COMMAND) .add_command("todo", &todo_cmds::TODO_PARSE_COMMAND) .add_command("blacklist", &moderation_cmds::BLACKLIST_COMMAND) + .add_command("timezone", &moderation_cmds::TIMEZONE_COMMAND) .build(); let mut client = Client::new(&env::var("DISCORD_TOKEN").expect("Missing DISCORD_TOKEN from environment"))