offset command. fix for timeparser not doing negatives properly on specified fields

This commit is contained in:
jude 2020-09-01 18:18:45 +01:00
parent 9e737dc9d6
commit 4bd24ff94c
4 changed files with 63 additions and 2 deletions

View File

@ -13,6 +13,7 @@ use serenity::{
use crate::{
models::{
ChannelData,
GuildData,
UserData,
},
SQLPool,
@ -65,3 +66,53 @@ async fn pause(ctx: &Context, msg: &Message, args: String) -> CommandResult {
Ok(())
}
#[command]
async fn offset(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();
if args.len() == 0 {
let _ = msg.channel_id.say(&ctx, user_data.response(&pool, "offset/help").await).await;
}
else {
let parser = TimeParser::new(args, user_data.timezone());
if let Ok(displacement) = parser.displacement() {
if let Some(guild) = msg.guild(&ctx).await {
let guild_data = GuildData::from_guild(guild, &pool).await.unwrap();
sqlx::query!(
"
UPDATE reminders
INNER JOIN `channels`
ON `channels`.id = reminders.channel_id
SET
reminders.`time` = reminders.`time` + ?
WHERE channels.guild_id = ?
", displacement, guild_data.id)
.execute(&pool)
.await
.unwrap();
} else {
sqlx::query!(
"
UPDATE reminders SET `time` = `time` + ? WHERE reminders.channel_id = ?
", displacement, user_data.dm_channel)
.execute(&pool)
.await
.unwrap();
}
let response = user_data.response(&pool, "offset/success").await.replacen("{}", &displacement.to_string(), 1);
let _ = msg.channel_id.say(&ctx, response).await;
} else {
let _ = msg.channel_id.say(&ctx, user_data.response(&pool, "offset/invalid_time").await).await;
}
}
Ok(())
}

View File

@ -63,12 +63,16 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
.add_command("donate", &info_cmds::DONATE_COMMAND)
.add_command("dashboard", &info_cmds::DASHBOARD_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)
.add_command("prefix", &moderation_cmds::PREFIX_COMMAND)
.add_command("lang", &moderation_cmds::LANGUAGE_COMMAND)
.add_command("pause", &reminder_cmds::PAUSE_COMMAND)
.add_command("offset", &reminder_cmds::OFFSET_COMMAND)
.build();
let mut client = Client::new(&env::var("DISCORD_TOKEN").expect("Missing DISCORD_TOKEN from environment"))

View File

@ -10,8 +10,10 @@ use serenity::{
use sqlx::MySqlPool;
use chrono::NaiveDateTime;
use chrono_tz::Tz;
pub struct GuildData {
id: u32,
pub id: u32,
guild: u64,
pub name: String,
pub prefix: String,
@ -198,4 +200,8 @@ SELECT value FROM strings WHERE (language = ? OR language = 'EN') AND name = ? O
row.value.expect("No string with that name")
}
pub fn timezone(&self) -> Tz {
self.timezone.parse().unwrap()
}
}

View File

@ -172,7 +172,7 @@ impl TimeParser {
}
}
let full = seconds + (minutes * 60) + (hours * 3600) + (days * 86400) + current_buffer.parse::<i64>().unwrap() *
let full = (seconds + (minutes * 60) + (hours * 3600) + (days * 86400) + current_buffer.parse::<i64>().unwrap()) *
if self.inverted { -1 } else { 1 };
Ok(full)