keyword 支持正则表达式支持 (#52)

Co-authored-by: Sunn Yao <sunyour@gmail.com>
Co-authored-by: Sourcery AI <>
Co-authored-by: omg-xtao <100690902+omg-xtao@users.noreply.github.com>
This commit is contained in:
sourcery-ai[bot] 2022-07-23 01:53:05 +08:00 committed by GitHub
parent cf1a6d9ed8
commit d522b509c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 7 deletions

View File

@ -1,4 +1,5 @@
import contextlib import contextlib
import re
from typing import Optional, List from typing import Optional, List
@ -20,6 +21,7 @@ class KeywordTask:
key: str key: str
msg: str msg: str
include: bool include: bool
regexp: bool
exact: bool exact: bool
case: bool case: bool
ignore_forward: bool ignore_forward: bool
@ -29,7 +31,7 @@ class KeywordTask:
restrict: int restrict: int
delay_delete: int delay_delete: int
def __init__(self, task_id: Optional[int] = None, cid: int = 0, key: str = "", msg: str = "", include: bool = True, def __init__(self, task_id: Optional[int] = None, cid: int = 0, key: str = "", msg: str = "", include: bool = True, regexp: bool = False,
exact: bool = False, case: bool = False, ignore_forward: bool = False, exact: bool = False, case: bool = False, ignore_forward: bool = False,
reply: bool = True, delete: bool = False, ban: int = 0, restrict: int = 0, delay_delete: int = 0): reply: bool = True, delete: bool = False, ban: int = 0, restrict: int = 0, delay_delete: int = 0):
self.task_id = task_id self.task_id = task_id
@ -37,6 +39,7 @@ class KeywordTask:
self.key = key self.key = key
self.msg = msg self.msg = msg
self.include = include self.include = include
self.regexp = regexp
self.exact = exact self.exact = exact
self.case = case self.case = case
self.ignore_forward = ignore_forward self.ignore_forward = ignore_forward
@ -47,7 +50,7 @@ class KeywordTask:
self.delay_delete = delay_delete self.delay_delete = delay_delete
def export(self): def export(self):
return {"task_id": self.task_id, "cid": self.cid, "key": self.key, "msg": self.msg, "include": self.include, return {"task_id": self.task_id, "cid": self.cid, "key": self.key, "msg": self.msg, "include": self.include, "regexp": self.regexp,
"exact": self.exact, "case": self.case, "ignore_forward": self.ignore_forward, "reply": self.reply, "exact": self.exact, "case": self.case, "ignore_forward": self.ignore_forward, "reply": self.reply,
"delete": self.delete, "ban": self.ban, "restrict": self.restrict, "delay_delete": self.delay_delete} "delete": self.delete, "ban": self.ban, "restrict": self.restrict, "delay_delete": self.delay_delete}
@ -69,20 +72,20 @@ class KeywordTask:
sqlite["keyword_tasks"] = data sqlite["keyword_tasks"] = data
def check_need_reply(self, message: Message) -> bool: def check_need_reply(self, message: Message) -> bool:
if not (message.text or message.caption): if not message.text and not message.caption:
return False return False
if self.ignore_forward and message.forward_date: if self.ignore_forward and message.forward_date:
return False return False
text = message.text or message.caption text = message.text or message.caption
key = self.key key = self.key
if self.regexp:
return re.search(key,text)
if not self.case: if not self.case:
text = text.lower() text = text.lower()
key = key.lower() key = key.lower()
if self.include and text.find(key) != -1: if self.include and text.find(key) != -1:
return True return True
if self.exact and text == key: return bool(self.exact and text == key)
return True
return False
@staticmethod @staticmethod
def mention_chat(chat: Chat): def mention_chat(chat: Chat):
@ -151,6 +154,8 @@ class KeywordTask:
self.include = True self.include = True
elif i.startswith("exact"): elif i.startswith("exact"):
self.exact = True self.exact = True
elif i.startswith("regexp"):
self.regexp = True
elif i.startswith("case"): elif i.startswith("case"):
self.case = True self.case = True
elif i.startswith("ignore_forward"): elif i.startswith("ignore_forward"):

View File

@ -312,7 +312,7 @@
}, },
{ {
"name": "keyword", "name": "keyword",
"version": "1.002", "version": "1.003",
"section": "chat", "section": "chat",
"maintainer": "xtaodada", "maintainer": "xtaodada",
"size": "10.9 kb", "size": "10.9 kb",