Skip to content

Commit

Permalink
🐛 bot.self_id missing platform
Browse files Browse the repository at this point in the history
  • Loading branch information
RF-Tar-Railt committed Sep 28, 2024
1 parent 609bcc3 commit af200b9
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 24 deletions.
30 changes: 15 additions & 15 deletions nonebot/adapters/satori/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"<y>Bot {escape_tag(bot.self_id)}</y> connected",
f"<y>Bot {escape_tag(bot.identity)}</y> 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!")
Expand Down Expand Up @@ -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"<y>Bot {escape_tag(bot.self_id)}</y> connected",
f"<y>Bot {escape_tag(bot.identity)}</y> 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"<y>Bot {escape_tag(event.self_id)}</y> disconnected",
f"<y>Bot {escape_tag(f'{event.platform}:{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)):
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)):
Expand Down
15 changes: 10 additions & 5 deletions nonebot/adapters/satori/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,22 +148,27 @@ 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 自身所属平台
self.platform: str = login.platform or "satori"
# 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):
Expand All @@ -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"]
Expand Down
8 changes: 8 additions & 0 deletions nonebot/adapters/satori/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

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.13.0rc1"
version = "0.13.0rc2"
description = "Satori Protocol Adapter for Nonebot2"
authors = [
{name = "RF-Tar-Railt",email = "[email protected]"},
Expand Down
2 changes: 1 addition & 1 deletion tests/test_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit af200b9

Please sign in to comment.