MCPcopy
hub / github.com/nonebot/nonebot2 / StartswithRule

Class StartswithRule

nonebot/rule.py:138–177  ·  view source on GitHub ↗

检查消息纯文本是否以指定字符串开头。 参数: msg: 指定消息开头字符串元组 ignorecase: 是否忽略大小写

Source from the content-addressed store, hash-verified

136
137
138class StartswithRule:
139 """检查消息纯文本是否以指定字符串开头。
140
141 参数:
142 msg: 指定消息开头字符串元组
143 ignorecase: 是否忽略大小写
144 """
145
146 __slots__ = ("ignorecase", "msg")
147
148 def __init__(self, msg: tuple[str, ...], ignorecase: bool = False):
149 self.msg = msg
150 self.ignorecase = ignorecase
151
152 def __repr__(self) -> str:
153 return f"Startswith(msg={self.msg}, ignorecase={self.ignorecase})"
154
155 def __eq__(self, other: object) -> bool:
156 return (
157 isinstance(other, StartswithRule)
158 and frozenset(self.msg) == frozenset(other.msg)
159 and self.ignorecase == other.ignorecase
160 )
161
162 def __hash__(self) -> int:
163 return hash((frozenset(self.msg), self.ignorecase))
164
165 async def __call__(self, event: Event, state: T_State) -> bool:
166 try:
167 text = event.get_plaintext()
168 except Exception:
169 return False
170 if match := re.match(
171 f"^(?:{'|'.join(re.escape(prefix) for prefix in self.msg)})",
172 text,
173 re.IGNORECASE if self.ignorecase else 0,
174 ):
175 state[STARTSWITH_KEY] = match.group()
176 return True
177 return False
178
179
180def startswith(msg: str | tuple[str, ...], ignorecase: bool = False) -> Rule:

Callers 1

startswithFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected