检查消息纯文本是否包含指定关键字。 参数: keywords: 指定关键字元组
| 300 | |
| 301 | |
| 302 | class KeywordsRule: |
| 303 | """检查消息纯文本是否包含指定关键字。 |
| 304 | |
| 305 | 参数: |
| 306 | keywords: 指定关键字元组 |
| 307 | """ |
| 308 | |
| 309 | __slots__ = ("keywords",) |
| 310 | |
| 311 | def __init__(self, *keywords: str): |
| 312 | self.keywords = keywords |
| 313 | |
| 314 | def __repr__(self) -> str: |
| 315 | return f"Keywords(keywords={self.keywords})" |
| 316 | |
| 317 | def __eq__(self, other: object) -> bool: |
| 318 | return isinstance(other, KeywordsRule) and frozenset( |
| 319 | self.keywords |
| 320 | ) == frozenset(other.keywords) |
| 321 | |
| 322 | def __hash__(self) -> int: |
| 323 | return hash(frozenset(self.keywords)) |
| 324 | |
| 325 | async def __call__(self, event: Event, state: T_State) -> bool: |
| 326 | try: |
| 327 | text = event.get_plaintext() |
| 328 | except Exception: |
| 329 | return False |
| 330 | if not text: |
| 331 | return False |
| 332 | if key := next((k for k in self.keywords if k in text), None): |
| 333 | state[KEYWORD_KEY] = key |
| 334 | return True |
| 335 | return False |
| 336 | |
| 337 | |
| 338 | def keyword(*keywords: str) -> Rule: |