Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ 添加消息选择判断的依赖注入 #7

Merged
merged 2 commits into from
Aug 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions kirami/depends.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,3 +503,44 @@ async def check_lock(matcher: Matcher, event: Event) -> AsyncGenerator[Lock, Non
lock.unclaim(key)

return check_lock


def useConfirm(*keywords: str) -> bool:
"""检查消息是否表示确认"""

keywords = keywords or ("确认", "确定", "是", "yes", "y")

@depends
async def dependency(message: EventPlainText) -> bool:
return any(kw == message for kw in keywords)

return dependency


Confirm: TypeAlias = Annotated[bool, useConfirm()]


def useCancel(*keywords: str) -> bool:
"""检查消息是否表示取消"""

keywords = keywords or ("取消", "退出", "否", "no", "n")

@depends
async def dependency(message: EventPlainText) -> bool:
return any(kw == message for kw in keywords)

return dependency


Cancel: TypeAlias = Annotated[bool, useCancel()]


def handleCancel(*keywords: str, prompt: str | None = None) -> None:
"""检查消息是否表示取消,并结束事件处理"""

@depends
async def dependency(matcher: Matcher, cancel: bool = useCancel(*keywords)) -> None:
if cancel:
await matcher.finish(prompt)

return dependency