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

✨ 添加暗语相关方法和依赖注入 #5

Merged
merged 1 commit into from
Aug 19, 2023
Merged
Show file tree
Hide file tree
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
17 changes: 9 additions & 8 deletions kirami/depends.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
from collections.abc import AsyncGenerator
from datetime import time
from re import Match
from typing import Annotated, Any, Literal, TypeAlias
from typing import Annotated, Any, Literal, TypeAlias, TypeVar

from httpx._types import ProxiesTypes, VerifyTypes
from nonebot.exception import ParserExit
from nonebot.internal.params import Arg as useArg
from nonebot.internal.params import ArgInner
from nonebot.internal.params import ArgPlainText as useArgPlainText
from nonebot.internal.params import ArgStr as useArgStr
from nonebot.internal.params import DependsInner as BaseDependsInner
Expand Down Expand Up @@ -44,7 +45,6 @@
Message,
MessageEvent,
MessageSegment,
State,
T_Handler,
)
from kirami.utils import (
Expand Down Expand Up @@ -294,14 +294,15 @@ async def client_session() -> AsyncGenerator[AsyncClient, None]:
"""网络连接会话对象"""


def useArgotArg(key: str | None = None) -> Any:
"""提取暗语中的字段"""
def useArgot(key: str | None = None) -> Any:
"""提取暗语的内容"""
return ArgInner(key, type="argot") # type: ignore

@depends
def argot_arg(state: State) -> Any:
return state.argot[key] if key else state.argot

return argot_arg
T = TypeVar("T")

Argot = Annotated[T, useArgot()]
"""暗语内容"""


def useWebContext(**kwargs) -> BrowserContext:
Expand Down
13 changes: 12 additions & 1 deletion kirami/matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import re
import time
from datetime import datetime, timedelta
from typing import TYPE_CHECKING, Any, Literal, NoReturn
from typing import TYPE_CHECKING, Any, Literal, NoReturn, TypeVar

from nonebot import get_bots
from nonebot.adapters import Bot, Event
Expand Down Expand Up @@ -46,6 +46,8 @@
from kirami.state import State
from kirami.utils import scheduler

T = TypeVar("T")

_param_rules = {
"to_me": ToMeRule,
"reply": ReplyRule,
Expand All @@ -62,6 +64,8 @@ def _extend_rule(rule: Rule | T_RuleChecker | None, **kwargs) -> Rule:


class Matcher(BaseMatcher):
state: State

@classmethod
def destroy(cls) -> None:
for checker in cls.rule.checkers:
Expand Down Expand Up @@ -125,6 +129,13 @@ async def finish(
"""
await super().finish(message, **kwargs)

def get_argot(self, key: str | None = None, default: T | None = None) -> Any | T:
"""获取指定的 `argot` 内容

如果没有找到对应的内容,返回 `default` 值
"""
return self.state.argot.get(key, default) if key else self.state.argot


class MatcherCase:
"""事件响应器容器类,用于包装事件响应器,以使其支持简写形式"""
Expand Down
11 changes: 10 additions & 1 deletion kirami/matcher.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import re
from datetime import datetime, timedelta, tzinfo
from typing import Any, Literal, NoReturn, overload
from typing import Any, Literal, NoReturn, TypeVar, overload

from nonebot.dependencies import Dependent
from nonebot.matcher import Matcher as BaseMatcher
Expand All @@ -24,7 +24,10 @@ from kirami.typing import (

# ruff: noqa: PYI021

_T = TypeVar("_T")

class Matcher(BaseMatcher):
state: State
@classmethod
async def send(
cls,
Expand All @@ -47,6 +50,12 @@ class Matcher(BaseMatcher):
argot_content: dict[str, Any] | None = None,
**kwargs,
) -> NoReturn: ...
@overload
def get_argot(self, key: None = None) -> dict[str, Any]: ...
@overload
def get_argot(self, key: str, default: _T) -> Any | _T: ...
@overload
def get_argot(self, key: str, default: None = None) -> Any | None: ...

class MatcherCase(Matcher):
matcher: type[Matcher] = ...
Expand Down
24 changes: 24 additions & 0 deletions kirami/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,27 @@ def get_plaintext(self: Event) -> str:


Event.get_plaintext = get_plaintext

# ==============================================================================

from nonebot.internal.params import ArgParam

from kirami.matcher import Matcher


async def _solve(self: ArgParam, matcher: Matcher, **_kwargs: Any) -> Any:
"""支持 Argot"""
key: str = self.extra["key"]
if self.extra["type"] == "argot":
return matcher.get_argot(key)
message = matcher.get_arg(key)
if message is None:
return message
if self.extra["type"] == "message":
return message
if self.extra["type"] == "str":
return str(message)
return message.extract_plain_text()


ArgParam._solve = _solve # type: ignore