silly goober bot
at main 56 lines 1.7 kB view raw
1use rusqlite::Connection; 2use std::path::PathBuf; 3use std::sync::{LazyLock, Mutex}; 4 5pub fn get_data_dir() -> PathBuf { 6 crate::config::get().data_dir.clone() 7} 8 9pub static STARBOARD_DB: LazyLock<Mutex<Connection>> = LazyLock::new(|| { 10 let db_path = get_data_dir().join("starboard.db"); 11 let conn = Connection::open(db_path).expect("Failed to open starboard database"); 12 13 conn.execute( 14 "CREATE TABLE IF NOT EXISTS starred_messages ( 15 message_id INTEGER PRIMARY KEY, 16 guild_id INTEGER NOT NULL, 17 channel_id INTEGER NOT NULL, 18 starboard_message_id INTEGER, 19 star_count INTEGER NOT NULL DEFAULT 1, 20 posting INTEGER NOT NULL DEFAULT 0, 21 UNIQUE(message_id) 22 )", 23 [], 24 ) 25 .expect("Failed to create starred_messages table"); 26 27 conn.execute( 28 "CREATE TABLE IF NOT EXISTS starboard_config ( 29 guild_id INTEGER PRIMARY KEY, 30 channel_id INTEGER NOT NULL, 31 threshold INTEGER NOT NULL DEFAULT 3 32 )", 33 [], 34 ) 35 .expect("Failed to create starboard_config table"); 36 37 ensure_starboard_schema(&conn).expect("Failed to migrate starboard schema"); 38 39 Mutex::new(conn) 40}); 41 42fn ensure_starboard_schema(conn: &Connection) -> rusqlite::Result<()> { 43 let mut stmt = conn.prepare("PRAGMA table_info(starred_messages)")?; 44 let column_names = stmt 45 .query_map([], |row| row.get::<_, String>(1))? 46 .collect::<Result<Vec<_>, _>>()?; 47 48 if !column_names.iter().any(|name| name == "posting") { 49 conn.execute( 50 "ALTER TABLE starred_messages ADD COLUMN posting INTEGER NOT NULL DEFAULT 0", 51 [], 52 )?; 53 } 54 55 Ok(()) 56}