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 ++-- src/handlers/event/message.rs | 14 ++++++++++---- src/handlers/event/mod.rs | 14 +++++--------- src/main.rs | 18 +++++++----------- src/settings.rs | 13 +++++++++++-- src/utils.rs | 14 ++------------ 7 files changed, 52 insertions(+), 49 deletions(-) (limited to 'src') 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(()); } diff --git a/src/handlers/event/message.rs b/src/handlers/event/message.rs index a84ec59..88faf85 100644 --- a/src/handlers/event/message.rs +++ b/src/handlers/event/message.rs @@ -1,4 +1,5 @@ -use crate::{consts, utils, Data}; +use crate::settings::Settings; +use crate::{consts, Data}; use color_eyre::eyre::{Report, Result}; use log::*; @@ -9,17 +10,22 @@ pub async fn handle( ctx: &Context, framework: FrameworkContext<'_, Data, Report>, msg: &Message, + settings: &Settings, ) -> Result<()> { - if should_echo(framework, msg) { + if should_echo(framework, msg, settings) { msg.reply(ctx, &msg.content).await?; } Ok(()) } -fn should_echo(framework: FrameworkContext<'_, Data, Report>, msg: &Message) -> bool { +fn should_echo( + framework: FrameworkContext<'_, Data, Report>, + msg: &Message, + settings: &Settings, +) -> bool { let gid = msg.guild_id.unwrap_or_default(); - if msg.author.id == framework.bot_id || !utils::is_guild_allowed(gid) { + if msg.author.id == framework.bot_id || !settings.is_guild_allowed(gid) { info!("not running copypasta command in {gid}"); return false; } diff --git a/src/handlers/event/mod.rs b/src/handlers/event/mod.rs index 09be62b..bbfc642 100644 --- a/src/handlers/event/mod.rs +++ b/src/handlers/event/mod.rs @@ -19,18 +19,14 @@ pub async fn handle( log::info!("logged in as {}", data_about_bot.user.name) } - Event::Message { new_message } => message::handle(ctx, framework, new_message).await?, - - Event::ChannelPinsUpdate { pin } => { - if let Some(settings) = &data.settings { - pinboard::handle(ctx, pin, settings).await - } + Event::Message { new_message } => { + message::handle(ctx, framework, new_message, &data.settings).await? } + Event::ChannelPinsUpdate { pin } => pinboard::handle(ctx, pin, &data.settings).await, + Event::ReactionAdd { add_reaction } => { - if let Some(settings) = &data.settings { - reactboard::handle(ctx, add_reaction, settings).await? - } + reactboard::handle(ctx, add_reaction, &data.settings).await? } _ => {} diff --git a/src/main.rs b/src/main.rs index 98ebe5f..0e9400b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,20 +19,15 @@ type Context<'a> = poise::Context<'a, Data, Report>; #[derive(Clone)] pub struct Data { - settings: Option, + settings: Settings, } impl Data { - pub fn new() -> Self { - let settings = Settings::new(); + pub fn new() -> Result { + let settings = + Settings::new().ok_or_else(|| eyre!("Couldn't create new settings object!"))?; - Self { settings } - } -} - -impl Default for Data { - fn default() -> Self { - Self::new() + Ok(Self { settings }) } } @@ -82,7 +77,8 @@ async fn main() -> Result<()> { .await?; info!("Registered guild commands to {}", consts::TEAWIE_GUILD); - Ok(Data::new()) + let data = Data::new()?; + Ok(data) }) }); diff --git a/src/settings.rs b/src/settings.rs index f0fef31..5cf0dec 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -1,9 +1,10 @@ -use crate::utils; +use crate::{consts, utils}; use log::*; -use poise::serenity_prelude::{ChannelId, EmojiId, MessageReaction, ReactionType}; +use poise::serenity_prelude::{ChannelId, EmojiId, GuildId, MessageReaction, ReactionType}; #[derive(Clone)] pub struct Settings { + pub allowed_guilds: Vec, pub pinboard_target: ChannelId, pub pinboard_sources: Option>, pub reactboard_target: ChannelId, @@ -14,6 +15,9 @@ pub struct Settings { impl Settings { pub fn new() -> Option { + let allowed_guilds = utils::parse_snowflakes_from_env("ALLOWED_GUILDS", GuildId) + .unwrap_or_else(|| vec![consts::TEAWIE_GUILD, GuildId(1091969030694375444)]); + let Some(pinboard_target) = utils::parse_snowflake_from_env("PIN_BOARD_TARGET", ChannelId) else { return None; @@ -56,6 +60,7 @@ impl Settings { ); Some(Self { + allowed_guilds, pinboard_target, pinboard_sources, reactboard_target, @@ -77,4 +82,8 @@ impl Settings { _ => false, } } + + pub fn is_guild_allowed(&self, gid: GuildId) -> bool { + self.allowed_guilds.contains(&gid) + } } diff --git a/src/utils.rs b/src/utils.rs index 9a1d09c..c0353f0 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,10 +1,9 @@ -use crate::{colors, consts, Context}; +use crate::{colors, Context}; use color_eyre::eyre::{eyre, Result}; -use once_cell::sync::Lazy; use poise::serenity_prelude as serenity; use rand::seq::SliceRandom; -use serenity::{CreateEmbed, GuildId, Message}; +use serenity::{CreateEmbed, Message}; use url::Url; pub fn parse_snowflake_from_env T>(key: &str, f: F) -> Option { @@ -45,15 +44,6 @@ pub fn floor_char_boundary(s: &str, index: usize) -> usize { } } -pub fn is_guild_allowed(gid: GuildId) -> bool { - static ALLOWED_GUILDS: Lazy> = Lazy::new(|| { - parse_snowflakes_from_env("ALLOWED_GUILDS", GuildId) - .unwrap_or_else(|| vec![consts::TEAWIE_GUILD, GuildId(1091969030694375444)]) - }); - - ALLOWED_GUILDS.contains(&gid) -} - pub async fn send_url_as_embed(ctx: Context<'_>, url: String) -> Result<()> { let parsed = Url::parse(&url)?; -- cgit v1.2.3