diff --git a/src/commands/command_macro/list.rs b/src/commands/command_macro/list.rs index efb894a..beb7b43 100644 --- a/src/commands/command_macro/list.rs +++ b/src/commands/command_macro/list.rs @@ -2,7 +2,7 @@ use poise::CreateReply; use crate::{ component_models::pager::{MacroPager, Pager}, - consts::{EMBED_DESCRIPTION_MAX_LENGTH, THEME_COLOR}, + consts::THEME_COLOR, models::{command_macro::CommandMacro, CtxData}, Context, Error, }; @@ -30,27 +30,7 @@ pub async fn list_macro(ctx: Context<'_>) -> Result<(), Error> { } pub fn max_macro_page(macros: &[CommandMacro]) -> usize { - let mut skipped_char_count = 0; - - macros - .iter() - .map(|m| { - if let Some(description) = &m.description { - format!("**{}**\n- *{}*\n- Has {} commands", m.name, description, m.commands.len()) - } else { - format!("**{}**\n- Has {} commands", m.name, m.commands.len()) - } - }) - .fold(1, |mut pages, p| { - skipped_char_count += p.len(); - - if skipped_char_count > EMBED_DESCRIPTION_MAX_LENGTH { - skipped_char_count = p.len(); - pages += 1; - } - - pages - }) + ((macros.len() as f64) / 25.0).ceil() as usize } pub fn show_macro_page(macros: &[CommandMacro], page: usize) -> CreateReply { @@ -75,45 +55,27 @@ pub fn show_macro_page(macros: &[CommandMacro], page: usize) -> Crea page = pages - 1; } - let mut char_count = 0; - let mut skipped_char_count = 0; + let lower = (page * 25).min(macros.len()); + let upper = ((page + 1) * 25).min(macros.len()); - let mut skipped_pages = 0; - - let display_vec: Vec = macros - .iter() - .map(|m| { - if let Some(description) = &m.description { - format!("**{}**\n- *{}*\n- Has {} commands", m.name, description, m.commands.len()) - } else { - format!("**{}**\n- Has {} commands", m.name, m.commands.len()) - } - }) - .skip_while(|p| { - skipped_char_count += p.len(); - - if skipped_char_count > EMBED_DESCRIPTION_MAX_LENGTH { - skipped_char_count = p.len(); - skipped_pages += 1; - } - - skipped_pages < page - }) - .take_while(|p| { - char_count += p.len(); - - char_count < EMBED_DESCRIPTION_MAX_LENGTH - }) - .collect::>(); - - let display = display_vec.join("\n"); + let fields = macros[lower..upper].iter().map(|m| { + if let Some(description) = &m.description { + ( + m.name.clone(), + format!("*{}*\n- Has {} commands", description, m.commands.len()), + true, + ) + } else { + (m.name.clone(), format!("- Has {} commands", m.commands.len()), true) + } + }); let mut reply = CreateReply::default(); reply .embed(|e| { e.title("Macros") - .description(display) + .fields(fields) .footer(|f| f.text(format!("Page {} of {}", page + 1, pages))) .color(*THEME_COLOR) }) diff --git a/src/commands/command_macro/record.rs b/src/commands/command_macro/record.rs index 0b5c4f9..4ff5f3f 100644 --- a/src/commands/command_macro/record.rs +++ b/src/commands/command_macro/record.rs @@ -15,6 +15,18 @@ pub async fn record_macro( #[description = "Name for the new macro"] name: String, #[description = "Description for the new macro"] description: Option, ) -> Result<(), Error> { + if name.len() > 100 { + ctx.say("Name must be less than 100 characters").await?; + + return Ok(()); + } + + if description.as_ref().map_or(0, |d| d.len()) > 100 { + ctx.say("Description must be less than 100 characters").await?; + + return Ok(()); + } + let guild_id = ctx.guild_id().unwrap(); let row = sqlx::query!( diff --git a/src/commands/moderation_cmds.rs b/src/commands/moderation_cmds.rs index bb21b5e..666bd22 100644 --- a/src/commands/moderation_cmds.rs +++ b/src/commands/moderation_cmds.rs @@ -1,6 +1,7 @@ use chrono::offset::Utc; use chrono_tz::{Tz, TZ_VARIANTS}; use levenshtein::levenshtein; +use log::warn; use super::autocomplete::timezone_autocomplete; use crate::{consts::THEME_COLOR, models::CtxData, Context, Error}; @@ -157,23 +158,24 @@ pub async fn webhook(ctx: Context<'_>) -> Result<(), Error> { match ctx.channel_data().await { Ok(data) => { if let (Some(id), Some(token)) = (data.webhook_id, data.webhook_token) { - let _ = ctx - .send(|b| { - b.ephemeral(true).content(format!( - "**Warning!** + ctx.send(|b| { + b.ephemeral(true).content(format!( + "**Warning!** This link can be used by users to anonymously send messages, with or without permissions. Do not share it! || https://discord.com/api/webhooks/{}/{} ||", - id, token, - )) - }) - .await; + id, token, + )) + }) + .await?; } else { - let _ = ctx.say("No webhook configured on this channel.").await; + ctx.say("No webhook configured on this channel.").await?; } } - Err(_) => { - let _ = ctx.say("No webhook configured on this channel.").await; + Err(e) => { + warn!("Error fetching channel data: {:?}", e); + + ctx.say("No webhook configured on this channel.").await?; } } diff --git a/src/consts.rs b/src/consts.rs index f63f0fb..d2b3c2d 100644 --- a/src/consts.rs +++ b/src/consts.rs @@ -2,7 +2,7 @@ pub const DAY: u64 = 86_400; pub const HOUR: u64 = 3_600; pub const MINUTE: u64 = 60; -pub const EMBED_DESCRIPTION_MAX_LENGTH: usize = 4000; +pub const EMBED_DESCRIPTION_MAX_LENGTH: usize = 4096; pub const SELECT_MAX_ENTRIES: usize = 25; pub const CHARACTERS: &str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";