From 41dfa94258215769b9d844875e79097d4a498770 Mon Sep 17 00:00:00 2001 From: seth Date: Thu, 30 Nov 2023 22:44:26 -0500 Subject: refactor: expand Settings --- src/commands/copypasta.rs | 24 +++++++++++++++--------- src/commands/teawiespam.rs | 4 ++-- 2 files changed, 17 insertions(+), 11 deletions(-) (limited to 'src/commands') diff --git a/src/commands/copypasta.rs b/src/commands/copypasta.rs index 313cd13..16ac562 100644 --- a/src/commands/copypasta.rs +++ b/src/commands/copypasta.rs @@ -1,4 +1,4 @@ -use crate::{utils, Context}; +use crate::Context; use std::collections::HashMap; @@ -34,23 +34,29 @@ impl Copypastas { } } -fn get_copypasta(name: Copypastas) -> String { +fn get_copypasta(name: Copypastas) -> Result { let mut files: HashMap<&str, &str> = HashMap::new(); for file in FILES.files() { - let name = file.path().file_stem().unwrap().to_str().unwrap(); + let name = file + .path() + .file_stem() + .ok_or_else(|| eyre!("couldn't get file stem from {file:#?}"))? + .to_str() + .ok_or_else(|| eyre!("couldn't convert file stem to str!"))?; - let contents = file.contents_utf8().unwrap(); + let contents = file + .contents_utf8() + .ok_or_else(|| eyre!("couldnt get contents from copypasta!"))?; // refer to files by their name w/o extension files.insert(name, contents); } if files.contains_key(name.as_str()) { - files[name.as_str()].to_string() + Ok(files[name.as_str()].to_string()) } else { - warn!("copypasta {} not found!", name); - format!("i don't have a copypasta named {name} :(") + Err(eyre!("couldnt find copypasta {name}!")) } } @@ -64,12 +70,12 @@ pub async fn copypasta( .guild_id() .ok_or_else(|| eyre!("couldnt get guild from message!"))?; - if !utils::is_guild_allowed(gid) { + if !ctx.data().settings.is_guild_allowed(gid) { info!("not running copypasta command in {gid}"); return Ok(()); } - ctx.say(get_copypasta(copypasta)).await?; + ctx.say(get_copypasta(copypasta)?).await?; Ok(()) } diff --git a/src/commands/teawiespam.rs b/src/commands/teawiespam.rs index da01af9..aeea255 100644 --- a/src/commands/teawiespam.rs +++ b/src/commands/teawiespam.rs @@ -1,4 +1,3 @@ -use crate::utils; use crate::Context; use color_eyre::eyre::Result; @@ -8,7 +7,8 @@ use log::*; #[poise::command(slash_command, prefix_command)] pub async fn teawiespam(ctx: Context<'_>) -> Result<()> { let gid = ctx.guild_id().unwrap_or_default(); - if !utils::is_guild_allowed(gid) { + + if !ctx.data().settings.is_guild_allowed(gid) { info!("not running teawiespam command in {gid}"); return Ok(()); } -- cgit v1.2.3