From 50d494b997bc07efbc49902704279f568bd6042d Mon Sep 17 00:00:00 2001 From: GnomedDev Date: Sat, 7 Sep 2024 21:08:14 +0100 Subject: [PATCH] Implement skip_emoji setting Closes #84 --- tts_commands/src/settings/mod.rs | 9 ++++++ tts_core/src/common.rs | 48 +++++++++++++++++++++----------- tts_core/src/database_models.rs | 3 ++ tts_core/src/structs.rs | 6 ++-- tts_events/src/message/tts.rs | 1 + tts_migrations/src/lib.rs | 3 +- 6 files changed, 50 insertions(+), 20 deletions(-) diff --git a/tts_commands/src/settings/mod.rs b/tts_commands/src/settings/mod.rs index 5e260f9..01deb46 100644 --- a/tts_commands/src/settings/mod.rs +++ b/tts_commands/src/settings/mod.rs @@ -139,6 +139,7 @@ pub async fn settings(ctx: Context<'_>) -> CommandResult { let autojoin = guild_row.auto_join(); let msg_length = guild_row.msg_length; let bot_ignore = guild_row.bot_ignore(); + let skip_emoji = guild_row.skip_emoji(); let guild_mode: &str = guild_mode.into(); let to_translate = guild_row.to_translate(); let require_voice = guild_row.require_voice(); @@ -171,6 +172,7 @@ pub async fn settings(ctx: Context<'_>) -> CommandResult { {sep2} Require users in voice channel: `{require_voice}` {sep2} Required prefix for TTS: `{required_prefix}` {sep2} Read from Text in Voice channels: `{text_in_voice}` +{sep2} Skip emojis when reading messages: `{skip_emoji}` **{sep2} Default Server Voice Mode: `{guild_mode}`** **{sep2} Default Server Voice: `{default_voice}`** @@ -537,6 +539,12 @@ create_bool_command!( "text_in_voice", aliases(), ); +create_bool_command!( + "Makes the bot skip emoji within messages", + skip_emoji, + "skip_emoji", + aliases("skip_emojis"), +); create_bool_command!( "Makes the bot translate all TTS messages to the same language", translation, @@ -1345,6 +1353,7 @@ pub fn commands() -> [Command; 5] { required_prefix(), command_prefix(), text_in_voice(), + skip_emoji(), owner::block(), owner::bot_ban(), owner::gtts_disabled(), diff --git a/tts_core/src/common.rs b/tts_core/src/common.rs index 2e95b47..a37c1a9 100644 --- a/tts_core/src/common.rs +++ b/tts_core/src/common.rs @@ -127,6 +127,27 @@ pub fn random_footer(server_invite: &str, client_id: serenity::UserId) -> Cow<'s } } +fn strip_emoji<'c>(regex_cache: &RegexCache, content: &'c str) -> Cow<'c, str> { + regex_cache.emoji_filter.replace_all(content, "") +} + +fn make_emoji_readable<'c>(regex_cache: &RegexCache, content: &'c str) -> Cow<'c, str> { + regex_cache + .emoji_captures + .replace_all(content, |re_match: ®ex::Captures<'_>| { + let is_animated = re_match.get(1).unwrap().as_str(); + let emoji_name = re_match.get(2).unwrap().as_str(); + + let emoji_prefix = if is_animated.is_empty() { + "emoji" + } else { + "animated emoji" + }; + + format!("{emoji_prefix} {emoji_name}") + }) +} + fn parse_acronyms(original: &str) -> String { original .split(' ') @@ -196,6 +217,7 @@ pub fn clean_msg( voice: &str, xsaid: bool, + skip_emoji: bool, repeated_limit: Option, nickname: Option<&str>, use_new_formatting: bool, @@ -206,28 +228,20 @@ pub fn clean_msg( let (contained_url, mut content) = if content == "?" { (false, String::from("what")) } else { - let mut content: String = regex_cache - .emoji - .replace_all(content, |re_match: ®ex::Captures<'_>| { - let is_animated = re_match.get(1).unwrap().as_str(); - let emoji_name = re_match.get(2).unwrap().as_str(); - - let emoji_prefix = if is_animated.is_empty() { - "emoji" - } else { - "animated emoji" - }; - - format!("{emoji_prefix} {emoji_name}") - }) - .into_owned(); + let mut content = if skip_emoji { + strip_emoji(regex_cache, content) + } else { + make_emoji_readable(regex_cache, content) + }; for (regex, replacement) in ®ex_cache.replacements { - content = regex.replace_all(&content, *replacement).into_owned(); + if let Cow::Owned(replaced) = regex.replace_all(&content, *replacement) { + content = Cow::Owned(replaced); + } } if voice.starts_with("en") { - content = parse_acronyms(&content); + content = Cow::Owned(parse_acronyms(&content)); } let filtered_content: String = linkify::LinkFinder::new() diff --git a/tts_core/src/database_models.rs b/tts_core/src/database_models.rs index e4d0145..1f32b9a 100644 --- a/tts_core/src/database_models.rs +++ b/tts_core/src/database_models.rs @@ -35,6 +35,7 @@ pub struct GuildRowRaw { pub xsaid: bool, pub auto_join: bool, pub bot_ignore: bool, + pub skip_emoji: bool, pub to_translate: bool, pub require_voice: bool, pub text_in_voice: bool, @@ -56,6 +57,7 @@ pub struct GuildRow { pub xsaid: bool, pub auto_join: bool, pub bot_ignore: bool, + pub skip_emoji: bool, pub to_translate: bool, pub require_voice: bool, pub text_in_voice: bool, @@ -103,6 +105,7 @@ impl Compact for GuildRowRaw { .set_xsaid(self.xsaid) .set_auto_join(self.auto_join) .set_bot_ignore(self.bot_ignore) + .set_skip_emoji(self.skip_emoji) .set_to_translate(self.to_translate) .set_require_voice(self.require_voice) .set_text_in_voice(self.text_in_voice) diff --git a/tts_core/src/structs.rs b/tts_core/src/structs.rs index d884445..e23e38a 100644 --- a/tts_core/src/structs.rs +++ b/tts_core/src/structs.rs @@ -137,7 +137,8 @@ pub struct RegexCache { pub replacements: [(regex::Regex, &'static str); 3], pub bot_mention: OnceLock, pub id_in_brackets: regex::Regex, - pub emoji: regex::Regex, + pub emoji_captures: regex::Regex, + pub emoji_filter: regex::Regex, } impl RegexCache { @@ -152,7 +153,8 @@ impl RegexCache { (regex::Regex::new(r"`(?s:.)*?`")?, ". code snippet."), ], id_in_brackets: regex::Regex::new(r"\((\d+)\)")?, - emoji: regex::Regex::new(r"<(a?):([^<>]+):\d+>")?, + emoji_captures: regex::Regex::new(r"<(a?):([^<>]+):\d+>")?, + emoji_filter: regex::Regex::new(r"(?s:]+:\d+>)|\p{Emoji}")?, bot_mention: OnceLock::new(), }) } diff --git a/tts_events/src/message/tts.rs b/tts_events/src/message/tts.rs index 0fc30d8..a6219c8 100644 --- a/tts_events/src/message/tts.rs +++ b/tts_events/src/message/tts.rs @@ -74,6 +74,7 @@ pub(crate) async fn process_tts_msg( &message.attachments, &voice, guild_row.xsaid(), + guild_row.skip_emoji(), guild_row.repeated_chars, nickname_row.name.as_deref(), user_row.use_new_formatting(), diff --git a/tts_migrations/src/lib.rs b/tts_migrations/src/lib.rs index c375600..0d42023 100644 --- a/tts_migrations/src/lib.rs +++ b/tts_migrations/src/lib.rs @@ -188,7 +188,8 @@ async fn _run( ADD COLUMN IF NOT EXISTS require_voice bool DEFAULT True, ADD COLUMN IF NOT EXISTS required_role bigint, ADD COLUMN IF NOT EXISTS required_prefix varchar(6), - ADD COLUMN IF NOT EXISTS text_in_voice bool DEFAULT True; + ADD COLUMN IF NOT EXISTS text_in_voice bool DEFAULT True, + ADD COLUMN IF NOT EXISTS skip_emoji bool DEFAULT False; ALTER TABLE user_voice ADD COLUMN IF NOT EXISTS speaking_rate real;