Skip to content

Commit

Permalink
🐛 version 0.12.2
Browse files Browse the repository at this point in the history
fix bot disconnect
  • Loading branch information
RF-Tar-Railt committed Jun 15, 2024
1 parent d1c7036 commit c0973fa
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
13 changes: 12 additions & 1 deletion nonebot/adapters/satori/adapter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import asyncio
from collections import defaultdict
from typing_extensions import override
from typing import Any, Literal, Optional

Expand Down Expand Up @@ -49,6 +50,7 @@ def __init__(self, driver: Driver, **kwargs: Any):
self.satori_config: Config = get_plugin_config(Config)
self.tasks: list[asyncio.Task] = [] # 存储 ws 任务
self.sequences: dict[str, int] = {} # 存储 连接序列号
self._bots: defaultdict[str, set[str]] = defaultdict(set) # 存储 identity 和 bot_id 的映射
self.setup()

@classmethod
Expand Down Expand Up @@ -89,6 +91,9 @@ async def shutdown(self) -> None:
*(asyncio.wait_for(task, timeout=10) for task in self.tasks),
return_exceptions=True,
)
self.tasks.clear()
self.sequences.clear()
self._bots.clear()

@staticmethod
def payload_to_json(payload: Payload) -> str:
Expand Down Expand Up @@ -137,12 +142,14 @@ async def _authenticate(self, info: ClientInfo, ws: WebSocket) -> Optional[Liter
continue
if login.self_id not in self.bots:
bot = Bot(self, login.self_id, login, info)
self._bots[info.identity].add(bot.self_id)
self.bot_connect(bot)
log(
"INFO",
f"<y>Bot {escape_tag(bot.self_id)}</y> connected",
)
else:
self._bots[info.identity].add(login.self_id)
bot = self.bots[login.self_id]
bot._update(login)
if not self.bots:
Expand Down Expand Up @@ -196,10 +203,11 @@ async def ws(self, info: ClientInfo) -> None:
if heartbeat_task:
heartbeat_task.cancel()
heartbeat_task = None
bots = list(self.bots.values())
bots = [self.bots[bot_id] for bot_id in self._bots[info.identity]]
for bot in bots:
self.bot_disconnect(bot)
bots.clear()
self._bots[info.identity].clear()
except Exception as e:
log(
"ERROR",
Expand Down Expand Up @@ -232,20 +240,23 @@ async def _loop(self, info: ClientInfo, ws: WebSocket):
else:
if isinstance(event, LoginAddedEvent):
bot = Bot(self, event.self_id, event.login, info)
self._bots[info.identity].add(bot.self_id)
self.bot_connect(bot)
log(
"INFO",
f"<y>Bot {escape_tag(bot.self_id)}</y> connected",
)
elif isinstance(event, LoginRemovedEvent):
self.bot_disconnect(self.bots[event.self_id])
self._bots[info.identity].discard(event.self_id)
log(
"INFO",
f"<y>Bot {escape_tag(event.self_id)}</y> disconnected",
)
continue
elif isinstance(event, LoginUpdatedEvent):
self.bots[event.self_id]._update(event.login)
self._bots[info.identity].add(event.self_id)
if not (bot := self.bots.get(event.self_id)):
log(
"WARNING",
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-satori"
version = "0.12.1"
version = "0.12.2"
description = "Satori Protocol Adapter for Nonebot2"
authors = [
{name = "RF-Tar-Railt",email = "[email protected]"},
Expand Down

0 comments on commit c0973fa

Please sign in to comment.