offset command. fix for timeparser not doing negatives properly on specified fields
This commit is contained in:
parent
9e737dc9d6
commit
4bd24ff94c
@ -13,6 +13,7 @@ use serenity::{
|
|||||||
use crate::{
|
use crate::{
|
||||||
models::{
|
models::{
|
||||||
ChannelData,
|
ChannelData,
|
||||||
|
GuildData,
|
||||||
UserData,
|
UserData,
|
||||||
},
|
},
|
||||||
SQLPool,
|
SQLPool,
|
||||||
@ -65,3 +66,53 @@ async fn pause(ctx: &Context, msg: &Message, args: String) -> CommandResult {
|
|||||||
|
|
||||||
Ok(())
|
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(())
|
||||||
|
}
|
||||||
|
@ -63,12 +63,16 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
|||||||
.add_command("donate", &info_cmds::DONATE_COMMAND)
|
.add_command("donate", &info_cmds::DONATE_COMMAND)
|
||||||
.add_command("dashboard", &info_cmds::DASHBOARD_COMMAND)
|
.add_command("dashboard", &info_cmds::DASHBOARD_COMMAND)
|
||||||
.add_command("clock", &info_cmds::CLOCK_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)
|
.add_command("timezone", &moderation_cmds::TIMEZONE_COMMAND)
|
||||||
.add_command("prefix", &moderation_cmds::PREFIX_COMMAND)
|
.add_command("prefix", &moderation_cmds::PREFIX_COMMAND)
|
||||||
.add_command("lang", &moderation_cmds::LANGUAGE_COMMAND)
|
.add_command("lang", &moderation_cmds::LANGUAGE_COMMAND)
|
||||||
|
|
||||||
.add_command("pause", &reminder_cmds::PAUSE_COMMAND)
|
.add_command("pause", &reminder_cmds::PAUSE_COMMAND)
|
||||||
|
.add_command("offset", &reminder_cmds::OFFSET_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"))
|
||||||
|
@ -10,8 +10,10 @@ use serenity::{
|
|||||||
use sqlx::MySqlPool;
|
use sqlx::MySqlPool;
|
||||||
use chrono::NaiveDateTime;
|
use chrono::NaiveDateTime;
|
||||||
|
|
||||||
|
use chrono_tz::Tz;
|
||||||
|
|
||||||
pub struct GuildData {
|
pub struct GuildData {
|
||||||
id: u32,
|
pub id: u32,
|
||||||
guild: u64,
|
guild: u64,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub prefix: 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")
|
row.value.expect("No string with that name")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn timezone(&self) -> Tz {
|
||||||
|
self.timezone.parse().unwrap()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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 };
|
if self.inverted { -1 } else { 1 };
|
||||||
|
|
||||||
Ok(full)
|
Ok(full)
|
||||||
|
Loading…
Reference in New Issue
Block a user