Skip to content

Commit

Permalink
🔖 version 0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
RF-Tar-Railt committed Apr 18, 2024
1 parent 60046a1 commit 7f0950e
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 37 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ DRIVER=~none
KRITOR_CLIENTS='
[
{
"host: "xxx",
"host": "xxx",
"port": "xxx",
"account": "xxx",
"ticket": "xxx"
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]"},
Expand Down
68 changes: 39 additions & 29 deletions src/nonebot/adapters/kritor/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"<r><bg #f8bbd0>Account {info.account} authenticate failed\n"
f"Error code: {response.code}"
f"Error message: {response.msg}</bg #f8bbd0></r>",
)
return
state = await auth.get_authentication_state(GetAuthenticationStateRequest(account=info.account))
log("INFO", f"<y>Account {info.account} authenticate success</y>")
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"<r><bg #f8bbd0>Account {info.account} authenticate failed\n"
f"Error code: {response.code}"
f"Error message: {response.msg}</bg #f8bbd0></r>",
)
return
state = await auth.get_authentication_state(
GetAuthenticationStateRequest(account=info.account), timeout=60
)
log("INFO", f"<y>Account {info.account} authenticate success</y>")
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:
Expand Down
4 changes: 3 additions & 1 deletion src/nonebot/adapters/kritor/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
10 changes: 5 additions & 5 deletions src/nonebot/adapters/kritor/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -71,6 +70,7 @@ class MessageEvent(Event):

@property
def replied_message(self) -> Optional["MessageEvent"]:
"""返回可能的回复元素代表的原消息事件。"""
return getattr(self, "_replied_message", None)

@override
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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


Expand Down

0 comments on commit 7f0950e

Please sign in to comment.