diff --git a/Cargo.lock b/Cargo.lock index a4f009a..11135d7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1514,7 +1514,7 @@ dependencies = [ [[package]] name = "shibe-bot" -version = "0.3.1" +version = "0.3.2" dependencies = [ "build-time", "dotenvy", diff --git a/Cargo.toml b/Cargo.toml index 56e0c04..1227dde 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "shibe-bot" description = "A Discord bot written in Rust, using Poise." license = "MIT" readme = "README.md" -version = "0.3.1" +version = "0.3.2" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/README.md b/README.md index e17538e..6b3e549 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Shibe Bot 0.3.1 +# Shibe Bot 0.3.2 [![Rust](https://github.com/shibedrill/shibe-bot/actions/workflows/rust.yml/badge.svg)](https://github.com/shibedrill/shibe-bot/actions/workflows/rust.yml) [![GitHub License](https://img.shields.io/github/license/shibedrill/shibe-bot)](LICENSE.txt) diff --git a/src/command/fun.rs b/src/command/fun.rs index abf4bd6..d6ac574 100644 --- a/src/command/fun.rs +++ b/src/command/fun.rs @@ -35,7 +35,7 @@ pub async fn meow(ctx: Context<'_>) -> Result<(), Error> { "woof" // Will never return None. The source is statically defined. // We know it will always have items in it. - } else { + } else { meows .choose(&mut rng) .ok_or("`meows` array is empty") @@ -155,8 +155,11 @@ pub async fn deer(ctx: Context<'_>) -> Result<(), Error> { error!("Executing command `deer` failed: {}", e); })? }; - ctx.say(format!("https://vxreddit.com{}", &chosen_post.data.permalink)) - .await?; + ctx.say(format!( + "https://vxreddit.com{}", + &chosen_post.data.permalink + )) + .await?; info!("Executed command `deer` successfully"); Ok(()) } diff --git a/src/command/util.rs b/src/command/util.rs index 6aaa309..7e8ed0a 100644 --- a/src/command/util.rs +++ b/src/command/util.rs @@ -68,7 +68,9 @@ pub async fn add_channel( match config.channels.iter().find(|item| **item == channel_id) { None => { config.channels.push(channel_id); - ctx.say(format!("Successfully added <#{channel_id}> to the channel registry.")) + ctx.say(format!( + "Successfully added <#{channel_id}> to the channel registry." + )) .await?; } Some(_) => { @@ -92,12 +94,16 @@ pub async fn remove_channel( let channel_id = { u64::from(channel.id()) }; match config.channels.iter().position(|item| *item == channel_id) { None => { - ctx.say(format!("Channel <#{channel_id}> was not in the channel registry.")) + ctx.say(format!( + "Channel <#{channel_id}> was not in the channel registry." + )) .await?; } Some(found) => { config.channels.remove(found); - ctx.say(format!("Successfully removed <#{channel_id}> from the channel registry.")) + ctx.say(format!( + "Successfully removed <#{channel_id}> from the channel registry." + )) .await?; } } @@ -145,7 +151,7 @@ pub async fn dice( } ) } - _ => format!("Rolled a random number from 1 to {sides}, got: {answer}") + _ => format!("Rolled a random number from 1 to {sides}, got: {answer}"), }; ctx.say(response).await?; info!("Executed command `dice` successfully"); diff --git a/src/main.rs b/src/main.rs index a251cdd..36b45cd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -88,10 +88,11 @@ async fn main() { // Configure persistent options let config_manager: Arc>> = Arc::new(Mutex::new( - Manager::load(SETTINGS_PATH) - .unwrap_or(Manager::manage(SETTINGS_PATH, Settings::default())), + Manager::load(SETTINGS_PATH).unwrap_or(Manager::manage(SETTINGS_PATH, Settings::default())), )); - config_manager.lock().await.store(); + let _ = config_manager.lock().await.store().inspect_err(|e| { + error!("Failed to store config: {}", e); + }); // Set up framework let framework = poise::Framework::builder() diff --git a/src/settings.rs b/src/settings.rs index c3a7f33..56da883 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -13,12 +13,12 @@ pub struct Manager Deserialize<'a>> { impl Deserialize<'a>> Manager { /// Instantiate new self if the path contains a valid serialization of /// the settings structure. - pub fn load(path: &str) -> Option { - let mut file = std::fs::File::open(path).ok()?; + pub fn load(path: &str) -> Result { + let mut file = std::fs::File::open(path)?; let mut data = String::new(); - file.read_to_string(&mut data).ok()?; - let settings = serde_json::from_str(&data).ok()?; - Some(Self { + file.read_to_string(&mut data)?; + let settings = serde_json::from_str(&data)?; + Ok(Self { internal: settings, path: String::from(path), }) @@ -27,20 +27,20 @@ impl Deserialize<'a>> Manager { /// disk but not in memory. Because this is a stupid method, it will most /// likely go unused by most. #[allow(dead_code)] - pub fn update(&mut self) -> Option<()> { - let mut file = std::fs::File::open(self.path.clone()).ok()?; + pub fn update(&mut self) -> Result<(), std::io::Error> { + let mut file = std::fs::File::open(self.path.clone())?; let mut data = String::new(); - file.read_to_string(&mut data).ok()?; - self.internal = serde_json::from_str(&data).ok()?; - Some(()) + file.read_to_string(&mut data)?; + self.internal = serde_json::from_str(&data)?; + Ok(()) } /// Serialize settings structure to the stored path. Returns None if /// unsuccessful. - pub fn store(&self) -> Option<()> { - let data = serde_json::to_string_pretty(&self.internal).ok()?; - let mut file = std::fs::File::create(&self.path).ok()?; + pub fn store(&self) -> Result<(), std::io::Error> { + let data = serde_json::to_string_pretty(&self.internal)?; + let mut file = std::fs::File::create(&self.path)?; let _ = file.write(data.as_bytes()); - Some(()) + Ok(()) } /// Create a new manager, passing in the path, and a structure to manage. /// We cannot initialize a settings manager without fully initialized settings.