diff --git a/src/commands/command_macro/run.rs b/src/commands/command_macro/run.rs index 342b102..030ae16 100644 --- a/src/commands/command_macro/run.rs +++ b/src/commands/command_macro/run.rs @@ -32,7 +32,7 @@ pub async fn run_macro( .await?; for command in command_macro.commands { - command.execute(poise::ApplicationContext { ..ctx }).await; + command.execute(poise::ApplicationContext { ..ctx }).await?; } } diff --git a/src/commands/reminder_cmds.rs b/src/commands/reminder_cmds.rs index eb6976d..35608c4 100644 --- a/src/commands/reminder_cmds.rs +++ b/src/commands/reminder_cmds.rs @@ -627,7 +627,7 @@ pub async fn remind( .await } -async fn create_reminder( +pub async fn create_reminder( ctx: Context<'_>, time: String, content: String, diff --git a/src/hooks.rs b/src/hooks.rs index 50f690a..7ec0fc5 100644 --- a/src/hooks.rs +++ b/src/hooks.rs @@ -1,10 +1,13 @@ -use log::warn; -use poise::{serenity_prelude::model::channel::Channel, CreateReply}; +use poise::{serenity_prelude::model::channel::Channel, CommandInteractionType, CreateReply}; use crate::{consts::MACRO_MAX_COMMANDS, models::command_macro::RecordedCommand, Context, Error}; async fn macro_check(ctx: Context<'_>) -> bool { if let Context::Application(app_ctx) = ctx { + if app_ctx.interaction_type != CommandInteractionType::Command { + return true; + } + if let Some(guild_id) = ctx.guild_id() { if ctx.command().identifying_name != "finish_macro" { let mut lock = ctx.data().recording_macros.write().await; @@ -12,12 +15,12 @@ async fn macro_check(ctx: Context<'_>) -> bool { if let Some(command_macro) = lock.get_mut(&(guild_id, ctx.author().id)) { if ctx.command().identifying_name != "remind" { let _ = ctx - .send( - CreateReply::default() - .ephemeral(true) - .content("Macro recording only supports `/remind`. Please stop recording with `/macro finish` before using other commands.") - ) - .await; + .send( + CreateReply::default() + .ephemeral(true) + .content("Macro recording only supports `/remind`. Please stop recording with `/macro finish` before using other commands.") + ) + .await; return false; } diff --git a/src/models/command_macro.rs b/src/models/command_macro.rs index 08b0bbb..22d0dbd 100644 --- a/src/models/command_macro.rs +++ b/src/models/command_macro.rs @@ -1,8 +1,9 @@ use chrono_tz::Tz; -use poise::serenity_prelude::model::id::GuildId; +use poise::serenity_prelude::{model::id::GuildId, ResolvedValue}; use serde::{Deserialize, Serialize}; +use serde_json::Value; -use crate::{ApplicationContext, Context}; +use crate::{commands::reminder_cmds::create_reminder, ApplicationContext, Context, Error}; #[derive(Serialize, Deserialize)] #[serde(tag = "command_name")] @@ -14,8 +15,30 @@ impl RecordedCommand { pub fn from_context(ctx: ApplicationContext) -> Option { match ctx.command().identifying_name.as_str() { "remind" => Some(Self::Remind(RemindOptions { - time: "10 seconds".to_string(), - content: "message".to_string(), + time: ctx + .args + .iter() + .find(|opt| opt.name == "time") + .map(|opt| &opt.value) + .map_or_else( + || String::new(), + |v| match v { + ResolvedValue::String(s) => s.to_string(), + _ => String::new(), + }, + ), + content: ctx + .args + .iter() + .find(|opt| opt.name == "content") + .map(|opt| &opt.value) + .map_or_else( + || String::new(), + |v| match v { + ResolvedValue::String(s) => s.to_string(), + _ => String::new(), + }, + ), channels: None, interval: None, expires: None, @@ -26,9 +49,21 @@ impl RecordedCommand { } } - pub async fn execute(&self, ctx: ApplicationContext<'_>) { + pub async fn execute(&self, ctx: ApplicationContext<'_>) -> Result<(), Error> { match self { - RecordedCommand::Remind(_) => {} + RecordedCommand::Remind(command_options) => { + create_reminder( + Context::Application(ctx), + command_options.time.clone(), + command_options.content.clone(), + None, + None, + None, + None, + None, + ) + .await + } } } } @@ -52,9 +87,16 @@ pub struct CommandMacro { } pub async fn guild_command_macro(ctx: &Context<'_>, name: &str) -> Option { - let row = sqlx::query!( + struct Row { + name: String, + description: Option, + commands: Value, + } + + let row = sqlx::query_as!( + Row, " - SELECT m.id, m.name, m.description, m.commands + SELECT m.name, m.description, m.commands FROM command_macro m INNER JOIN guilds g ON g.id = m.guild_id @@ -68,12 +110,11 @@ pub async fn guild_command_macro(ctx: &Context<'_>, name: &str) -> Option = serde_json::from_str(&row.commands).unwrap(); let command_macro = CommandMacro { guild_id: ctx.guild_id().unwrap(), name: row.name, description: row.description, - commands, + commands: serde_json::from_value(row.commands).unwrap(), }; Some(command_macro)