diff options
| author | seth <[email protected]> | 2023-12-05 08:08:55 -0500 |
|---|---|---|
| committer | seth <[email protected]> | 2023-12-15 16:41:13 -0500 |
| commit | aa29304951763fd6cc0ae7a809c9a6e41f673434 (patch) | |
| tree | 9db8a092edbfe4c6958fdd176edfe1495265e4d5 | |
| parent | 3c1daf3cdc95924fd91158d5e46df668b0e43833 (diff) | |
fix: allow multiple arguments for config opts
| -rw-r--r-- | src/commands/moderation/config.rs | 62 | ||||
| -rw-r--r-- | src/handlers/event/reactboard.rs | 17 |
2 files changed, 38 insertions, 41 deletions
diff --git a/src/commands/moderation/config.rs b/src/commands/moderation/config.rs index d64c4cc..4a1fb47 100644 --- a/src/commands/moderation/config.rs +++ b/src/commands/moderation/config.rs @@ -1,10 +1,19 @@ +use std::str::FromStr; + use crate::{storage, Context}; use storage::SettingsProperties; -use color_eyre::eyre::{eyre, Context as _, ContextCompat, Result}; +use color_eyre::eyre::{eyre, Result}; use log::*; use poise::serenity_prelude::{GuildChannel, ReactionType}; +fn split_list<T>(list: String) -> Vec<T> +where + T: FromStr, +{ + list.split(',').filter_map(|s| s.parse().ok()).collect() +} + #[poise::command( slash_command, subcommands("set", "get"), @@ -20,13 +29,12 @@ pub async fn set( #[channel_types("Text")] #[description = "Where to redirect pins from channels. If empty (the default), the PinBoard is disabled."] pinboard_channel: Option<GuildChannel>, - #[channel_types("Text")] - #[description = "A channel that PinBoard will redirect pins from. This will be all channels if empty."] - pinboard_watch: Option<GuildChannel>, + #[description = "Comma separated list of channels PinBoard redirects. If empty, this will be all channels"] + pinboard_watch: Option<String>, #[channel_types("Text")] #[description = "Where to post messages that made it to the ReactBoard. If left empty, ReactBoard is disabled."] reactboard_channel: Option<GuildChannel>, - #[description = "An emoji that will get messages on the ReactBoard. If empty, ReactBoard is disabled."] + #[description = "Comma separated list of emojis that will count towards ReactBoard. If empty, ReactBoard is disabled."] reactboard_reaction: Option<String>, #[description = "Minimum number of reactions a message needs to make it to the ReactBoard (defaults to 5)"] reactboard_requirement: Option<u64>, @@ -39,55 +47,37 @@ pub async fn set( let previous_settings = settings.clone(); if let Some(channel) = pinboard_channel { + debug!("Setting pinboard_channel to {channel} for {gid}"); settings.pinboard_channel = Some(channel.id); } if let Some(watch) = pinboard_watch { - if let Some(mut prev) = settings.pinboard_watch { - prev.push(watch.id); - settings.pinboard_watch = Some(prev); - } else { - let new = Vec::from([watch.id]); - debug!("Setting pinboard_watch to {new:#?} for {}", gid); - - settings.pinboard_watch = Some(new); - } + let channels = split_list(watch); + debug!("Setting pinboard_watch to {channels:#?} for {gid}"); + + settings.pinboard_watch = Some(channels); } if let Some(channel) = reactboard_channel { - debug!("Setting reactboard_channel to {channel} for {}", gid); - + debug!("Setting reactboard_channel to {channel} for {gid}"); settings.reactboard_channel = Some(channel.id); } if let Some(requirement) = reactboard_requirement { - debug!( - "Setting reactboard_requirement to {requirement} for {}", - gid - ); - + debug!("Setting reactboard_requirement to {requirement} for {gid}"); settings.reactboard_requirement = Some(requirement); } if let Some(reaction) = reactboard_reaction { - let emoji = reaction - .parse::<ReactionType>() - .wrap_err_with(|| format!("Couldn't parse {reaction} as string!"))?; - - if let Some(mut prev) = settings.reactboard_reactions { - prev.push(emoji); - settings.reactboard_reactions = Some(prev); - } else { - let new = Vec::from([emoji]); - debug!("Setting pinboard_watch to {new:#?} for {}", gid); - - settings.reactboard_reactions = Some(new); - } + let emojis: Vec<ReactionType> = + reaction.split(',').filter_map(|r| r.parse().ok()).collect(); + debug!("Setting reactboard_reactions to {emojis:#?} for {gid}"); + + settings.reactboard_reactions = Some(emojis); } if let Some(enabled) = optional_commands_enabled { debug!("Setting optional_commands_enabled to {enabled} for {}", gid); - settings.optional_commands_enabled = enabled; } @@ -111,7 +101,7 @@ pub async fn get( ) -> Result<()> { let gid = &ctx .guild_id() - .wrap_err_with(|| eyre!("Failed to get GuildId from context!"))?; + .ok_or_else(|| eyre!("Failed to get GuildId from context!"))?; let settings = ctx.data().storage.get_guild_settings(gid).await?; diff --git a/src/handlers/event/reactboard.rs b/src/handlers/event/reactboard.rs index 53b51a7..d23a762 100644 --- a/src/handlers/event/reactboard.rs +++ b/src/handlers/event/reactboard.rs @@ -3,7 +3,7 @@ use storage::{ReactBoardEntry, REACT_BOARD_KEY}; use color_eyre::eyre::{eyre, Context as _, Result}; use log::*; -use poise::serenity_prelude::{Context, Message, MessageReaction, Reaction}; +use poise::serenity_prelude::{Context, GuildId, Message, MessageReaction, Reaction}; pub async fn handle(ctx: &Context, reaction: &Reaction, data: &Data) -> Result<()> { let msg = reaction @@ -24,7 +24,14 @@ pub async fn handle(ctx: &Context, reaction: &Reaction, data: &Data) -> Result<( ) })?; - send_to_reactboard(ctx, &matched, &msg, data).await?; + send_to_reactboard( + ctx, + &matched, + &msg, + &reaction.guild_id.unwrap_or_default(), + data, + ) + .await?; Ok(()) } @@ -33,17 +40,17 @@ async fn send_to_reactboard( ctx: &Context, reaction: &MessageReaction, msg: &Message, + guild_id: &GuildId, data: &Data, ) -> Result<()> { let storage = &data.storage; - let gid = msg.guild_id.unwrap_or_default(); - let settings = storage.get_guild_settings(&gid).await?; + let settings = storage.get_guild_settings(guild_id).await?; // make sure everything is in order... let target = if let Some(target) = settings.reactboard_channel { target } else { - debug!("Reactboard is disabled in {gid}, ignoring"); + debug!("Reactboard is disabled in {guild_id}, ignoring"); return Ok(()); }; |
