reminder-bot/src/models/command_macro.rs
2024-02-09 17:03:04 +00:00

81 lines
2.0 KiB
Rust

use chrono_tz::Tz;
use poise::serenity_prelude::model::id::GuildId;
use serde::{Deserialize, Serialize};
use crate::{ApplicationContext, Context};
#[derive(Serialize, Deserialize)]
#[serde(tag = "command_name")]
pub enum RecordedCommand {
Remind(RemindOptions),
}
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(),
channels: None,
interval: None,
expires: None,
tts: None,
timezone: None,
})),
_ => None,
}
}
pub async fn execute(&self, ctx: ApplicationContext<'_>) {
match self {
RecordedCommand::Remind(_) => {}
}
}
}
#[derive(Serialize, Deserialize, Default)]
pub struct RemindOptions {
time: String,
content: String,
channels: Option<String>,
interval: Option<String>,
expires: Option<String>,
tts: Option<bool>,
timezone: Option<Tz>,
}
pub struct CommandMacro {
pub guild_id: GuildId,
pub name: String,
pub description: Option<String>,
pub commands: Vec<RecordedCommand>,
}
pub async fn guild_command_macro(ctx: &Context<'_>, name: &str) -> Option<CommandMacro> {
let row = sqlx::query!(
"
SELECT m.id, m.name, m.description, m.commands
FROM macro m
INNER JOIN guilds g
ON g.id = m.guild_id
WHERE guild = ?
AND m.name = ?
",
ctx.guild_id().unwrap().get(),
name
)
.fetch_one(&ctx.data().database)
.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,
};
Some(command_macro)
}