Skip to content

Commit

Permalink
Implement skip_emoji setting
Browse files Browse the repository at this point in the history
Closes #84
  • Loading branch information
GnomedDev committed Sep 7, 2024
1 parent d92e650 commit 50d494b
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 20 deletions.
9 changes: 9 additions & 0 deletions tts_commands/src/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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}`**
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
Expand Down
48 changes: 31 additions & 17 deletions tts_core/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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: &regex::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(' ')
Expand Down Expand Up @@ -196,6 +217,7 @@ pub fn clean_msg(

voice: &str,
xsaid: bool,
skip_emoji: bool,
repeated_limit: Option<NonZeroU8>,
nickname: Option<&str>,
use_new_formatting: bool,
Expand All @@ -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: &regex::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 &regex_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()
Expand Down
3 changes: 3 additions & 0 deletions tts_core/src/database_models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions tts_core/src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ pub struct RegexCache {
pub replacements: [(regex::Regex, &'static str); 3],
pub bot_mention: OnceLock<regex::Regex>,
pub id_in_brackets: regex::Regex,
pub emoji: regex::Regex,
pub emoji_captures: regex::Regex,
pub emoji_filter: regex::Regex,
}

impl RegexCache {
Expand All @@ -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:<a?:[^<>]+:\d+>)|\p{Emoji}")?,
bot_mention: OnceLock::new(),
})
}
Expand Down
1 change: 1 addition & 0 deletions tts_events/src/message/tts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
3 changes: 2 additions & 1 deletion tts_migrations/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 50d494b

Please sign in to comment.