Record some parameters for /remind

This commit is contained in:
jude 2024-02-15 17:28:43 +00:00
parent 2f6d035efe
commit b81c3c80c1
4 changed files with 64 additions and 20 deletions

View File

@ -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?;
}
}

View File

@ -627,7 +627,7 @@ pub async fn remind(
.await
}
async fn create_reminder(
pub async fn create_reminder(
ctx: Context<'_>,
time: String,
content: String,

View File

@ -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;
}

View File

@ -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<Self> {
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<CommandMacro> {
let row = sqlx::query!(
struct Row {
name: String,
description: Option<String>,
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<Comman
.await
.ok()?;
let commands: Vec<RecordedCommand> = 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)