added per server profiles
This commit is contained in:
parent
ad80e7d3ae
commit
a47bb29e7d
2 changed files with 57 additions and 31 deletions
|
|
@ -6,7 +6,8 @@ use serenity::all::CreateEmbed;
|
|||
|
||||
// Define your FAQ struct
|
||||
struct Faq {
|
||||
// id: i64,
|
||||
id: i64,
|
||||
guild_id: String,
|
||||
title: String,
|
||||
description: String,
|
||||
}
|
||||
|
|
@ -16,10 +17,11 @@ fn format_embed_description<S: Into<String>>(description: S) -> Result<String, E
|
|||
Ok(str::replace(&string_val, "\\n", "\n"))
|
||||
}
|
||||
|
||||
fn faq_exists(conn: &Connection, faq: &str) -> Result<bool, Error> {
|
||||
fn faq_exists(conn: &Connection, faq: &str, guild_id: &u64) -> Result<bool, Error> {
|
||||
let guild_id = guild_id.to_string();
|
||||
let exists: bool = conn.query_row(
|
||||
"SELECT EXISTS(SELECT 1 FROM faqs WHERE title = ?)",
|
||||
[faq],
|
||||
"SELECT EXISTS(SELECT 1 FROM faqs WHERE title = ?1 AND description = ?2)",
|
||||
[faq, &guild_id],
|
||||
|row| row.get(0),
|
||||
)?;
|
||||
|
||||
|
|
@ -33,19 +35,28 @@ async fn autocomplete_faq_titles<'a>(
|
|||
let data = ctx.data();
|
||||
|
||||
let fetch_faqs = data
|
||||
.execute_db(|conn| {
|
||||
let mut stmt = conn.prepare("SELECT ID, title, description FROM faqs")?;
|
||||
.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(&format!(
|
||||
"SELECT id, guild_id, title, description FROM faqs WHERE guild_id='{}'",
|
||||
guild_id
|
||||
))?;
|
||||
|
||||
let faqs: Vec<Faq> = stmt
|
||||
.query_map([], |row| {
|
||||
Ok(Faq {
|
||||
title: row.get(1)?,
|
||||
description: row.get(2)?,
|
||||
})
|
||||
})?
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
let faqs: Vec<Faq> = stmt
|
||||
.query_map([], |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)
|
||||
Ok(faqs)
|
||||
}
|
||||
})
|
||||
.await;
|
||||
|
||||
|
|
@ -59,7 +70,7 @@ async fn autocomplete_faq_titles<'a>(
|
|||
.map(|faq| faq.title)
|
||||
}
|
||||
|
||||
#[poise::command(slash_command, subcommands("view", "add", "edit"))]
|
||||
#[poise::command(slash_command, subcommands("view", "add", "edit"), guild_only)]
|
||||
pub async fn faq(_ctx: Context<'_>) -> Result<(), Error> {
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -75,24 +86,29 @@ pub async fn view(
|
|||
let data = ctx.data();
|
||||
|
||||
let faqs = data
|
||||
.execute_db(move |conn| {
|
||||
.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(&format!(
|
||||
"SELECT ID, title, description FROM faqs WHERE title='{}'",
|
||||
&faq_title
|
||||
"SELECT id, guild_id, title, description FROM faqs WHERE title='{}' AND guild_id='{}'",
|
||||
&faq_title, &guild_id
|
||||
))?;
|
||||
|
||||
let faqs: Vec<Faq> = stmt
|
||||
.query_map([], |row| {
|
||||
Ok(Faq {
|
||||
// id: row.get(0)?,
|
||||
title: row.get(1)?,
|
||||
description: row.get(2)?,
|
||||
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();
|
||||
|
|
@ -124,13 +140,16 @@ pub async fn add(
|
|||
.execute_db({
|
||||
let title = title.clone();
|
||||
let description = description.clone();
|
||||
|conn| {
|
||||
let exists = faq_exists(conn, &title).unwrap_or(false);
|
||||
|
||||
let server_guild_id = ctx.guild_id().expect("Guild ID not found");
|
||||
let guild_id = server_guild_id.get();
|
||||
move |conn| {
|
||||
let exists = faq_exists(conn, &title, &guild_id).unwrap_or(false);
|
||||
|
||||
if !exists {
|
||||
conn.execute(
|
||||
"INSERT INTO faqs (title, description) VALUES (?1, ?2)",
|
||||
[title, description],
|
||||
"INSERT INTO faqs (guild_id, title, description) VALUES (?1, ?2, ?3)",
|
||||
[guild_id.to_string(), title, description],
|
||||
)
|
||||
} else {
|
||||
Ok(0)
|
||||
|
|
@ -175,10 +194,14 @@ pub async fn edit(
|
|||
.execute_db({
|
||||
let title = title.clone();
|
||||
let description = description.clone();
|
||||
|
||||
let server_guild_id = ctx.guild_id().expect("Guild ID not found");
|
||||
let guild_id = server_guild_id.get();
|
||||
|
||||
move |conn| {
|
||||
conn.execute(
|
||||
"UPDATE faqs SET description = ?2 WHERE title = ?1",
|
||||
[title, description],
|
||||
"UPDATE faqs SET description = ?3 WHERE title = ?1 AND guild_id = ?2",
|
||||
[title, guild_id.to_string(), description],
|
||||
)
|
||||
}
|
||||
})
|
||||
|
|
@ -214,11 +237,13 @@ pub async fn delete(
|
|||
title: String,
|
||||
) -> Result<(), Error> {
|
||||
let data = ctx.data();
|
||||
let server_guild_id = ctx.guild_id().expect("Guild ID not found");
|
||||
let guild_id = server_guild_id.to_string();
|
||||
|
||||
let result = data
|
||||
.execute_db({
|
||||
let title = title.clone();
|
||||
move |conn| conn.execute("DELETE FROM faqs WHERE title = ?1", [title])
|
||||
move |conn| conn.execute("DELETE FROM faqs WHERE title = ?1 AND guild_id = ?2", [title, guild_id])
|
||||
})
|
||||
.await?;
|
||||
|
||||
|
|
|
|||
|
|
@ -33,7 +33,8 @@ pub fn init_tables(conn: &Connection) -> Result<()> {
|
|||
if !faq_table_exists {
|
||||
conn.execute(
|
||||
"CREATE TABLE faqs (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
guild_id TEXT NOT NULL,
|
||||
title TEXT NOT NULL,
|
||||
description TEXT NOT NULL
|
||||
)",
|
||||
|
|
|
|||
Loading…
Reference in a new issue