clock and timezone cmd

This commit is contained in:
jude
2020-08-27 21:37:44 +01:00
parent 2cde012c15
commit e88e4cc4f1
3 changed files with 59 additions and 2 deletions

View File

@ -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::<SQLPool>().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(())
}

View File

@ -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::<SQLPool>().cloned().expect("Could not get SQLPool from data");
let user_data = UserData::from_id(&msg.author, &ctx, pool.clone()).await.unwrap();
match args.parse::<Tz>() {
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::<SQLPool>().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(())
}