Refactor macros
This commit is contained in:
@ -1,47 +1,66 @@
|
||||
use poise::serenity_prelude::{model::id::GuildId, CommandDataOption};
|
||||
use chrono_tz::Tz;
|
||||
use poise::serenity_prelude::model::id::GuildId;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::{Context, Data, Error};
|
||||
|
||||
type Func<U, E> = for<'a> fn(
|
||||
poise::ApplicationContext<'a, U, E>,
|
||||
) -> poise::BoxFuture<'a, Result<(), poise::FrameworkError<'a, U, E>>>;
|
||||
|
||||
fn default_none<U, E>() -> Option<Func<U, E>> {
|
||||
None
|
||||
}
|
||||
use crate::{ApplicationContext, Context};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct RecordedCommand<U, E> {
|
||||
#[serde(skip)]
|
||||
#[serde(default = "default_none::<U, E>")]
|
||||
pub action: Option<Func<U, E>>,
|
||||
pub command_name: String,
|
||||
pub options: Vec<CommandDataOption>,
|
||||
#[serde(tag = "command_name")]
|
||||
pub enum RecordedCommand {
|
||||
Remind(RemindOptions),
|
||||
}
|
||||
|
||||
pub struct CommandMacro<U, E> {
|
||||
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<U, E>>,
|
||||
pub commands: Vec<RecordedCommand>,
|
||||
}
|
||||
|
||||
pub struct RawCommandMacro {
|
||||
pub guild_id: GuildId,
|
||||
pub name: String,
|
||||
pub description: Option<String>,
|
||||
pub commands: Value,
|
||||
}
|
||||
|
||||
pub async fn guild_command_macro(
|
||||
ctx: &Context<'_>,
|
||||
name: &str,
|
||||
) -> Option<CommandMacro<Data, Error>> {
|
||||
pub async fn guild_command_macro(ctx: &Context<'_>, name: &str) -> Option<CommandMacro> {
|
||||
let row = sqlx::query!(
|
||||
"
|
||||
SELECT * FROM macro WHERE guild_id = (SELECT id FROM guilds WHERE guild = ?) AND name = ?
|
||||
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
|
||||
@ -50,20 +69,7 @@ SELECT * FROM macro WHERE guild_id = (SELECT id FROM guilds WHERE guild = ?) AND
|
||||
.await
|
||||
.ok()?;
|
||||
|
||||
let mut commands: Vec<RecordedCommand<Data, Error>> =
|
||||
serde_json::from_str(&row.commands).unwrap();
|
||||
|
||||
for recorded_command in &mut commands {
|
||||
let command = &ctx
|
||||
.framework()
|
||||
.options()
|
||||
.commands
|
||||
.iter()
|
||||
.find(|c| c.identifying_name == recorded_command.command_name);
|
||||
|
||||
recorded_command.action = command.map(|c| c.slash_action).flatten();
|
||||
}
|
||||
|
||||
let commands: Vec<RecordedCommand> = serde_json::from_str(&row.commands).unwrap();
|
||||
let command_macro = CommandMacro {
|
||||
guild_id: ctx.guild_id().unwrap(),
|
||||
name: row.name,
|
||||
|
@ -25,7 +25,7 @@ pub trait CtxData {
|
||||
|
||||
async fn channel_data(&self) -> Result<ChannelData, Error>;
|
||||
|
||||
async fn command_macros(&self) -> Result<Vec<CommandMacro<Data, Error>>, Error>;
|
||||
async fn command_macros(&self) -> Result<Vec<CommandMacro>, Error>;
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
@ -57,7 +57,7 @@ impl CtxData for Context<'_> {
|
||||
ChannelData::from_channel(&channel, &self.data().database).await
|
||||
}
|
||||
|
||||
async fn command_macros(&self) -> Result<Vec<CommandMacro<Data, Error>>, Error> {
|
||||
async fn command_macros(&self) -> Result<Vec<CommandMacro>, Error> {
|
||||
self.data().command_macros(self.guild_id().unwrap()).await
|
||||
}
|
||||
}
|
||||
@ -66,7 +66,7 @@ impl Data {
|
||||
pub(crate) async fn command_macros(
|
||||
&self,
|
||||
guild_id: GuildId,
|
||||
) -> Result<Vec<CommandMacro<Data, Error>>, Error> {
|
||||
) -> Result<Vec<CommandMacro>, Error> {
|
||||
let rows = sqlx::query!(
|
||||
"SELECT name, description, commands FROM macro WHERE guild_id = (SELECT id FROM guilds WHERE guild = ?)",
|
||||
guild_id.get()
|
||||
|
Reference in New Issue
Block a user