Xd and xd commands + match_case parameter
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
LordMZTE 2021-07-10 21:40:37 +02:00
parent 58569bae3f
commit 568607fe3e
5 changed files with 47 additions and 4 deletions

View file

@ -1,3 +1,6 @@
# 0.2.4 # 0.2.4
- fix match = "contains" commands matching when keyword is at the start of message, and there are invalid characters after it. - fix match = "contains" commands matching when keyword is at the start of message, and there are invalid characters after it.
- add tests - add tests
- only 1 meme per message
- add Xd and xd commands
- add match_case option

View file

@ -8,6 +8,7 @@ store_path = "store"
# MEMES!! # MEMES!!
memes = [ memes = [
# random stuff # random stuff
{ keyword = "Xd", id = 1023, match = "contains", match_case = true },
{ keyword = "alec", id = 650 }, { keyword = "alec", id = 650 },
{ keyword = "bastard", id = 375 }, { keyword = "bastard", id = 375 },
{ keyword = "drogen", id = 191 }, { keyword = "drogen", id = 191 },
@ -25,6 +26,7 @@ memes = [
{ keyword = "tilera", id = 316 }, { keyword = "tilera", id = 316 },
{ keyword = "wtf", randomcat = "random", match = "contains" }, { keyword = "wtf", randomcat = "random", match = "contains" },
{ keyword = "wtuff", randomcat = "random", match = "contains" }, { keyword = "wtuff", randomcat = "random", match = "contains" },
{ keyword = "xd", id = 1023, match = "contains", match_case = true },
# üffen # üffen
{ keyword = "uff", randomcat = "uff" }, { keyword = "uff", randomcat = "uff" },

View file

@ -32,6 +32,7 @@ impl<'de> Deserialize<'de> for Meme {
Keyword, Keyword,
Ident(IdentField), Ident(IdentField),
Matcher, Matcher,
MatchCase,
} }
enum IdentField { enum IdentField {
@ -64,6 +65,7 @@ impl<'de> Deserialize<'de> for Meme {
"randomcat" => Field::Ident(IdentField::RandomCat), "randomcat" => Field::Ident(IdentField::RandomCat),
"id" => Field::Ident(IdentField::Id), "id" => Field::Ident(IdentField::Id),
"match" => Field::Matcher, "match" => Field::Matcher,
"match_case" => Field::MatchCase,
_ => return Err(de::Error::unknown_field(v, FIELDS)), _ => return Err(de::Error::unknown_field(v, FIELDS)),
}) })
} }
@ -88,6 +90,8 @@ impl<'de> Deserialize<'de> for Meme {
let mut keyword = None; let mut keyword = None;
let mut ident = None; let mut ident = None;
let mut matcher = None; let mut matcher = None;
let mut match_case = None;
while let Some(key) = map.next_key()? { while let Some(key) = map.next_key()? {
match key { match key {
Field::Keyword => { Field::Keyword => {
@ -122,17 +126,27 @@ impl<'de> Deserialize<'de> for Meme {
matcher = Some(map.next_value()?); matcher = Some(map.next_value()?);
} }
Field::MatchCase => {
if match_case.is_some() {
return Err(de::Error::duplicate_field("match"));
}
match_case = Some(map.next_value()?);
}
} }
} }
let keyword = keyword.ok_or_else(|| de::Error::missing_field("keyword"))?; let keyword = keyword.ok_or_else(|| de::Error::missing_field("keyword"))?;
let ident = ident.ok_or_else(|| de::Error::missing_field("ident"))?; let ident = ident.ok_or_else(|| de::Error::missing_field("ident"))?;
let matcher = matcher.unwrap_or(Matcher::Begins); let matcher = matcher.unwrap_or(Matcher::Begins);
let match_case = match_case.unwrap_or(false);
Ok(Meme { Ok(Meme {
keyword, keyword,
ident, ident,
matcher, matcher,
match_case,
}) })
} }
} }

View file

@ -9,6 +9,7 @@ pub struct Meme {
pub keyword: String, pub keyword: String,
pub ident: MemeIdent, pub ident: MemeIdent,
pub matcher: Matcher, pub matcher: Matcher,
pub match_case: bool,
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
@ -21,17 +22,23 @@ pub enum Matcher {
impl Meme { impl Meme {
/// checks if the meme should be triggered for the given message /// checks if the meme should be triggered for the given message
pub fn matches(&self, msg: &str) -> bool { pub fn matches(&self, msg: &str) -> bool {
let msg = msg.to_ascii_lowercase(); let mut msg = msg.to_string();
let mut keyword = self.keyword.clone();
if !self.match_case {
msg = msg.to_ascii_lowercase();
keyword = keyword.to_ascii_lowercase();
}
match self.matcher { match self.matcher {
Matcher::Begins => { Matcher::Begins => {
msg.starts_with(&self.keyword) && msg.starts_with(&keyword) &&
// msg must have one of allowed chars after keyword // msg must have one of allowed chars after keyword
msg.chars().nth(self.keyword.len()).map(|c| ALLOWED_SPACES.contains(c)).unwrap_or(true) msg.chars().nth(keyword.len()).map(|c| ALLOWED_SPACES.contains(c)).unwrap_or(true)
} }
Matcher::Contains => msg Matcher::Contains => msg
.match_indices(&self.keyword) .match_indices(&keyword)
.map(|(idx, subs)| { .map(|(idx, subs)| {
(idx == 0 (idx == 0
|| msg || msg
@ -81,6 +88,7 @@ mod tests {
keyword: String::from("test"), keyword: String::from("test"),
ident: MemeIdent::Id(42), ident: MemeIdent::Id(42),
matcher: Matcher::Begins, matcher: Matcher::Begins,
match_case: false,
}; };
assert!(!meme.matches("xxx")); assert!(!meme.matches("xxx"));
@ -102,6 +110,7 @@ mod tests {
keyword: String::from("test"), keyword: String::from("test"),
ident: MemeIdent::Id(42), ident: MemeIdent::Id(42),
matcher: Matcher::Contains, matcher: Matcher::Contains,
match_case: false,
}; };
assert!(!meme.matches("xxx")); assert!(!meme.matches("xxx"));
@ -115,4 +124,17 @@ mod tests {
assert!(meme.matches("xxx,TEST.xxx")); assert!(meme.matches("xxx,TEST.xxx"));
assert!(meme.matches("xxx,TeSt.xxx")); assert!(meme.matches("xxx,TeSt.xxx"));
} }
#[test]
fn matches_case_test() {
let meme = Meme {
keyword: String::from("TeSt"),
ident: MemeIdent::Id(42),
matcher: Matcher::Contains,
match_case: true,
};
assert!(!meme.matches("test"));
assert!(meme.matches("TeSt"));
}
} }

View file

@ -139,6 +139,8 @@ pub async fn on_msg(msg: &str, room: Room, bot: &Bot) -> anyhow::Result<()> {
} else { } else {
error!("Found meme with invalid id! {:?}", &meme); error!("Found meme with invalid id! {:?}", &meme);
} }
break;
} }
// we do this after we have responded, in order to not delay the response // we do this after we have responded, in order to not delay the response