From 7f0950e0c8410c74e244797e395b74453f4ca8a2 Mon Sep 17 00:00:00 2001 From: rf_tar_railt <3165388245@qq.com> Date: Thu, 18 Apr 2024 19:00:13 +0800 Subject: [PATCH] :bookmark: version 0.1.0 --- README.md | 2 +- pyproject.toml | 2 +- src/nonebot/adapters/kritor/adapter.py | 68 +++++++++++++++----------- src/nonebot/adapters/kritor/bot.py | 4 +- src/nonebot/adapters/kritor/event.py | 10 ++-- 5 files changed, 49 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index 7fb6b3e..c27514c 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ DRIVER=~none KRITOR_CLIENTS=' [ { - "host: "xxx", + "host": "xxx", "port": "xxx", "account": "xxx", "ticket": "xxx" diff --git a/pyproject.toml b/pyproject.toml index 3f42804..eb3a368 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "nonebot-adapter-kritor" -version = "0.1.2" +version = "0.1.0" description = "Nonebot Adapter for Kritor Protocol" authors = [ {name = "rf_tar_railt", email = "rf_tar_railt@qq.com"}, diff --git a/src/nonebot/adapters/kritor/adapter.py b/src/nonebot/adapters/kritor/adapter.py index 8af5270..25cdf32 100644 --- a/src/nonebot/adapters/kritor/adapter.py +++ b/src/nonebot/adapters/kritor/adapter.py @@ -107,35 +107,45 @@ async def _listen_core(self, bot: Bot, service: EventServiceStub): async def grpc(self, info: ClientInfo) -> None: channel = Channel(info.host, info.port) - async with channel: - auth = AuthenticationServiceStub(channel) - if not self.kritor_config.kritor_skip_auth: - request = AuthenticateRequest(account=info.account, ticket=info.ticket) - response = await auth.authenticate(request) - if response.code != AuthenticateResponseAuthenticateResponseCode.OK: - log( - "ERROR", - f"Account {info.account} authenticate failed\n" - f"Error code: {response.code}" - f"Error message: {response.msg}", - ) - return - state = await auth.get_authentication_state(GetAuthenticationStateRequest(account=info.account)) - log("INFO", f"Account {info.account} authenticate success") - bot = Bot(self, info.account, info, channel, state.is_required) - self.bot_connect(bot) - event = EventServiceStub(channel) - listens = [ - asyncio.create_task(self._listen_message(bot, event)), - asyncio.create_task(self._listen_notice(bot, event)), - asyncio.create_task(self._listen_request(bot, event)), - asyncio.create_task(self._listen_core(bot, event)), - ] - await asyncio.wait(listens, return_when=asyncio.FIRST_EXCEPTION) - for task in listens: - task.cancel() - await task - self.bot_disconnect(bot) + listens = [] + bot: Optional[Bot] = None + try: + async with channel: + auth = AuthenticationServiceStub(channel) + if not self.kritor_config.kritor_skip_auth: + request = AuthenticateRequest(account=info.account, ticket=info.ticket) + response = await auth.authenticate(request, timeout=60) + if response.code != AuthenticateResponseAuthenticateResponseCode.OK: + log( + "ERROR", + f"Account {info.account} authenticate failed\n" + f"Error code: {response.code}" + f"Error message: {response.msg}", + ) + return + state = await auth.get_authentication_state( + GetAuthenticationStateRequest(account=info.account), timeout=60 + ) + log("INFO", f"Account {info.account} authenticate success") + bot = Bot(self, info.account, info, channel, state.is_required) + self.bot_connect(bot) + event = EventServiceStub(channel) + listens = [ + asyncio.create_task(self._listen_message(bot, event)), + asyncio.create_task(self._listen_notice(bot, event)), + asyncio.create_task(self._listen_request(bot, event)), + asyncio.create_task(self._listen_core(bot, event)), + ] + await asyncio.wait(listens, return_when=asyncio.FIRST_COMPLETED) + except Exception as e: + log("ERROR", f"Bot {info.account} connection failed: {e}") + raise + finally: + for task in listens: + task.cancel() + await task + if bot: + self.bot_disconnect(bot) @override async def _call_api(self, bot: Bot, api: str, **data: Any) -> Any: diff --git a/src/nonebot/adapters/kritor/bot.py b/src/nonebot/adapters/kritor/bot.py index bf09740..7d1b5a0 100644 --- a/src/nonebot/adapters/kritor/bot.py +++ b/src/nonebot/adapters/kritor/bot.py @@ -412,7 +412,9 @@ async def send_message( """ if contact.type == SceneType.GUILD: raise ValueError("Guild contact is not supported in this method. Use send_channel_message instead.") - return await self.service.message.send_message(SendMessageRequest(contact=contact.dump(), elements=elements, message_id=message_id)) + return await self.service.message.send_message( + SendMessageRequest(contact=contact.dump(), elements=elements, message_id=message_id) + ) @API async def send_message_by_res_id( diff --git a/src/nonebot/adapters/kritor/event.py b/src/nonebot/adapters/kritor/event.py index 8002a7d..1fbede9 100644 --- a/src/nonebot/adapters/kritor/event.py +++ b/src/nonebot/adapters/kritor/event.py @@ -9,7 +9,6 @@ from nonebot.adapters import Event as BaseEvent -from .bot import Bot from .compat import model_validator from .message import Reply, Message from .model import Group, Guild, Friend, Nearby, Sender, Stranger, ContactType, StrangerFromGroup @@ -71,6 +70,7 @@ class MessageEvent(Event): @property def replied_message(self) -> Optional["MessageEvent"]: + """返回可能的回复元素代表的原消息事件。""" return getattr(self, "_replied_message", None) @override @@ -419,7 +419,7 @@ def get_session_id(self) -> str: return f"{self.group_id}_{self.operator_uin or self.operator_uid}" @override - def check_tome(self, bot: Bot) -> None: + def check_tome(self, bot: "Bot") -> None: self.to_me = f"{self.target_uin or self.target_uid}" == bot.self_id @@ -449,7 +449,7 @@ def get_session_id(self) -> str: return f"{self.group_id}_{self.operator_uin or self.operator_uid}" @override - def check_tome(self, bot: Bot) -> None: + def check_tome(self, bot: "Bot") -> None: self.to_me = f"{self.target_uin or self.target_uid}" == bot.self_id @@ -477,7 +477,7 @@ def get_session_id(self) -> str: return f"{self.group_id}_{self.operator_uin or self.operator_uid}" @override - def check_tome(self, bot: Bot) -> None: + def check_tome(self, bot: "Bot") -> None: self.to_me = f"{self.target_uin or self.target_uid}" == bot.self_id @@ -617,7 +617,7 @@ def get_session_id(self) -> str: return f"{self.group_id}_{self.operator_uin or self.operator_uid}" @override - def check_tome(self, bot: Bot) -> None: + def check_tome(self, bot: "Bot") -> None: self.to_me = f"{self.target_uin or self.target_uid}" == bot.self_id