Extract trait
This commit is contained in:
@ -1,84 +1,27 @@
|
||||
use chrono_tz::Tz;
|
||||
use poise::serenity_prelude::{model::id::GuildId, ResolvedValue};
|
||||
use poise::serenity_prelude::model::id::GuildId;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::{
|
||||
commands::remind::RemindOptions, models::reminder::create_reminder, ApplicationContext,
|
||||
Context, Error,
|
||||
};
|
||||
use crate::{commands::remind, utils::Extract, ApplicationContext, Context, Error};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[serde(tag = "command_name")]
|
||||
pub enum RecordedCommand {
|
||||
Remind(RemindOptions),
|
||||
}
|
||||
|
||||
macro_rules! extract_arg {
|
||||
($ctx:ident, $name:literal, String) => {
|
||||
$ctx.args.iter().find(|opt| opt.name == $name).map(|opt| &opt.value).map_or_else(
|
||||
|| String::new(),
|
||||
|v| match v {
|
||||
ResolvedValue::String(s) => s.to_string(),
|
||||
_ => String::new(),
|
||||
},
|
||||
)
|
||||
};
|
||||
($ctx:ident, $name:literal, Option<String>) => {
|
||||
$ctx.args.iter().find(|opt| opt.name == $name).map(|opt| &opt.value).map(|v| match v {
|
||||
ResolvedValue::String(s) => s.to_string(),
|
||||
_ => String::new(),
|
||||
})
|
||||
};
|
||||
($ctx:ident, $name:literal, bool) => {
|
||||
$ctx.args.iter().find(|opt| opt.name == $name).map(|opt| &opt.value).map(|v| match v {
|
||||
ResolvedValue::Boolean(b) => b.to_owned(),
|
||||
_ => false,
|
||||
})
|
||||
};
|
||||
($ctx:ident, $name:literal, Option<Tz>) => {
|
||||
$ctx.args
|
||||
.iter()
|
||||
.find(|opt| opt.name == $name)
|
||||
.map(|opt| &opt.value)
|
||||
.map(|v| match v {
|
||||
ResolvedValue::String(s) => s.parse::<Tz>().ok(),
|
||||
_ => None,
|
||||
})
|
||||
.flatten()
|
||||
};
|
||||
Remind(remind::Options),
|
||||
}
|
||||
|
||||
impl RecordedCommand {
|
||||
pub fn from_context(ctx: ApplicationContext) -> Option<Self> {
|
||||
match ctx.command().identifying_name.as_str() {
|
||||
"remind" => Some(Self::Remind(RemindOptions {
|
||||
time: extract_arg!(ctx, "time", String),
|
||||
content: extract_arg!(ctx, "content", String),
|
||||
channels: extract_arg!(ctx, "channels", Option<String>),
|
||||
interval: extract_arg!(ctx, "interval", Option<String>),
|
||||
expires: extract_arg!(ctx, "expires", Option<String>),
|
||||
tts: extract_arg!(ctx, "tts", bool),
|
||||
timezone: extract_arg!(ctx, "timezone", Option<Tz>),
|
||||
})),
|
||||
"remind" => Some(Self::Remind(remind::Options::extract(ctx))),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn execute(self, ctx: ApplicationContext<'_>) -> Result<(), Error> {
|
||||
match self {
|
||||
RecordedCommand::Remind(command_options) => {
|
||||
create_reminder(
|
||||
Context::Application(ctx),
|
||||
command_options.time,
|
||||
command_options.content,
|
||||
command_options.channels,
|
||||
command_options.interval,
|
||||
command_options.expires,
|
||||
command_options.tts,
|
||||
command_options.timezone,
|
||||
)
|
||||
.await
|
||||
RecordedCommand::Remind(options) => {
|
||||
remind::remind(Context::Application(ctx), options).await
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -69,7 +69,9 @@ pub struct ReminderBuilder {
|
||||
impl ReminderBuilder {
|
||||
pub async fn build(self) -> Result<Reminder, ReminderError> {
|
||||
let queried_time = sqlx::query!(
|
||||
"SELECT DATE_ADD(?, INTERVAL (SELECT nudge FROM channels WHERE id = ?) SECOND) AS `utc_time`",
|
||||
"
|
||||
SELECT DATE_ADD(?, INTERVAL (SELECT nudge FROM channels WHERE id = ?) SECOND) AS `utc_time`
|
||||
",
|
||||
self.utc_time,
|
||||
self.channel,
|
||||
)
|
||||
@ -84,36 +86,24 @@ impl ReminderBuilder {
|
||||
} else {
|
||||
sqlx::query!(
|
||||
"
|
||||
INSERT INTO reminders (
|
||||
`uid`,
|
||||
`channel_id`,
|
||||
`utc_time`,
|
||||
`timezone`,
|
||||
`interval_seconds`,
|
||||
`interval_days`,
|
||||
`interval_months`,
|
||||
`expires`,
|
||||
`content`,
|
||||
`tts`,
|
||||
`attachment_name`,
|
||||
`attachment`,
|
||||
`set_by`
|
||||
) VALUES (
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?
|
||||
)
|
||||
",
|
||||
INSERT INTO reminders (
|
||||
`uid`,
|
||||
`channel_id`,
|
||||
`utc_time`,
|
||||
`timezone`,
|
||||
`interval_seconds`,
|
||||
`interval_days`,
|
||||
`interval_months`,
|
||||
`expires`,
|
||||
`content`,
|
||||
`tts`,
|
||||
`attachment_name`,
|
||||
`attachment`,
|
||||
`set_by`
|
||||
) VALUES (
|
||||
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
|
||||
)
|
||||
",
|
||||
self.uid,
|
||||
self.channel,
|
||||
utc_time,
|
||||
|
Reference in New Issue
Block a user