From def5caedbcb00ce6088cfb3a409fac44cc0a29ea Mon Sep 17 00:00:00 2001 From: yanyongyu Date: Fri, 28 Aug 2020 11:54:21 +0800 Subject: [PATCH] :sparkles: check for reply --- nonebot/adapters/__init__.py | 10 ++++++++++ nonebot/adapters/cqhttp.py | 32 +++++++++++++++++++++++++++----- nonebot/config.py | 4 ++-- nonebot/matcher.py | 2 +- nonebot/message.py | 1 - 5 files changed, 40 insertions(+), 9 deletions(-) diff --git a/nonebot/adapters/__init__.py b/nonebot/adapters/__init__.py index efc34acc..a4d10c2d 100644 --- a/nonebot/adapters/__init__.py +++ b/nonebot/adapters/__init__.py @@ -150,6 +150,16 @@ class BaseEvent(abc.ABC): def message(self, value) -> None: raise NotImplementedError + @property + @abc.abstractmethod + def reply(self) -> Optional[dict]: + raise NotImplementedError + + @reply.setter + @abc.abstractmethod + def reply(self, value) -> None: + raise NotImplementedError + @property @abc.abstractmethod def raw_message(self) -> Optional[str]: diff --git a/nonebot/adapters/cqhttp.py b/nonebot/adapters/cqhttp.py index 57742bc3..6424c1d0 100644 --- a/nonebot/adapters/cqhttp.py +++ b/nonebot/adapters/cqhttp.py @@ -57,6 +57,19 @@ def _b2s(b: Optional[bool]) -> Optional[str]: return b if b is None else str(b).lower() +async def _check_reply(bot: "Bot", event: "Event"): + if event.type != "message": + return + + first_msg_seg = event.message[0] + if first_msg_seg.type == "reply": + msg_id = first_msg_seg.data["id"] + event.reply = await bot.get_msg(message_id=msg_id) + if event.reply["sender"]["user_id"] == event.self_id: + event.to_me = True + del event.message[0] + + def _check_at_me(bot: "Bot", event: "Event"): if event.type != "message": return @@ -64,7 +77,6 @@ def _check_at_me(bot: "Bot", event: "Event"): if event.detail_type == "private": event.to_me = True else: - event.to_me = False at_me_seg = MessageSegment.at(event.self_id) # check the first segment @@ -150,7 +162,7 @@ class ResultStore: try: return await asyncio.wait_for(future, timeout) except asyncio.TimeoutError: - raise NetworkError("WebSocket API call timeout") + raise NetworkError("WebSocket API call timeout") from None finally: del cls._futures[seq] @@ -190,7 +202,7 @@ class Bot(BaseBot): event = Event(message) # Check whether user is calling me - # TODO: Check reply + await _check_reply(self, event) _check_at_me(self, event) _check_nickname(self, event) @@ -205,7 +217,7 @@ class Bot(BaseBot): return await bot.call_api(api, **data) log("DEBUG", f"Calling API {api}") - if self.type == "websocket": + if self.connection_type == "websocket": seq = ResultStore.get_seq() await self.websocket.send({ "action": api, @@ -217,7 +229,7 @@ class Bot(BaseBot): return _handle_api_result(await ResultStore.fetch( seq, self.config.api_timeout)) - elif self.type == "http": + elif self.connection_type == "http": api_root = self.config.api_root.get(self.self_id) if not api_root: raise ApiNotAvailable @@ -377,6 +389,16 @@ class Event(BaseEvent): def message(self, value) -> None: self._raw_event["message"] = value + @property + @overrides(BaseEvent) + def reply(self) -> Optional[dict]: + return self._raw_event.get("reply") + + @reply.setter + @overrides(BaseEvent) + def reply(self, value) -> None: + self._raw_event["reply"] = value + @property @overrides(BaseEvent) def raw_message(self) -> Optional[str]: diff --git a/nonebot/config.py b/nonebot/config.py index c80cc32e..5ecc93e6 100644 --- a/nonebot/config.py +++ b/nonebot/config.py @@ -174,10 +174,10 @@ class Config(BaseConfig): API_ROOT={"123456": "http://127.0.0.1:5700"} """ - api_timeout: Optional[float] = 60. + api_timeout: Optional[float] = 30. """ - 类型: ``Optional[float]`` - - 默认值: ``60.`` + - 默认值: ``30.`` - 说明: API 请求超时时间,单位: 秒。 """ diff --git a/nonebot/matcher.py b/nonebot/matcher.py index e150f484..351661b9 100644 --- a/nonebot/matcher.py +++ b/nonebot/matcher.py @@ -28,7 +28,7 @@ class MatcherMeta(type): f"temp={self.temp}>") # type: ignore def __str__(self) -> str: - return self.__repr__() + return repr(self) class Matcher(metaclass=MatcherMeta): diff --git a/nonebot/message.py b/nonebot/message.py index 7caffbda..b6c3d888 100644 --- a/nonebot/message.py +++ b/nonebot/message.py @@ -34,7 +34,6 @@ async def _run_matcher(Matcher: Type[Matcher], bot: Bot, event: Event, f"Rule check failed for {Matcher}.") return - # TODO: log matcher logger.info(f"Event will be handled by {Matcher}") matcher = Matcher()