added FAQ preview

This commit is contained in:
liyunze 2026-01-08 08:57:30 +08:00
parent 8d34fd8c74
commit 5ce9cf8e73

View file

@ -67,7 +67,7 @@ async fn autocomplete_faq_titles<'a>(
.map(|faq| faq.title)
}
#[poise::command(slash_command, subcommands("view", "add", "edit", "delete"), guild_only)]
#[poise::command(slash_command, subcommands("view", "preview", "add", "edit", "delete"), guild_only)]
pub async fn faq(_ctx: Context<'_>) -> Result<(), Error> {
Ok(())
}
@ -119,6 +119,59 @@ pub async fn view(
Ok(())
}
/// Preview FAQ
#[poise::command(prefix_command, slash_command)]
pub async fn preview(
ctx: Context<'_>,
#[description = "FAQ Title"]
#[autocomplete = "autocomplete_faq_titles"]
faq_title: String,
) -> Result<(), Error> {
let data = ctx.data();
let faqs = data
.execute_db({
let server_guild_id = ctx.guild_id().expect("Guild ID not found");
let guild_id = server_guild_id.to_string();
move |conn| {
let mut stmt = conn.prepare(
"SELECT id, guild_id, title, description FROM faqs WHERE title = ?1 AND guild_id = ?2"
)?;
let faqs: Vec<Faq> = stmt
.query_map([&faq_title, &guild_id], |row| {
Ok(Faq {
// id: row.get(0)?,
// guild_id: row.get(1)?,
title: row.get(2)?,
description: row.get(3)?,
})
})?
.collect::<Result<Vec<_>, _>>()?;
Ok(faqs)
}})
.await?;
let faq = faqs.first().unwrap();
// let embed_description = format_embed_description(&faq.description).expect("Format error");
let embed = CreateEmbed::new()
.title(&faq.title)
.description(
format!("```
{}
```",
&faq.description
)
);
ctx.send(CreateReply::default().embed(embed)).await?;
Ok(())
}
/// Add FAQ
#[poise::command(
prefix_command,
@ -254,3 +307,4 @@ pub async fn delete(
Ok(())
}