Added simple swear filter logic
parent
1eb0a7edcc
commit
9eac89a1be
13
src/utils.rs
13
src/utils.rs
|
@ -32,7 +32,6 @@ pub mod utils {
|
|||
None
|
||||
}
|
||||
|
||||
|
||||
pub fn create_swear_list(swear_list_url: &str) -> Result<Vec<String>> {
|
||||
// Downloads the text file
|
||||
let resp = reqwest::blocking::get(swear_list_url).expect("request failed");
|
||||
|
@ -44,5 +43,17 @@ pub mod utils {
|
|||
BufReader::new(File::open("list.txt")?).lines().collect()
|
||||
}
|
||||
|
||||
pub fn detect_swear_from_message(swear_list: &Vec<String>, message: &str) -> bool {
|
||||
let message_words = message.split_whitespace();
|
||||
for words in message_words {
|
||||
for swear in swear_list {
|
||||
if words.contains(swear) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use matrix_modbot::utils::utils::{detect_swear_from_message, create_swear_list};
|
||||
use matrix_modbot::config_reader::config_reader::BotUserInfo;
|
||||
|
||||
#[test]
|
||||
fn test_detect_swear() {
|
||||
let creds = BotUserInfo::get_info("tests/test_creds.ini").unwrap();
|
||||
let swear_list = create_swear_list(&creds.swear_list_url).unwrap();
|
||||
assert!(detect_swear_from_message(&swear_list, "fuck you"));
|
||||
assert!(!detect_swear_from_message(&swear_list, "This isn't a swear"));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue