From 0025ad5ea8d412aacc3184d18063fd5ff3de0175 Mon Sep 17 00:00:00 2001 From: seth Date: Sat, 2 Dec 2023 07:00:24 -0500 Subject: feat: add per guild configuration --- src/commands/moderation/config.rs | 140 ++++++++++++++++++++++++++++++++++++++ src/commands/moderation/mod.rs | 3 + 2 files changed, 143 insertions(+) create mode 100644 src/commands/moderation/config.rs create mode 100644 src/commands/moderation/mod.rs (limited to 'src/commands/moderation') diff --git a/src/commands/moderation/config.rs b/src/commands/moderation/config.rs new file mode 100644 index 0000000..2d1410c --- /dev/null +++ b/src/commands/moderation/config.rs @@ -0,0 +1,140 @@ +use crate::settings::{Settings, SettingsProperties}; +use crate::Context; + +use color_eyre::eyre::{eyre, Context as _, ContextCompat, Result}; +use log::*; +use poise::serenity_prelude::{GuildChannel, ReactionType}; + +#[poise::command( + slash_command, + subcommands("set", "get"), + default_member_permissions = "MANAGE_GUILD" +)] +pub async fn config(_ctx: Context<'_>) -> Result<()> { + Ok(()) +} + +#[poise::command(slash_command, ephemeral, guild_only)] +pub async fn set( + ctx: Context<'_>, + #[channel_types("Text")] + #[description = "Where to redirect pins from channels. If empty (the default), the PinBoard is disabled."] + pinboard_channel: Option, + #[channel_types("Text")] + #[description = "A channel that PinBoard will redirect pins from. This will be all channels if empty."] + pinboard_watch: Option, + #[channel_types("Text")] + #[description = "Where to post messages that made it to the ReactBoard. If left empty, ReactBoard is disabled."] + reactboard_channel: Option, + #[description = "An emoji that will get messages on the ReactBoard. If empty, ReactBoard is disabled."] + reactboard_reaction: Option, + #[description = "Minimum number of reactions a message needs to make it to the ReactBoard (defaults to 5)"] + reactboard_requirement: Option, + #[description = "Enables 'extra' commands like teawiespam and copypasta. Defaults to false."] + optional_commands_enabled: Option, +) -> Result<()> { + let redis = &ctx.data().redis; + let gid = ctx.guild_id().unwrap_or_default(); + let mut settings = Settings::from_redis(redis, &gid).await?; + let previous_settings = settings.clone(); + + if let Some(channel) = pinboard_channel { + 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 {} in Redis", gid); + + settings.pinboard_watch = Some(new); + } + } + + if let Some(channel) = reactboard_channel { + debug!( + "Setting reactboard_channel to {channel} for {} in Redis", + gid + ); + + settings.reactboard_channel = Some(channel.id); + } + + if let Some(requirement) = reactboard_requirement { + debug!( + "Setting reactboard_requirement to {requirement} for {} in Redis", + gid + ); + + settings.reactboard_requirement = Some(requirement); + } + + if let Some(reaction) = reactboard_reaction { + let emoji = reaction + .parse::() + .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 {} in Redis", gid); + + settings.reactboard_reactions = Some(new); + } + } + + if let Some(enabled) = optional_commands_enabled { + debug!( + "Setting optional_commands_enabled to {enabled} for {} in Redis", + gid + ); + + settings.optional_commands_enabled = enabled; + } + + if previous_settings != settings { + settings.save(redis).await?; + ctx.reply("Configuration updated!").await?; + } else { + ctx.reply("No changes made, so i'm not updating anything") + .await?; + } + + Ok(()) +} + +#[poise::command(slash_command, ephemeral, guild_only)] +pub async fn get( + ctx: Context<'_>, + #[description = "The setting you want to get"] setting: SettingsProperties, +) -> Result<()> { + let gid = &ctx + .guild_id() + .wrap_err_with(|| eyre!("Failed to get GuildId from context!"))?; + + let settings = Settings::from_redis(&ctx.data().redis, gid).await?; + + let value = match setting { + SettingsProperties::GuildId => settings.guild_id.to_string(), + SettingsProperties::PinBoardChannel => format!("{:#?}", settings.pinboard_channel), + SettingsProperties::PinBoardWatch => format!("{:#?}", settings.pinboard_watch), + SettingsProperties::ReactBoardChannel => format!("{:#?}", settings.reactboard_channel), + SettingsProperties::ReactBoardRequirement => { + format!("{:?}", settings.reactboard_requirement) + } + SettingsProperties::ReactBoardReactions => format!("{:?}", settings.reactboard_reactions), + SettingsProperties::OptionalCommandsEnabled => { + settings.optional_commands_enabled.to_string() + } + }; + + ctx.send(|m| m.embed(|e| e.field(setting, value, false))) + .await?; + + Ok(()) +} diff --git a/src/commands/moderation/mod.rs b/src/commands/moderation/mod.rs new file mode 100644 index 0000000..d54b3f8 --- /dev/null +++ b/src/commands/moderation/mod.rs @@ -0,0 +1,3 @@ +mod config; + +pub use config::config; -- cgit v1.2.3