Skip to content

Commit

Permalink
✨ version 0.2.0
Browse files Browse the repository at this point in the history
support internal api
  • Loading branch information
RF-Tar-Railt committed Oct 9, 2023
1 parent a0e0f75 commit 9b8dde6
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 17 deletions.
12 changes: 9 additions & 3 deletions nonebot/adapters/satori/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
)
from .models import (
Payload,
LoginStatus,
PayloadType,
PingPayload,
PongPayload,
Expand Down Expand Up @@ -134,6 +135,10 @@ async def _authenticate(
)
return
for login in resp.body.logins:
if not login.self_id:
continue
if login.status != LoginStatus.ONLINE:
continue
if login.self_id not in self.bots:
bot = Bot(self, login.self_id, login.platform, info)
self.bot_connect(bot)
Expand All @@ -144,7 +149,9 @@ async def _authenticate(
else:
bot = self.bots[login.self_id]
bot.on_ready(login.user)

if not self.bots:
log("WARNING", "No bots connected!")
return
return True

async def _heartbeat(self, info: ClientInfo, ws: WebSocket):
Expand Down Expand Up @@ -269,8 +276,7 @@ def payload_to_event(payload: SatoriEvent) -> Event:
EventClass = EVENT_CLASSES.get(payload.type, None)
if EventClass is None:
log("WARNING", f"Unknown payload type: {payload.type}")
event = Event.parse_obj(payload)
return event
return Event.parse_obj(payload)
return EventClass.parse_obj(payload)

@override
Expand Down
18 changes: 16 additions & 2 deletions nonebot/adapters/satori/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from .event import Event, MessageEvent
from .message import Message, MessageSegment
from .models import InnerMessage as SatoriMessage
from .models import Role, User, Guild, Channel, OuterLogin, OuterMember
from .models import Role, User, Guild, Login, Channel, OuterMember
from .exception import (
ActionFailed,
NetworkError,
Expand Down Expand Up @@ -514,7 +514,7 @@ async def login_get(self):
"POST",
self.info.api_base / "login.get",
)
return OuterLogin.parse_obj(await self._request(request))
return Login.parse_obj(await self._request(request))

@API
async def user_get(self, *, user_id: str):
Expand Down Expand Up @@ -542,3 +542,17 @@ async def friend_approve(self, *, request_id: str, approve: bool, comment: str):
json={"message_id": request_id, "approve": approve, "comment": comment},
)
await self._request(request)

@API
async def internal(
self,
*,
action: str,
**kwargs,
):
request = Request(
"POST",
self.info.api_base / "internal" / action,
json=kwargs,
)
return await self._request(request)
4 changes: 2 additions & 2 deletions nonebot/adapters/satori/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from .models import Event as SatoriEvent
from .message import Message, RenderMessage
from .models import InnerMessage as SatoriMessage
from .models import Guild, Channel, InnerLogin, ChannelType, InnerMember
from .models import Guild, Login, Channel, ChannelType, InnerMember

E = TypeVar("E", bound="Event")

Expand Down Expand Up @@ -191,7 +191,7 @@ class GuildRoleUpdatedEvent(GuildRoleEvent):


class LoginEvent(NoticeEvent):
login: InnerLogin
login: Login
user: User

@override
Expand Down
12 changes: 3 additions & 9 deletions nonebot/adapters/satori/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,13 @@ class LoginStatus(IntEnum):
RECONNECT = 4


class InnerLogin(BaseModel, extra=Extra.allow):
class Login(BaseModel, extra=Extra.allow):
user: Optional[User] = None
self_id: Optional[str] = None
platform: Optional[str] = None
status: LoginStatus


class OuterLogin(InnerLogin):
user: User
self_id: str
platform: str


class Opcode(IntEnum):
EVENT = 0
PING = 1
Expand All @@ -103,7 +97,7 @@ class Identify(BaseModel):


class Ready(BaseModel):
logins: List[OuterLogin]
logins: List[Login]


class IdentifyPayload(Payload):
Expand Down Expand Up @@ -197,7 +191,7 @@ class Event(BaseModel, extra=Extra.allow):
timestamp: datetime
channel: Optional[Channel] = None
guild: Optional[Guild] = None
login: Optional[InnerLogin] = None
login: Optional[Login] = None
member: Optional[InnerMember] = None
message: Optional[InnerMessage] = None
operator: Optional[User] = None
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.1.3"
version = "0.2.0"
description = "Satori Protocol Adapter for Nonebot2"
authors = [
{name = "RF-Tar-Railt",email = "[email protected]"},
Expand Down

0 comments on commit 9b8dde6

Please sign in to comment.