Skip to content

Commit

Permalink
[Defender] WD: Pattern match for message-contains-word
Browse files Browse the repository at this point in the history
  • Loading branch information
Twentysix26 committed Feb 26, 2022
1 parent 4aac376 commit 2ff4ae2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
6 changes: 3 additions & 3 deletions defender/core/warden/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,11 +462,11 @@ async def message_matches_regex(params: models.IsStr):

@checker(Condition.MessageContainsWord)
async def message_contains_word(params: models.NonEmptyListStr):
to_check = [w.lower() for w in params.value]
message_words = message.content.lower().split()
for word in message_words:
if word in to_check:
return True
for pattern in params.value:
if fnmatch.fnmatch(word, pattern.lower()):
return True
return False

@checker(Condition.UserActivityMatchesAny)
Expand Down
5 changes: 4 additions & 1 deletion defender/tests/test_warden.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,11 +394,14 @@ async def eval_cond(condition: Condition, params, expected_result: bool):
guild=FAKE_GUILD,
message=FAKE_MESSAGE)) is expected_result

FAKE_MESSAGE.content = "aaa 2626 aaa"
FAKE_MESSAGE.content = "aaa 2626 aaa I like cats"
await eval_cond(Condition.MessageMatchesAny, ["abcd", "*hello*"], False)
await eval_cond(Condition.MessageMatchesAny, ["*2626*", "hi", 12345], True)

await eval_cond(Condition.MessageContainsWord, [6, "aa", "2"], False)
await eval_cond(Condition.MessageContainsWord, [111, "111", "AAA"], True)
await eval_cond(Condition.MessageContainsWord, ["c?ts"], True)
await eval_cond(Condition.MessageContainsWord, ["c?t"], False)

FAKE_MESSAGE.attachments = []
await eval_cond(Condition.MessageHasAttachment, "true", False)
Expand Down

0 comments on commit 2ff4ae2

Please sign in to comment.