From a47bb29e7d5a2bbd82f3bab18825aff53a3eaa6a Mon Sep 17 00:00:00 2001 From: liyunze <50455574+liyunze-coding@users.noreply.github.com> Date: Tue, 6 Jan 2026 22:29:10 +0800 Subject: [PATCH] added per server profiles --- src/commands/faq.rs | 85 ++++++++++++++++++++++++++-------------- src/database/database.rs | 3 +- 2 files changed, 57 insertions(+), 31 deletions(-) diff --git a/src/commands/faq.rs b/src/commands/faq.rs index 227696f..20d44ea 100644 --- a/src/commands/faq.rs +++ b/src/commands/faq.rs @@ -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>(description: S) -> Result Result { +fn faq_exists(conn: &Connection, faq: &str, guild_id: &u64) -> Result { + 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 = stmt - .query_map([], |row| { - Ok(Faq { - title: row.get(1)?, - description: row.get(2)?, - }) - })? - .collect::, _>>()?; + let faqs: Vec = stmt + .query_map([], |row| { + Ok(Faq { + id: row.get(0)?, + guild_id: row.get(1)?, + title: row.get(2)?, + description: row.get(3)?, + }) + })? + .collect::, _>>()?; - 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 = 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::, _>>()?; 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?; diff --git a/src/database/database.rs b/src/database/database.rs index 8457402..ab0143d 100644 --- a/src/database/database.rs +++ b/src/database/database.rs @@ -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 )",