Xd and xd commands + match_case parameter
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
parent
58569bae3f
commit
568607fe3e
5 changed files with 47 additions and 4 deletions
|
@ -1,3 +1,6 @@
|
|||
# 0.2.4
|
||||
- fix match = "contains" commands matching when keyword is at the start of message, and there are invalid characters after it.
|
||||
- add tests
|
||||
- only 1 meme per message
|
||||
- add Xd and xd commands
|
||||
- add match_case option
|
||||
|
|
|
@ -8,6 +8,7 @@ store_path = "store"
|
|||
# MEMES!!
|
||||
memes = [
|
||||
# random stuff
|
||||
{ keyword = "Xd", id = 1023, match = "contains", match_case = true },
|
||||
{ keyword = "alec", id = 650 },
|
||||
{ keyword = "bastard", id = 375 },
|
||||
{ keyword = "drogen", id = 191 },
|
||||
|
@ -25,6 +26,7 @@ memes = [
|
|||
{ keyword = "tilera", id = 316 },
|
||||
{ keyword = "wtf", randomcat = "random", match = "contains" },
|
||||
{ keyword = "wtuff", randomcat = "random", match = "contains" },
|
||||
{ keyword = "xd", id = 1023, match = "contains", match_case = true },
|
||||
|
||||
# üffen
|
||||
{ keyword = "uff", randomcat = "uff" },
|
||||
|
|
|
@ -32,6 +32,7 @@ impl<'de> Deserialize<'de> for Meme {
|
|||
Keyword,
|
||||
Ident(IdentField),
|
||||
Matcher,
|
||||
MatchCase,
|
||||
}
|
||||
|
||||
enum IdentField {
|
||||
|
@ -64,6 +65,7 @@ impl<'de> Deserialize<'de> for Meme {
|
|||
"randomcat" => Field::Ident(IdentField::RandomCat),
|
||||
"id" => Field::Ident(IdentField::Id),
|
||||
"match" => Field::Matcher,
|
||||
"match_case" => Field::MatchCase,
|
||||
_ => return Err(de::Error::unknown_field(v, FIELDS)),
|
||||
})
|
||||
}
|
||||
|
@ -88,6 +90,8 @@ impl<'de> Deserialize<'de> for Meme {
|
|||
let mut keyword = None;
|
||||
let mut ident = None;
|
||||
let mut matcher = None;
|
||||
let mut match_case = None;
|
||||
|
||||
while let Some(key) = map.next_key()? {
|
||||
match key {
|
||||
Field::Keyword => {
|
||||
|
@ -122,17 +126,27 @@ impl<'de> Deserialize<'de> for Meme {
|
|||
|
||||
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 ident = ident.ok_or_else(|| de::Error::missing_field("ident"))?;
|
||||
let matcher = matcher.unwrap_or(Matcher::Begins);
|
||||
let match_case = match_case.unwrap_or(false);
|
||||
|
||||
Ok(Meme {
|
||||
keyword,
|
||||
ident,
|
||||
matcher,
|
||||
match_case,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
30
src/meme.rs
30
src/meme.rs
|
@ -9,6 +9,7 @@ pub struct Meme {
|
|||
pub keyword: String,
|
||||
pub ident: MemeIdent,
|
||||
pub matcher: Matcher,
|
||||
pub match_case: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
|
@ -21,17 +22,23 @@ pub enum Matcher {
|
|||
impl Meme {
|
||||
/// checks if the meme should be triggered for the given message
|
||||
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 {
|
||||
Matcher::Begins => {
|
||||
msg.starts_with(&self.keyword) &&
|
||||
msg.starts_with(&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
|
||||
.match_indices(&self.keyword)
|
||||
.match_indices(&keyword)
|
||||
.map(|(idx, subs)| {
|
||||
(idx == 0
|
||||
|| msg
|
||||
|
@ -81,6 +88,7 @@ mod tests {
|
|||
keyword: String::from("test"),
|
||||
ident: MemeIdent::Id(42),
|
||||
matcher: Matcher::Begins,
|
||||
match_case: false,
|
||||
};
|
||||
|
||||
assert!(!meme.matches("xxx"));
|
||||
|
@ -102,6 +110,7 @@ mod tests {
|
|||
keyword: String::from("test"),
|
||||
ident: MemeIdent::Id(42),
|
||||
matcher: Matcher::Contains,
|
||||
match_case: false,
|
||||
};
|
||||
|
||||
assert!(!meme.matches("xxx"));
|
||||
|
@ -115,4 +124,17 @@ mod tests {
|
|||
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"));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -139,6 +139,8 @@ pub async fn on_msg(msg: &str, room: Room, bot: &Bot) -> anyhow::Result<()> {
|
|||
} else {
|
||||
error!("Found meme with invalid id! {:?}", &meme);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
// we do this after we have responded, in order to not delay the response
|
||||
|
|
Loading…
Reference in a new issue