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, framework::standard::CommandResult,
}; };
use crate::THEME_COLOR; use chrono_tz::Tz;
use chrono::{
DateTime,
offset::Utc,
};
use crate::{
THEME_COLOR,
SQLPool,
models::UserData,
};
#[command] #[command]
@ -52,3 +63,19 @@ async fn donate(ctx: &Context, msg: &Message, _args: String) -> CommandResult {
Ok(()) 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 regex::Regex;
use chrono_tz::Tz;
use crate::{ use crate::{
models::{ models::{
ChannelData, ChannelData,
@ -60,7 +62,33 @@ async fn timezone(ctx: &Context, msg: &Message, args: String) -> CommandResult {
let pool = ctx.data.read().await let pool = ctx.data.read().await
.get::<SQLPool>().cloned().expect("Could not get SQLPool from data"); .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(()) Ok(())
} }

View File

@ -60,8 +60,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
.add_command("help", &info_cmds::HELP_COMMAND) .add_command("help", &info_cmds::HELP_COMMAND)
.add_command("info", &info_cmds::INFO_COMMAND) .add_command("info", &info_cmds::INFO_COMMAND)
.add_command("donate", &info_cmds::DONATE_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("todo", &todo_cmds::TODO_PARSE_COMMAND)
.add_command("blacklist", &moderation_cmds::BLACKLIST_COMMAND) .add_command("blacklist", &moderation_cmds::BLACKLIST_COMMAND)
.add_command("timezone", &moderation_cmds::TIMEZONE_COMMAND)
.build(); .build();
let mut client = Client::new(&env::var("DISCORD_TOKEN").expect("Missing DISCORD_TOKEN from environment")) let mut client = Client::new(&env::var("DISCORD_TOKEN").expect("Missing DISCORD_TOKEN from environment"))