diff --git a/src/commands/server_icon.rs b/src/commands/server_icon.rs index 4ce10ac..9c970fc 100644 --- a/src/commands/server_icon.rs +++ b/src/commands/server_icon.rs @@ -17,7 +17,7 @@ pub(crate) mod admin { pub(crate) mod change { use super::*; - pub async fn run(command: &CommandInteraction, ctx: &Context) -> String { + pub async fn run(_command: &CommandInteraction, ctx: &Context) -> String { let db_lock = { let data_read = ctx.data.read().await; data_read.get::().expect("Expected Databse in TypeMap.").clone() @@ -78,11 +78,12 @@ pub(crate) mod user { let config_toml = get_config_icons::minimal(); if let Some(logo) = get_current_icon(&db).await { - let attachment = CreateAttachment::path(&logo.path).await.unwrap(); - match command.edit_response(&ctx.http, EditInteractionResponse::new().new_attachment(attachment)).await { - Ok(_) => {} - Err(e) => { - dbg!(e); + if let Ok(attachment) = CreateAttachment::path(&logo.path).await { + match command.edit_response(&ctx.http, EditInteractionResponse::new().new_attachment(attachment)).await { + Ok(_) => {} + Err(e) => { + dbg!(e); + } } } @@ -116,7 +117,7 @@ pub(crate) mod user { use skynet_discord_bot::Config; // use this to return what current festivals are active? - pub async fn run(command: &CommandInteraction, ctx: &Context) -> String { + pub async fn run(_command: &CommandInteraction, ctx: &Context) -> String { let config_lock = { let data_read = ctx.data.read().await; data_read.get::().expect("Expected Config in TypeMap.").clone() @@ -141,7 +142,7 @@ pub(crate) mod user { use super::*; use sqlx::{Pool, Sqlite}; - pub async fn run(command: &CommandInteraction, ctx: &Context) -> String { + pub async fn run(_command: &CommandInteraction, ctx: &Context) -> String { let db_lock = { let data_read = ctx.data.read().await; data_read.get::().expect("Expected Databse in TypeMap.").clone() @@ -178,8 +179,8 @@ pub(crate) mod user { }) } - fn fmt_msg(config_toml: &ConfigTomlLocal, totals: &Vec) -> String { - let mut totals_local = totals.clone(); + fn fmt_msg(config_toml: &ConfigTomlLocal, totals: &[CountResult]) -> String { + let mut totals_local = totals.to_owned(); totals_local.sort_by_key(|x| x.times); totals_local.reverse(); diff --git a/src/common/server_icon.rs b/src/common/server_icon.rs index ab4b991..167a105 100644 --- a/src/common/server_icon.rs +++ b/src/common/server_icon.rs @@ -43,8 +43,16 @@ pub mod get_config_icons { } pub fn minimal() -> ConfigTomlLocal { let toml_raw_min = include_str!("../../.server-icons.toml"); - let config_min: ConfigTomlLocal = toml::from_str(toml_raw_min).unwrap(); - config_min + toml::from_str::(toml_raw_min).unwrap_or_else(|e| { + dbg!(e); + ConfigTomlLocal { + source: ConfigTomlSource { + repo: "".to_string(), + directory: "".to_string(), + file: "".to_string(), + }, + } + }) } // since a copy of the festival file is in the repo we just need to get to it @@ -52,12 +60,21 @@ pub mod get_config_icons { let config_source = minimal(); let file_path = format!("{}/open-governance/{}/{}", &config.home, &config_source.source.directory, &config_source.source.file); - let contents = fs::read_to_string(file_path).expect("Should have been able to read the file"); - let config_festivals: ConfigTomlRemote = toml::from_str(&contents).unwrap(); + let contents = fs::read_to_string(file_path).unwrap_or_else(|e| { + dbg!(e); + "".to_string() + }); + let festivals = match toml::from_str::(&contents) { + Ok(config_festivals) => config_festivals.festivals, + Err(e) => { + dbg!(e); + vec![] + } + }; ConfigToml { source: config_source.source, - festivals: config_festivals.festivals, + festivals, } } } @@ -154,15 +171,26 @@ pub mod update_icon { let url = &config_toml.source.repo; let folder = format!("{}/open-governance", &config.home); - Command::new("git").arg("clone").arg(url).arg(&folder).output().expect("failed to execute process"); + if let Err(e) = Command::new("git") + // clone the repo, gracefully "fails" + .arg("clone") + .arg(url) + .arg(&folder) + .output() + { + dbg!(e); + } - Command::new("git") + if let Err(e) = Command::new("git") + // Update the repo .arg("pull") .arg("origin") .arg("main") .current_dir(&folder) .output() - .expect("failed to execute process"); + { + dbg!(e); + } } fn get_logos(config: &Config, config_toml: &ConfigToml) -> Vec { @@ -170,7 +198,13 @@ pub mod update_icon { let folder_path = PathBuf::from(&folder); let mut folder_output = folder_path.clone(); folder_output.push("converted"); - let paths = fs::read_dir(folder).unwrap(); + let paths = match fs::read_dir(folder) { + Ok(x) => x, + Err(e) => { + dbg!(e); + return vec![]; + } + }; let args = Args { input: folder_path.clone(), @@ -179,14 +213,26 @@ pub mod update_icon { width: 1024, height: 1024, }; - let mut r = Renderer::new(&args).unwrap(); + let mut r = match Renderer::new(&args) { + Ok(x) => x, + Err(e) => { + let _ = dbg!(e); + return vec![]; + } + }; let mut logos = vec![]; for tmp in paths.flatten() { let path_local = tmp.path().to_owned(); let path_local2 = tmp.path().to_owned(); - let name = path_local2.file_name().unwrap().to_owned(); + let name = match path_local2.file_name() { + None => { + dbg!(path_local2); + continue; + } + Some(x) => x.to_owned(), + }; let mut path = tmp.path(); if path.is_dir() { @@ -200,7 +246,13 @@ pub mod update_icon { let mut path_new = path_local.clone(); path_new.set_extension("png"); let filename_tmp = path_new.clone(); - let filename = filename_tmp.file_name().unwrap_or_default(); + let filename = match filename_tmp.file_name() { + None => { + dbg!(filename_tmp); + continue; + } + Some(x) => x, + }; path_new.pop(); path_new.push("converted"); path_new.push(filename); @@ -266,18 +318,36 @@ pub mod update_icon { async fn logo_set(ctx: &Context, db: &Pool, server: &GuildId, logo_selected: &LogoData) { // add to teh database - logo_set_db(db, logo_selected).await; + if !logo_set_db(db, logo_selected).await { + // something went wrong + return; + } - let icon = CreateAttachment::path(logo_selected.path.to_str().unwrap_or_default()).await.unwrap(); - - // assuming a `guild` has already been bound - let builder = EditGuild::new().icon(Some(&icon)); - server.edit(ctx, builder).await.unwrap(); + if let Some(logo_path) = logo_selected.path.to_str() { + match CreateAttachment::path(logo_path).await { + Ok(icon) => { + // assuming a `guild` has already been bound + let builder = EditGuild::new().icon(Some(&icon)); + if let Err(e) = server.edit(ctx, builder).await { + dbg!(e); + } + } + Err(e) => { + dbg!(e); + } + } + } } - async fn logo_set_db(db: &Pool, logo_selected: &LogoData) { - let name = logo_selected.name.to_str().unwrap_or_default(); - let path = logo_selected.path.to_str().unwrap_or_default(); + async fn logo_set_db(db: &Pool, logo_selected: &LogoData) -> bool { + let name = match logo_selected.name.to_str() { + None => return false, + Some(x) => x, + }; + let path = match logo_selected.path.to_str() { + None => return false, + Some(x) => x, + }; match sqlx::query_as::<_, ServerIcons>( " @@ -294,7 +364,9 @@ pub mod update_icon { Ok(_) => {} Err(e) => { dbg!(e); + return false; } } + true } } diff --git a/src/main.rs b/src/main.rs index 4d58bdd..ebad7a0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,8 +2,7 @@ pub mod commands; use crate::commands::role_adder::tools::on_role_change; use serenity::all::{ - ActivityData, Command, CommandDataOption, CommandDataOptionValue, CommandOptionType, CreateMessage, EditInteractionResponse, GuildId, - GuildMemberUpdateEvent, Interaction, + ActivityData, Command, CommandDataOptionValue, CreateMessage, EditInteractionResponse, GuildId, GuildMemberUpdateEvent, Interaction, }; use serenity::model::guild::Member; use serenity::{