Skip to content

Commit

Permalink
[OneBot] Fix v11 Echo validate handler error when create new field
Browse files Browse the repository at this point in the history
  • Loading branch information
aicorein committed Dec 16, 2024
1 parent 0589f3a commit a4d35b3
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/melobot/protocols/onebot/v11/adapter/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ async def create(self, packet: EchoPacket) -> ec.Echo | None:

data = packet.data
data["action_type"] = packet.action_type
return await self.validate_handle(data)(ec.Echo.resolve)(**data)
return await self.validate_handle(data)(ec.Echo.resolve)(data)


class EchoRequireCtx(Context[bool]):
Expand Down
50 changes: 25 additions & 25 deletions src/melobot/protocols/onebot/v11/adapter/echo.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,50 +38,50 @@ def is_failed(self) -> bool:
return self._model.status == "failed"

@classmethod
def resolve(cls, **kwds: Any) -> Echo:
match kwds["action_type"]:
def resolve(cls, raw: dict[str, Any]) -> Echo:
match raw["action_type"]:
case "send_private_msg" | "send_group_msg" | "send_msg":
return SendMsgEcho(**kwds)
return SendMsgEcho(**raw)
case "send_private_forward_msg" | "send_group_forward_msg":
return SendForwardMsgEcho(**kwds)
return SendForwardMsgEcho(**raw)
case "get_msg":
return GetMsgEcho(**kwds)
return GetMsgEcho(**raw)
case "get_forward_msg":
return GetForwardMsgEcho(**kwds)
return GetForwardMsgEcho(**raw)
case "get_login_info":
return GetLoginInfoEcho(**kwds)
return GetLoginInfoEcho(**raw)
case "get_stranger_info":
return GetStrangerInfoEcho(**kwds)
return GetStrangerInfoEcho(**raw)
case "get_friend_list":
return GetFriendListEcho(**kwds)
return GetFriendListEcho(**raw)
case "get_group_info":
return GetGroupInfoEcho(**kwds)
return GetGroupInfoEcho(**raw)
case "get_group_list":
return GetGroupListEcho(**kwds)
return GetGroupListEcho(**raw)
case "get_group_member_info":
return GetGroupMemberInfoEcho(**kwds)
return GetGroupMemberInfoEcho(**raw)
case "get_group_member_list":
return GetGroupMemberListEcho(**kwds)
return GetGroupMemberListEcho(**raw)
case "get_group_honor_info":
return GetGroupHonorInfoEcho(**kwds)
return GetGroupHonorInfoEcho(**raw)
case "get_cookies":
return GetCookiesEcho(**kwds)
return GetCookiesEcho(**raw)
case "get_csrf_token":
return GetCsrfTokenEcho(**kwds)
return GetCsrfTokenEcho(**raw)
case "get_credentials":
return GetCredentialsEcho(**kwds)
return GetCredentialsEcho(**raw)
case "get_record":
return GetRecordEcho(**kwds)
return GetRecordEcho(**raw)
case "get_image":
return GetImageEcho(**kwds)
return GetImageEcho(**raw)
case "can_send_image":
return CanSendImageEcho(**kwds)
return CanSendImageEcho(**raw)
case "can_send_record":
return CanSendRecordEcho(**kwds)
return CanSendRecordEcho(**raw)
case "get_status":
return GetStatusEcho(**kwds)
return GetStatusEcho(**raw)
case "get_version_info":
return GetVersionInfoEcho(**kwds)
return GetVersionInfoEcho(**raw)
case (
"delete_msg"
| "send_like"
Expand All @@ -100,9 +100,9 @@ def resolve(cls, **kwds: Any) -> Echo:
| "set_restart"
| "clean_cache"
):
return EmptyEcho(**kwds)
return EmptyEcho(**raw)
case _:
return Echo(**kwds)
return Echo(**raw)


class EmptyEcho(Echo):
Expand Down
28 changes: 17 additions & 11 deletions tests/onebot/v11/test_adapter_echo.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,31 @@ async def test_empty():

async def test_other():
assert isinstance(
echo.Echo.resolve(**ec(message_id=123), action_type="send_msg"), echo.SendMsgEcho
echo.Echo.resolve({**ec(message_id=123), "action_type": "send_msg"}),
echo.SendMsgEcho,
)
assert isinstance(
echo.Echo.resolve(
**ec(message_id=123, forward_id="abc"), action_type="send_private_forward_msg"
{
**ec(message_id=123, forward_id="abc"),
"action_type": "send_private_forward_msg",
}
),
echo.SendForwardMsgEcho,
)

assert echo.Echo.resolve(
**ec(
time=123,
message_type="private",
message_id=123,
real_id=456,
sender={"user_id": 789, "nickname": "melody", "sex": "male", "age": 18},
message="123[45[CQ:node,user_id=10001000,nickname=某人,content=[CQ:face,id=123]哈喽~]12345",
),
action_type="get_msg",
{
**ec(
time=123,
message_type="private",
message_id=123,
real_id=456,
sender={"user_id": 789, "nickname": "melody", "sex": "male", "age": 18},
message="123[45[CQ:node,user_id=10001000,nickname=某人,content=[CQ:face,id=123]哈喽~]12345",
),
"action_type": "get_msg",
}
).data["message"][0].data == {"text": "123[45"}

assert not (
Expand Down

0 comments on commit a4d35b3

Please sign in to comment.