this repo has no description
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

chore: clippy

isabel b3bf9f68 c62039f5

+118 -120
-1
src/commands/misc/mod.rs
··· 1 1 pub mod crates; 2 2 pub mod starboard; 3 -
+7 -41
src/commands/misc/starboard.rs
··· 1 1 use crate::types::Context; 2 + use crate::utils::STARBOARD_DB; 2 3 use color_eyre::eyre::Result; 3 - use poise::serenity_prelude::ChannelId; 4 4 use poise::CreateReply; 5 - use rusqlite::Connection; 6 - use std::sync::{LazyLock, Mutex}; 7 - 8 - static STARBOARD_DB: LazyLock<Mutex<Connection>> = LazyLock::new(|| { 9 - let db_path = crate::utils::get_data_dir().join("starboard.db"); 10 - Mutex::new(Connection::open(db_path).expect("Failed to open starboard database")) 11 - }); 12 - 13 - fn ensure_tables_exist(conn: &Connection) -> rusqlite::Result<()> { 14 - conn.execute( 15 - "CREATE TABLE IF NOT EXISTS starred_messages ( 16 - message_id INTEGER PRIMARY KEY, 17 - guild_id INTEGER NOT NULL, 18 - channel_id INTEGER NOT NULL, 19 - starboard_message_id INTEGER, 20 - star_count INTEGER NOT NULL DEFAULT 1, 21 - UNIQUE(message_id) 22 - )", 23 - [], 24 - )?; 25 - 26 - conn.execute( 27 - "CREATE TABLE IF NOT EXISTS starboard_config ( 28 - guild_id INTEGER PRIMARY KEY, 29 - channel_id INTEGER NOT NULL, 30 - threshold INTEGER NOT NULL DEFAULT 3 31 - )", 32 - [], 33 - )?; 34 - 35 - Ok(()) 36 - } 5 + use poise::serenity_prelude::ChannelId; 37 6 38 7 /// Enable the starboard feature for this server 39 8 #[poise::command(slash_command, required_permissions = "ADMINISTRATOR")] ··· 53 22 return Ok(()); 54 23 }; 55 24 56 - let threshold = threshold.unwrap_or(3).max(1).min(100); 25 + let threshold = threshold.unwrap_or(3).clamp(1, 100); 57 26 58 27 { 59 28 let conn = STARBOARD_DB.lock().unwrap(); 60 - ensure_tables_exist(&conn)?; 61 29 conn.execute( 62 30 "INSERT OR REPLACE INTO starboard_config (guild_id, channel_id, threshold) VALUES (?, ?, ?)", 63 - [guild_id.get() as i64, channel.get() as i64, i64::from(threshold)], 31 + [guild_id.get().cast_signed(), channel.get().cast_signed(), i64::from(threshold)], 64 32 )?; 65 33 } 66 34 ··· 91 59 92 60 { 93 61 let conn = STARBOARD_DB.lock().unwrap(); 94 - ensure_tables_exist(&conn)?; 95 62 conn.execute( 96 63 "DELETE FROM starboard_config WHERE guild_id = ?", 97 - [guild_id.get() as i64], 64 + [guild_id.get().cast_signed()], 98 65 )?; 99 66 } 100 67 ··· 123 90 124 91 let config: Option<(u64, i32)> = { 125 92 let conn = STARBOARD_DB.lock().unwrap(); 126 - ensure_tables_exist(&conn).ok(); 127 93 conn.query_row( 128 94 "SELECT channel_id, threshold FROM starboard_config WHERE guild_id = ?", 129 - [guild_id.get() as i64], 95 + [guild_id.get().cast_signed()], 130 96 |row| { 131 97 let channel_id: i64 = row.get(0)?; 132 98 let threshold: i32 = row.get(1)?; 133 - Ok((channel_id as u64, threshold)) 99 + Ok((channel_id.cast_unsigned(), threshold)) 134 100 }, 135 101 ) 136 102 .ok()
+1 -1
src/commands/nix/nixpkgs.rs
··· 1 1 use color_eyre::eyre::Result; 2 2 use nixpkgs_track_lib::{branch_contains_commit, fetch_nixpkgs_pull_request}; 3 - use poise::{serenity_prelude::CreateEmbed, CreateReply}; 3 + use poise::{CreateReply, serenity_prelude::CreateEmbed}; 4 4 use std::fmt::Write as _; 5 5 6 6 use crate::types::Context;
+16 -8
src/commands/user/color_me.rs
··· 29 29 .prepare("SELECT role_id, role_name FROM color_roles WHERE user_id = ? AND guild_id = ?") 30 30 .ok()?; 31 31 32 - stmt.query_row(params![user_id.get() as i64, guild_id as i64], |row| { 33 - let role_id: i64 = row.get(0)?; 34 - let role_name: String = row.get(1)?; 35 - Ok((RoleId::new(role_id as u64), role_name)) 36 - }) 32 + stmt.query_row( 33 + params![user_id.get().cast_signed(), guild_id.cast_signed()], 34 + |row| { 35 + let role_id: i64 = row.get(0)?; 36 + let role_name: String = row.get(1)?; 37 + Ok((RoleId::new(role_id.cast_unsigned()), role_name)) 38 + }, 39 + ) 37 40 .ok() 38 41 } 39 42 ··· 41 44 let conn = COLOR_DB.lock().unwrap(); 42 45 conn.execute( 43 46 "INSERT OR REPLACE INTO color_roles (user_id, guild_id, role_id, role_name) VALUES (?, ?, ?, ?)", 44 - params![user_id.get() as i64, guild_id as i64, role_id.get() as i64, role_name], 47 + params![user_id.get().cast_signed(), guild_id.cast_signed(), role_id.get().cast_signed(), role_name], 45 48 )?; 46 49 Ok(()) 47 50 } ··· 50 53 let conn = COLOR_DB.lock().unwrap(); 51 54 conn.execute( 52 55 "DELETE FROM color_roles WHERE user_id = ? AND guild_id = ?", 53 - params![user_id.get() as i64, guild_id as i64], 56 + params![user_id.get().cast_signed(), guild_id.cast_signed()], 54 57 )?; 55 58 Ok(()) 56 59 } ··· 59 62 let conn = COLOR_DB.lock().unwrap(); 60 63 conn.execute( 61 64 "UPDATE color_roles SET role_name = ? WHERE user_id = ? AND guild_id = ?", 62 - params![new_name, user_id.get() as i64, guild_id as i64], 65 + params![ 66 + new_name, 67 + user_id.get().cast_signed(), 68 + guild_id.cast_signed() 69 + ], 63 70 )?; 64 71 Ok(()) 65 72 } 66 73 67 74 /// Change your display color or remove your color role. 75 + #[allow(clippy::too_many_lines)] 68 76 #[poise::command(slash_command)] 69 77 pub async fn color_me( 70 78 ctx: Context<'_>,
+34 -58
src/event_handler/starboard.rs
··· 1 1 use color_eyre::eyre::Result; 2 2 use poise::serenity_prelude::{Colour, Context, EditMessage, FullEvent, ReactionType}; 3 - use rusqlite::Connection; 4 - use std::sync::{LazyLock, Mutex}; 5 3 6 4 use crate::types::Data; 7 - 8 - static STARBOARD_DB: LazyLock<Mutex<Connection>> = LazyLock::new(|| { 9 - let db_path = crate::utils::get_data_dir().join("starboard.db"); 10 - let conn = Connection::open(db_path).expect("Failed to open starboard database"); 11 - 12 - conn.execute( 13 - "CREATE TABLE IF NOT EXISTS starred_messages ( 14 - message_id INTEGER PRIMARY KEY, 15 - guild_id INTEGER NOT NULL, 16 - channel_id INTEGER NOT NULL, 17 - starboard_message_id INTEGER, 18 - star_count INTEGER NOT NULL DEFAULT 1, 19 - UNIQUE(message_id) 20 - )", 21 - [], 22 - ) 23 - .expect("Failed to create starred_messages table"); 24 - 25 - conn.execute( 26 - "CREATE TABLE IF NOT EXISTS starboard_config ( 27 - guild_id INTEGER PRIMARY KEY, 28 - channel_id INTEGER NOT NULL, 29 - threshold INTEGER NOT NULL DEFAULT 3 30 - )", 31 - [], 32 - ) 33 - .expect("Failed to create starboard_config table"); 34 - 35 - Mutex::new(conn) 36 - }); 5 + use crate::utils::STARBOARD_DB; 37 6 38 7 pub async fn handle(ctx: &Context, event: &FullEvent, _data: &Data) -> Result<()> { 39 8 match event { ··· 49 18 Ok(()) 50 19 } 51 20 21 + #[allow(clippy::too_many_lines)] 52 22 async fn handle_reaction_add( 53 23 ctx: &Context, 54 24 reaction: &poise::serenity_prelude::Reaction, ··· 63 33 }; 64 34 65 35 // Get starboard config 66 - let config: Option<(u64, i32)> = { 36 + let config: Option<(u64, u32)> = { 67 37 let conn = STARBOARD_DB.lock().unwrap(); 68 38 conn.query_row( 69 39 "SELECT channel_id, threshold FROM starboard_config WHERE guild_id = ?", 70 - [guild_id.get() as i64], 40 + [guild_id.get().cast_signed()], 71 41 |row| { 72 42 let channel_id: i64 = row.get(0)?; 73 43 let threshold: i32 = row.get(1)?; 74 - Ok((channel_id as u64, threshold)) 44 + Ok((channel_id.cast_unsigned(), threshold.cast_unsigned())) 75 45 }, 76 46 ) 77 47 .ok() ··· 94 64 .find(|r| r.reaction_type == ReactionType::Unicode("⭐".to_string())) 95 65 .map_or(0, |r| r.count); 96 66 97 - if star_count < threshold as u64 { 67 + if star_count < u64::from(threshold) { 98 68 return Ok(()); 99 69 } 100 70 ··· 103 73 let conn = STARBOARD_DB.lock().unwrap(); 104 74 conn.query_row( 105 75 "SELECT COUNT(*) FROM starred_messages WHERE message_id = ?", 106 - [reaction.message_id.get() as i64], 76 + [reaction.message_id.get().cast_signed()], 107 77 |row| { 108 78 let count: i32 = row.get(0)?; 109 79 Ok(count > 0) ··· 118 88 let conn = STARBOARD_DB.lock().unwrap(); 119 89 conn.execute( 120 90 "UPDATE starred_messages SET star_count = ? WHERE message_id = ?", 121 - [star_count as i64, reaction.message_id.get() as i64], 91 + [ 92 + star_count.cast_signed(), 93 + reaction.message_id.get().cast_signed(), 94 + ], 122 95 ) 123 96 .ok(); 124 97 } ··· 128 101 let conn = STARBOARD_DB.lock().unwrap(); 129 102 conn.query_row( 130 103 "SELECT starboard_message_id FROM starred_messages WHERE message_id = ?", 131 - [reaction.message_id.get() as i64], 104 + [reaction.message_id.get().cast_signed()], 132 105 |row| row.get::<_, i64>(0), 133 106 ) 134 107 .ok() ··· 139 112 poise::serenity_prelude::ChannelId::new(starboard_channel_id) 140 113 .message( 141 114 ctx, 142 - poise::serenity_prelude::MessageId::new(starboard_msg_id as u64), 115 + poise::serenity_prelude::MessageId::new(starboard_msg_id.cast_unsigned()), 143 116 ) 144 117 .await 145 118 { 146 - let embed = create_star_embed(&message, star_count as i32); 119 + let embed = create_star_embed(&message, star_count); 147 120 starboard_msg 148 121 .edit(ctx, EditMessage::new().embed(embed)) 149 122 .await ··· 155 128 156 129 // Create starboard message 157 130 let starboard_channel = poise::serenity_prelude::ChannelId::new(starboard_channel_id); 158 - let embed = create_star_embed(&message, star_count as i32); 131 + let embed = create_star_embed(&message, star_count); 159 132 160 133 if let Ok(starboard_msg) = starboard_channel 161 134 .send_message( ··· 174 147 conn.execute( 175 148 "INSERT INTO starred_messages (message_id, guild_id, channel_id, starboard_message_id, star_count) VALUES (?, ?, ?, ?, ?)", 176 149 [ 177 - reaction.message_id.get() as i64, 178 - guild_id.get() as i64, 179 - reaction.channel_id.get() as i64, 180 - starboard_msg.id.get() as i64, 181 - star_count as i64, 150 + reaction.message_id.get().cast_signed(), 151 + guild_id.get().cast_signed(), 152 + reaction.channel_id.get().cast_signed(), 153 + starboard_msg.id.get().cast_signed(), 154 + star_count.cast_signed(), 182 155 ], 183 156 ).ok(); 184 157 } ··· 200 173 }; 201 174 202 175 // Get starboard config 203 - let config: Option<(u64, i32)> = { 176 + let config: Option<(u64, u32)> = { 204 177 let conn = STARBOARD_DB.lock().unwrap(); 205 178 conn.query_row( 206 179 "SELECT channel_id, threshold FROM starboard_config WHERE guild_id = ?", 207 - [guild_id.get() as i64], 180 + [guild_id.get().cast_signed()], 208 181 |row| { 209 182 let channel_id: i64 = row.get(0)?; 210 183 let threshold: i32 = row.get(1)?; 211 - Ok((channel_id as u64, threshold)) 184 + Ok((channel_id.cast_unsigned(), threshold.cast_unsigned())) 212 185 }, 213 186 ) 214 187 .ok() ··· 236 209 let conn = STARBOARD_DB.lock().unwrap(); 237 210 conn.query_row( 238 211 "SELECT starboard_message_id FROM starred_messages WHERE message_id = ?", 239 - [reaction.message_id.get() as i64], 212 + [reaction.message_id.get().cast_signed()], 240 213 |row| row.get(0), 241 214 ) 242 215 .ok() ··· 246 219 return Ok(()); 247 220 }; 248 221 249 - if star_count < threshold as u64 { 222 + if star_count < u64::from(threshold) { 250 223 // Remove from starboard 251 224 poise::serenity_prelude::ChannelId::new(starboard_channel_id) 252 225 .delete_message( 253 226 ctx, 254 - poise::serenity_prelude::MessageId::new(starboard_msg_id as u64), 227 + poise::serenity_prelude::MessageId::new(starboard_msg_id.cast_unsigned()), 255 228 ) 256 229 .await 257 230 .ok(); ··· 259 232 let conn = STARBOARD_DB.lock().unwrap(); 260 233 conn.execute( 261 234 "DELETE FROM starred_messages WHERE message_id = ?", 262 - [reaction.message_id.get() as i64], 235 + [reaction.message_id.get().cast_signed()], 263 236 ) 264 237 .ok(); 265 238 } else { ··· 268 241 let conn = STARBOARD_DB.lock().unwrap(); 269 242 conn.execute( 270 243 "UPDATE starred_messages SET star_count = ? WHERE message_id = ?", 271 - [star_count as i64, reaction.message_id.get() as i64], 244 + [ 245 + star_count.cast_signed(), 246 + reaction.message_id.get().cast_signed(), 247 + ], 272 248 ) 273 249 .ok(); 274 250 } ··· 277 253 if let Ok(mut starboard_msg) = poise::serenity_prelude::ChannelId::new(starboard_channel_id) 278 254 .message( 279 255 ctx, 280 - poise::serenity_prelude::MessageId::new(starboard_msg_id as u64), 256 + poise::serenity_prelude::MessageId::new(starboard_msg_id.cast_unsigned()), 281 257 ) 282 258 .await 283 259 { 284 - let embed = create_star_embed(&message, star_count as i32); 260 + let embed = create_star_embed(&message, star_count); 285 261 starboard_msg 286 262 .edit(ctx, EditMessage::new().embed(embed)) 287 263 .await ··· 294 270 295 271 fn create_star_embed( 296 272 message: &poise::serenity_prelude::Message, 297 - star_count: i32, 273 + star_count: u64, 298 274 ) -> poise::serenity_prelude::CreateEmbed { 299 275 let mut embed = poise::serenity_prelude::CreateEmbed::default() 300 276 .author(
+2 -1
src/main.rs
··· 1 1 mod commands; 2 2 mod event_handler; 3 + mod nixpkgs_db; 3 4 mod types; 4 5 mod utils; 5 - mod nixpkgs_db; 6 6 7 7 use dotenv::dotenv; 8 8 use std::env; ··· 13 13 }; 14 14 15 15 #[tokio::main] 16 + #[allow(clippy::too_many_lines)] 16 17 async fn main() -> Result<()> { 17 18 // Load the .env file 18 19 dotenv().ok();
+26 -10
src/nixpkgs_db.rs
··· 90 90 Ok(()) 91 91 } 92 92 93 - #[allow(clippy::too_many_lines)] 93 + #[allow(clippy::too_many_lines, clippy::items_after_statements)] 94 94 pub async fn ensure_nixpkgs_database() -> Result<()> { 95 95 let db_path = crate::utils::get_data_dir().join("packages.db"); 96 96 ··· 139 139 .ok_or_else(|| color_eyre::eyre::eyre!("Invalid packages.json format"))?; 140 140 141 141 println!("Creating database with {} packages...", packages.len()); 142 + 143 + const BATCH_SIZE: usize = 5000; // Increased from 1000 for better performance 142 144 143 145 let mut conn = rusqlite::Connection::open(&db_path)?; 144 146 ··· 194 196 195 197 // Enable WAL mode for better concurrent access 196 198 conn.pragma_update(None, "journal_mode", "WAL")?; 197 - 199 + 198 200 // Increase cache size for better performance 199 201 conn.pragma_update(None, "cache_size", "-64000")?; 200 202 201 203 let total = packages.len(); 202 204 let mut count = 0; 203 - const BATCH_SIZE: usize = 5000; // Increased from 1000 for better performance 204 205 205 206 let mut package_batch = Vec::with_capacity(BATCH_SIZE); 206 207 let mut maintainer_batch = Vec::with_capacity(BATCH_SIZE * 4); // Estimate 4 maintainers per package ··· 260 261 if let Some(obj) = m.as_object() { 261 262 maintainer_batch.push(Maintainer { 262 263 package_name: pkg_name.clone(), 263 - name: obj.get("name").and_then(|v| v.as_str()).map(|s| s.to_string()), 264 - email: obj.get("email").and_then(|v| v.as_str()).map(|s| s.to_string()), 265 - github: obj.get("github").and_then(|v| v.as_str()).map(|s| s.to_string()), 264 + name: obj 265 + .get("name") 266 + .and_then(|v| v.as_str()) 267 + .map(std::string::ToString::to_string), 268 + email: obj 269 + .get("email") 270 + .and_then(|v| v.as_str()) 271 + .map(std::string::ToString::to_string), 272 + github: obj 273 + .get("github") 274 + .and_then(|v| v.as_str()) 275 + .map(std::string::ToString::to_string), 266 276 github_id: obj.get("githubId").and_then(serde_json::Value::as_i64), 267 - matrix: obj.get("matrix").and_then(|v| v.as_str()).map(|s| s.to_string()), 277 + matrix: obj 278 + .get("matrix") 279 + .and_then(|v| v.as_str()) 280 + .map(std::string::ToString::to_string), 268 281 }); 269 282 } 270 283 } ··· 297 310 fn extract_string(obj: &serde_json::Value, key: &str) -> Option<String> { 298 311 obj.get(key) 299 312 .and_then(serde_json::Value::as_str) 300 - .map(|s| s.to_string()) 313 + .map(std::string::ToString::to_string) 301 314 } 302 315 303 316 /// Extract license information from license data ··· 306 319 serde_json::Value::Object(obj) => obj 307 320 .get("spdxId") 308 321 .and_then(|v| v.as_str()) 309 - .map(|s| s.to_string()), 322 + .map(std::string::ToString::to_string), 310 323 serde_json::Value::Array(arr) => { 311 324 let ids: Vec<&str> = arr 312 325 .iter() ··· 328 341 fn extract_homepage(meta: &serde_json::Value) -> Option<String> { 329 342 meta.get("homepage").and_then(|h| match h { 330 343 serde_json::Value::String(s) => Some(s.clone()), 331 - serde_json::Value::Array(arr) => arr.first().and_then(|v| v.as_str()).map(|s| s.to_string()), 344 + serde_json::Value::Array(arr) => arr 345 + .first() 346 + .and_then(|v| v.as_str()) 347 + .map(std::string::ToString::to_string), 332 348 _ => None, 333 349 }) 334 350 }
+32
src/utils.rs
··· 1 + use rusqlite::Connection; 1 2 use std::path::PathBuf; 3 + use std::sync::{LazyLock, Mutex}; 2 4 3 5 // TODO: figure this out lol 4 6 pub fn get_data_dir() -> PathBuf { 5 7 PathBuf::from("/var/lib/blahaj") 6 8 } 9 + 10 + pub static STARBOARD_DB: LazyLock<Mutex<Connection>> = LazyLock::new(|| { 11 + let db_path = get_data_dir().join("starboard.db"); 12 + let conn = Connection::open(db_path).expect("Failed to open starboard database"); 13 + 14 + conn.execute( 15 + "CREATE TABLE IF NOT EXISTS starred_messages ( 16 + message_id INTEGER PRIMARY KEY, 17 + guild_id INTEGER NOT NULL, 18 + channel_id INTEGER NOT NULL, 19 + starboard_message_id INTEGER, 20 + star_count INTEGER NOT NULL DEFAULT 1, 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 + Mutex::new(conn) 38 + });