检查消息纯文本是否与指定字符串全匹配。 参数: msg: 指定消息全匹配字符串元组 ignorecase: 是否忽略大小写
| 246 | |
| 247 | |
| 248 | class FullmatchRule: |
| 249 | """检查消息纯文本是否与指定字符串全匹配。 |
| 250 | |
| 251 | 参数: |
| 252 | msg: 指定消息全匹配字符串元组 |
| 253 | ignorecase: 是否忽略大小写 |
| 254 | """ |
| 255 | |
| 256 | __slots__ = ("ignorecase", "msg") |
| 257 | |
| 258 | def __init__(self, msg: tuple[str, ...], ignorecase: bool = False): |
| 259 | self.msg = tuple(map(str.casefold, msg) if ignorecase else msg) |
| 260 | self.ignorecase = ignorecase |
| 261 | |
| 262 | def __repr__(self) -> str: |
| 263 | return f"Fullmatch(msg={self.msg}, ignorecase={self.ignorecase})" |
| 264 | |
| 265 | def __eq__(self, other: object) -> bool: |
| 266 | return ( |
| 267 | isinstance(other, FullmatchRule) |
| 268 | and frozenset(self.msg) == frozenset(other.msg) |
| 269 | and self.ignorecase == other.ignorecase |
| 270 | ) |
| 271 | |
| 272 | def __hash__(self) -> int: |
| 273 | return hash((frozenset(self.msg), self.ignorecase)) |
| 274 | |
| 275 | async def __call__(self, event: Event, state: T_State) -> bool: |
| 276 | try: |
| 277 | text = event.get_plaintext() |
| 278 | except Exception: |
| 279 | return False |
| 280 | if not text: |
| 281 | return False |
| 282 | text = text.casefold() if self.ignorecase else text |
| 283 | if text in self.msg: |
| 284 | state[FULLMATCH_KEY] = text |
| 285 | return True |
| 286 | return False |
| 287 | |
| 288 | |
| 289 | def fullmatch(msg: str | tuple[str, ...], ignorecase: bool = False) -> Rule: |