Skip to content

Commit

Permalink
Update the regex matching
Browse files Browse the repository at this point in the history
  • Loading branch information
1Git2Clone committed Dec 14, 2024
1 parent 45c6bc7 commit c2b4db7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/data/bot_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ lazy_static! {
temp
};

// https://regex101.com/r/aX8vec/3
pub(crate) static ref EMOJIS_AND_IMAGE_EMOJIS_REGEX: Regex = Regex::new(r"((:|\[)([a-zA-Z0-9_]+)(:|\]))|\([^()]*\)").unwrap();
// https://regex101.com/r/aX8vec/5
pub(crate) static ref EMOJIS_AND_EMBEDS_REGEX: Regex = Regex::new(r"(?<emoji>(:)([a-zA-Z0-9_]+)(:))|(?<embed>(\[)([a-zA-Z0-9_]+)(\])\([^()]*\))").unwrap();
}
4 changes: 2 additions & 2 deletions src/event_handler/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
command_data::{Data, Error},
database_interactions::*,
},
utils::{replies::handle_replies, string_manipulation::remove_emojis_from_str},
utils::{replies::handle_replies, string_manipulation::remove_emojis_and_embeds_from_str},
};
use poise::serenity_prelude as serenity;
use rand::Rng;
Expand Down Expand Up @@ -53,7 +53,7 @@ pub async fn event_handler(
);
let text_patterns = ["hutao", "hu tao"];
let lowercase_msg = msg.to_lowercase();
let trimmed_emojis = remove_emojis_from_str(&lowercase_msg);
let trimmed_emojis = remove_emojis_and_embeds_from_str(&lowercase_msg);
match text_patterns
.iter()
.any(|text| trimmed_emojis.contains(text))
Expand Down
14 changes: 7 additions & 7 deletions src/utils/string_manipulation.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::borrow::Cow;

use crate::data::bot_data::EMOJIS_AND_IMAGE_EMOJIS_REGEX;
use crate::data::bot_data::EMOJIS_AND_EMBEDS_REGEX;

pub fn upper_lowercase_permutations(data: &str) -> Vec<String> {
if data.is_empty() {
Expand All @@ -25,12 +25,12 @@ pub fn upper_lowercase_permutations(data: &str) -> Vec<String> {
/// Removes the emojis from a string.
///
/// ```rust
/// use serenity_discord_bot::utils::string_manipulation::remove_emojis_from_str;
/// use serenity_discord_bot::utils::string_manipulation::remove_emojis_and_embeds_from_str;
///
/// assert_eq!(remove_emojis_from_str(":hutao:"), "");
/// assert_eq!(remove_emojis_from_str(":hutao"), ":hutao");
/// assert_eq!(remove_emojis_from_str("Some longer example : messsage hutao: :hutao:"), "Some longer example : messsage hutao: ");
/// assert_eq!(remove_emojis_and_embeds_from_str(":hutao:"), "");
/// assert_eq!(remove_emojis_and_embeds_from_str(":hutao"), ":hutao");
/// assert_eq!(remove_emojis_and_embeds_from_str("Some longer example : messsage hutao: :hutao:"), "Some longer example : messsage hutao: ");
/// ```
pub fn remove_emojis_from_str(whole_str: &str) -> Cow<str> {
EMOJIS_AND_IMAGE_EMOJIS_REGEX.replace_all(whole_str, "")
pub fn remove_emojis_and_embeds_from_str(whole_str: &str) -> Cow<str> {
EMOJIS_AND_EMBEDS_REGEX.replace_all(whole_str, "")
}

0 comments on commit c2b4db7

Please sign in to comment.