diff --git a/nonebot/adapters/satori/adapter.py b/nonebot/adapters/satori/adapter.py index a03ce3c..4d165a4 100644 --- a/nonebot/adapters/satori/adapter.py +++ b/nonebot/adapters/satori/adapter.py @@ -140,17 +140,17 @@ async def _authenticate(self, info: ClientInfo, ws: WebSocket) -> Optional[Liter continue if login.status != LoginStatus.ONLINE: continue - if login.id not in self.bots: + if login.identity not in self.bots: bot = Bot(self, login.id, login, info) - self._bots[info.identity].add(bot.self_id) + self._bots[info.identity].add(bot.identity) self.bot_connect(bot) log( "INFO", - f"Bot {escape_tag(bot.self_id)} connected", + f"Bot {escape_tag(bot.identity)} connected", ) else: - self._bots[info.identity].add(login.id) - bot = self.bots[login.id] + self._bots[info.identity].add(login.identity) + bot = self.bots[login.identity] bot._update(login) if not self.bots: log("WARNING", "No bots connected!") @@ -235,32 +235,32 @@ async def _loop(self, info: ClientInfo, ws: WebSocket): log( "WARNING", f"Failed to parse event {escape_tag(repr(payload))}", - e, + e if payload.body["type"] != "internal" else None, ) else: if isinstance(event, LoginAddedEvent): bot = Bot(self, event.self_id, event.login, info) - self._bots[info.identity].add(bot.self_id) + self._bots[info.identity].add(bot.identity) self.bot_connect(bot) log( "INFO", - f"Bot {escape_tag(bot.self_id)} connected", + f"Bot {escape_tag(bot.identity)} connected", ) elif isinstance(event, LoginRemovedEvent): - self.bot_disconnect(self.bots[event.self_id]) - self._bots[info.identity].discard(event.self_id) + self.bot_disconnect(self.bots[f"{event.platform}:{event.self_id}"]) + self._bots[info.identity].discard(f"{event.platform}:{event.self_id}") log( "INFO", - f"Bot {escape_tag(event.self_id)} disconnected", + f"Bot {escape_tag(f'{event.platform}:{event.self_id}')} 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)): + self.bots[f"{event.platform}:{event.self_id}"]._update(event.login) + self._bots[info.identity].add(f"{event.platform}:{event.self_id}") + if not (bot := self.bots.get(f"{event.platform}:{event.self_id}")): log( "WARNING", - f"Received event for unknown bot " f"{escape_tag(event.self_id)}", + f"Received event for unknown bot {escape_tag(f'{event.platform}:{event.self_id}')}", ) continue if isinstance(event, (MessageEvent, InteractionEvent)): diff --git a/nonebot/adapters/satori/bot.py b/nonebot/adapters/satori/bot.py index 2179742..820a371 100644 --- a/nonebot/adapters/satori/bot.py +++ b/nonebot/adapters/satori/bot.py @@ -148,8 +148,7 @@ class Bot(BaseBot): @override def __init__(self, adapter: "Adapter", self_id: str, login: LoginType, info: ClientInfo): - super().__init__(adapter, self_id) - + self._self_id = self_id # Bot 配置信息 self.info: ClientInfo = info # Bot 自身所属平台 @@ -157,13 +156,19 @@ def __init__(self, adapter: "Adapter", self_id: str, login: LoginType, info: Cli # Bot 自身信息 self._self_info = login + super().__init__(adapter, self.identity) + def __getattr__(self, item): raise AttributeError(f"'Bot' object has no attribute '{item}'") + @property + def identity(self): + return f"{self.platform}:{self.get_self_id()}" + def get_self_id(self): if self._self_info and self._self_info.user: return self._self_info.user.id - return self.self_id + return self._self_id @property def support_features(self): @@ -189,10 +194,10 @@ def get_authorization_header(self) -> dict[str, str]: """获取当前 Bot 的鉴权信息""" header = { "Authorization": f"Bearer {self.info.token}", - "X-Self-ID": self.self_id, + "X-Self-ID": self.get_self_id(), "X-Platform": self.platform, "Satori-Platform": self.platform, - "Satori-Login-ID": self.self_id, + "Satori-Login-ID": self.get_self_id(), } if not self.info.token: del header["Authorization"] diff --git a/nonebot/adapters/satori/models.py b/nonebot/adapters/satori/models.py index d8fb93c..755e098 100644 --- a/nonebot/adapters/satori/models.py +++ b/nonebot/adapters/satori/models.py @@ -125,6 +125,10 @@ class Login(BaseModel): def id(self) -> Optional[str]: return self.self_id or (self.user.id if self.user else None) + @property + def identity(self) -> str: + return f"{self.platform or 'satori'}:{self.id}" + if PYDANTIC_V2: model_config: ConfigDict = ConfigDict(extra="allow") # type: ignore @@ -145,6 +149,10 @@ class LoginPreview(BaseModel): def id(self) -> str: return self.user.id + @property + def identity(self) -> str: + return f"{self.platform}:{self.id}" + if PYDANTIC_V2: model_config: ConfigDict = ConfigDict(extra="allow") # type: ignore diff --git a/pyproject.toml b/pyproject.toml index a179e44..fb46984 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "nonebot-adapter-satori" -version = "0.13.0rc1" +version = "0.13.0rc2" description = "Satori Protocol Adapter for Nonebot2" authors = [ {name = "RF-Tar-Railt",email = "rf_tar_railt@qq.com"}, diff --git a/tests/test_adapter.py b/tests/test_adapter.py index 8873428..79b24ae 100644 --- a/tests/test_adapter.py +++ b/tests/test_adapter.py @@ -22,7 +22,7 @@ async def handle(bot: Bot): async with app.test_matcher(cmd) as ctx: adapter: Adapter = nonebot.get_adapter(Adapter) bot: Bot = ctx.create_bot( - base=Bot, adapter=adapter, self_id="0", login=Login(status=LoginStatus.CONNECT), info=None + base=Bot, adapter=adapter, self_id="0", login=Login(status=LoginStatus.CONNECT, platform="test"), info=None ) ctx.receive_event( diff --git a/tests/test_connection.py b/tests/test_connection.py index e0292c8..9167f15 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -43,6 +43,6 @@ def _ping(json: dict) -> dict: await asyncio.sleep(5) bots = nonebot.get_bots() - assert "0" in bots + assert "test:0" in bots await adapter.shutdown() - assert "0" not in nonebot.get_bots() + assert "test:0" not in nonebot.get_bots()