diff --git a/.gitignore b/.gitignore index e25e417..9229d32 100644 --- a/.gitignore +++ b/.gitignore @@ -159,3 +159,4 @@ cython_debug/ # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. .idea/ +/example/ diff --git a/kritor b/kritor index 27669a8..f44684a 160000 --- a/kritor +++ b/kritor @@ -1 +1 @@ -Subproject commit 27669a8f5704a8876658a47cc976f1fb3e9cc2f4 +Subproject commit f44684a8468481e8374c1ddd886797a1da994ac0 diff --git a/pyproject.toml b/pyproject.toml index 37a235c..cc20482 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "nonebot-adapter-kritor" -version = "0.1.1" +version = "0.2.0" description = "Nonebot Adapter for Kritor Protocol" authors = [ {name = "rf_tar_railt", email = "rf_tar_railt@qq.com"}, diff --git a/src/nonebot/adapters/kritor/bot.py b/src/nonebot/adapters/kritor/bot.py index 7d1b5a0..a3f5b15 100644 --- a/src/nonebot/adapters/kritor/bot.py +++ b/src/nonebot/adapters/kritor/bot.py @@ -387,7 +387,7 @@ async def send( message=str(message), **kwargs, ) - return SendMessageResponse(message_id=resp.message_id, message_time=resp.time) + return SendMessageResponse(message_id=resp.message_id, message_time=resp.message_time) if isinstance(message, str): message = Message(message) elif isinstance(message, MessageSegment): diff --git a/src/nonebot/adapters/kritor/model.py b/src/nonebot/adapters/kritor/model.py index 84fe944..de9974c 100644 --- a/src/nonebot/adapters/kritor/model.py +++ b/src/nonebot/adapters/kritor/model.py @@ -4,6 +4,7 @@ from pydantic import Field, BaseModel from .protos.kritor.common import Scene +from .protos.kritor.common import Role as ProtoRole from .protos.kritor.common import Sender as ProtoSender from .protos.kritor.common import Contact as ProtoContact @@ -62,14 +63,23 @@ class Nearby(Contact): ] +class Role(IntEnum): + UNKNOWN = 0 + MEMBER = 1 + ADMIN = 2 + OWNER = 3 + + class Sender(BaseModel): uid: str uin: Optional[int] = None nick: Optional[str] = None + role: Optional[Role] = None def dump(self) -> ProtoSender: return ProtoSender( uid=self.uid, uin=self.uin, nick=self.nick, + role=ProtoRole(self.role.value) if self.role else None, ) diff --git a/src/nonebot/adapters/kritor/permission.py b/src/nonebot/adapters/kritor/permission.py new file mode 100644 index 0000000..331dd38 --- /dev/null +++ b/src/nonebot/adapters/kritor/permission.py @@ -0,0 +1,78 @@ +from nonebot.permission import Permission + +from .model import Role +from .event import TempMessage, GroupMessage, MessageEvent, FriendMessage, StrangerMessage + + +async def _private(event: MessageEvent) -> bool: + return isinstance(event, (FriendMessage, TempMessage, StrangerMessage)) + + +async def _private_friend(event: FriendMessage) -> bool: + return True + + +async def _private_temp(event: TempMessage) -> bool: + return True + + +async def _private_other(event: StrangerMessage) -> bool: + return True + + +PRIVATE: Permission = Permission(_private) +""" 匹配任意私聊消息类型事件""" +P_FRIEND: Permission = Permission(_private_friend) +"""匹配任意好友私聊消息类型事件""" +P_TEMP: Permission = Permission(_private_temp) +"""匹配任意群临时私聊消息类型事件""" +P_STRANGER: Permission = Permission(_private_other) +"""匹配任意陌生人私聊消息类型事件""" + + +async def _group(event: GroupMessage) -> bool: + return True + + +async def _group_member(event: GroupMessage) -> bool: + if not event.sender.role: + return True + return event.sender.role is Role.UNKNOWN + + +async def _group_admin(event: GroupMessage) -> bool: + if not event.sender.role: + return True + return event.sender.role is Role.ADMIN + + +async def _group_owner(event: GroupMessage) -> bool: + if not event.sender.role: + return True + return event.sender.role is Role.OWNER + + +GROUP: Permission = Permission(_group) +"""匹配任意群聊消息类型事件""" +GROUP_MEMBER: Permission = Permission(_group_member) +"""匹配任意群员群聊消息类型事件 + +:::warning 警告 +该权限通过 event.sender 进行判断且不包含管理员以及群主! +::: +""" +GROUP_ADMIN: Permission = Permission(_group_admin) +"""匹配任意群管理员群聊消息类型事件""" +GROUP_OWNER: Permission = Permission(_group_owner) +"""匹配任意群主群聊消息类型事件""" + +__all__ = [ + "PRIVATE", + "P_FRIEND", + "P_TEMP", + "P_STRANGER", + "GROUP", + "GROUP_MEMBER", + "GROUP_ADMIN", + "GROUP_OWNER", +] diff --git a/src/nonebot/adapters/kritor/protos/kritor/common/__init__.py b/src/nonebot/adapters/kritor/protos/kritor/common/__init__.py index 94c3b40..0ed30d0 100644 --- a/src/nonebot/adapters/kritor/protos/kritor/common/__init__.py +++ b/src/nonebot/adapters/kritor/protos/kritor/common/__init__.py @@ -20,6 +20,13 @@ class Scene(betterproto.Enum): STRANGER = 9 +class Role(betterproto.Enum): + UNKNOWN = 0 + MEMBER = 1 + ADMIN = 2 + OWNER = 3 + + class ElementElementType(betterproto.Enum): TEXT = 0 AT = 1 @@ -80,6 +87,7 @@ class Sender(betterproto.Message): uid: str = betterproto.string_field(1) uin: Optional[int] = betterproto.uint64_field(2, optional=True, group="_uin") nick: Optional[str] = betterproto.string_field(3, optional=True, group="_nick") + role: Optional["Role"] = betterproto.enum_field(4, optional=True, group="_role") @dataclass(eq=False, repr=False) @@ -328,7 +336,7 @@ class KeyboardElement(betterproto.Message): @dataclass(eq=False, repr=False) class PushMessageBody(betterproto.Message): - time: int = betterproto.uint32_field(1) + time: int = betterproto.uint64_field(1) message_id: str = betterproto.string_field(2) message_seq: int = betterproto.uint64_field(3) contact: "Contact" = betterproto.message_field(4) @@ -351,8 +359,8 @@ class EssenceMessageBody(betterproto.Message): operator_uid: int = betterproto.uint64_field(5) operator_uin: int = betterproto.uint64_field(6) operator_nick: str = betterproto.string_field(7) - operation_time: int = betterproto.uint32_field(8) - message_time: int = betterproto.uint32_field(9) + operation_time: int = betterproto.uint64_field(8) + message_time: int = betterproto.uint64_field(9) message_id: str = betterproto.string_field(10) message_seq: int = betterproto.uint64_field(11) json_elements: str = betterproto.string_field(12) diff --git a/src/nonebot/adapters/kritor/protos/kritor/event/__init__.py b/src/nonebot/adapters/kritor/protos/kritor/event/__init__.py index cca2411..d8a6cc4 100644 --- a/src/nonebot/adapters/kritor/protos/kritor/event/__init__.py +++ b/src/nonebot/adapters/kritor/protos/kritor/event/__init__.py @@ -93,7 +93,7 @@ class PrivateFileUploadedNotice(betterproto.Message): file_sub_id: str = betterproto.string_field(4) file_name: str = betterproto.string_field(5) file_size: int = betterproto.uint64_field(6) - expire_time: int = betterproto.uint32_field(7) + expire_time: int = betterproto.uint64_field(7) url: str = betterproto.string_field(8) @@ -129,7 +129,7 @@ class GroupFileUploadedNotice(betterproto.Message): file_name: str = betterproto.string_field(5) file_size: int = betterproto.uint64_field(6) bus_id: int = betterproto.int32_field(7) - expire_time: int = betterproto.uint32_field(8) + expire_time: int = betterproto.uint64_field(8) file_url: str = betterproto.string_field(9) @@ -229,7 +229,7 @@ class GroupReactMessageWithEmojiNotice(betterproto.Message): @dataclass(eq=False, repr=False) class NoticeEvent(betterproto.Message): type: "NoticeEventNoticeType" = betterproto.enum_field(1) - time: int = betterproto.uint32_field(2) + time: int = betterproto.uint64_field(2) notice_id: str = betterproto.string_field(3) private_poke: "PrivatePokeNotice" = betterproto.message_field(10, group="notice") private_recall: "PrivateRecallNotice" = betterproto.message_field(11, group="notice") @@ -276,7 +276,7 @@ class InvitedJoinGroupRequest(betterproto.Message): @dataclass(eq=False, repr=False) class RequestEvent(betterproto.Message): type: "RequestEventRequestType" = betterproto.enum_field(1) - time: int = betterproto.uint32_field(2) + time: int = betterproto.uint64_field(2) request_id: str = betterproto.string_field(3) friend_apply: "FriendApplyRequest" = betterproto.message_field(10, group="request") group_apply: "GroupApplyRequest" = betterproto.message_field(11, group="request") diff --git a/src/nonebot/adapters/kritor/protos/kritor/file/__init__.py b/src/nonebot/adapters/kritor/protos/kritor/file/__init__.py index ae535a0..8769b60 100644 --- a/src/nonebot/adapters/kritor/protos/kritor/file/__init__.py +++ b/src/nonebot/adapters/kritor/protos/kritor/file/__init__.py @@ -22,9 +22,9 @@ class File(betterproto.Message): file_name: str = betterproto.string_field(2) file_size: int = betterproto.uint64_field(3) bus_id: int = betterproto.int32_field(4) - upload_time: int = betterproto.uint32_field(5) - expire_time: int = betterproto.uint32_field(6) - modify_time: int = betterproto.uint32_field(7) + upload_time: int = betterproto.uint64_field(5) + expire_time: int = betterproto.uint64_field(6) + modify_time: int = betterproto.uint64_field(7) download_times: int = betterproto.uint32_field(8) uploader: int = betterproto.uint64_field(9) uploader_name: str = betterproto.string_field(10) @@ -38,7 +38,7 @@ class Folder(betterproto.Message): folder_id: str = betterproto.string_field(1) folder_name: str = betterproto.string_field(2) total_file_count: int = betterproto.uint32_field(3) - create_time: int = betterproto.uint32_field(4) + create_time: int = betterproto.uint64_field(4) creator: int = betterproto.uint64_field(5) creator_name: str = betterproto.string_field(6) diff --git a/src/nonebot/adapters/kritor/protos/kritor/group/__init__.py b/src/nonebot/adapters/kritor/protos/kritor/group/__init__.py index f965dcf..64a6a66 100644 --- a/src/nonebot/adapters/kritor/protos/kritor/group/__init__.py +++ b/src/nonebot/adapters/kritor/protos/kritor/group/__init__.py @@ -43,7 +43,7 @@ class NotJoinedGroupInfo(betterproto.Message): group_name: str = betterproto.string_field(4) group_desc: str = betterproto.string_field(5) owner: int = betterproto.uint64_field(6) - create_time: int = betterproto.uint32_field(7) + create_time: int = betterproto.uint64_field(7) group_flag: int = betterproto.uint32_field(8) group_flag_ext: int = betterproto.uint32_field(9) @@ -52,7 +52,7 @@ class NotJoinedGroupInfo(betterproto.Message): class ProhibitedUserInfo(betterproto.Message): uid: str = betterproto.string_field(1) uin: int = betterproto.uint64_field(2) - prohibited_time: int = betterproto.uint32_field(3) + prohibited_time: int = betterproto.uint64_field(3) @dataclass(eq=False, repr=False) @@ -73,12 +73,12 @@ class GroupMemberInfo(betterproto.Message): nick: str = betterproto.string_field(3) age: int = betterproto.uint32_field(4) unique_title: str = betterproto.string_field(5) - unique_title_expire_time: int = betterproto.uint32_field(6) + unique_title_expire_time: int = betterproto.uint64_field(6) card: str = betterproto.string_field(7) join_time: int = betterproto.uint64_field(8) last_active_time: int = betterproto.uint64_field(9) level: int = betterproto.uint32_field(10) - shut_up_timestamp: int = betterproto.uint64_field(11) + shut_up_time: int = betterproto.uint64_field(11) distance: Optional[int] = betterproto.uint32_field(100, optional=True, group="_distance") honors: List[int] = betterproto.uint32_field(101) unfriendly: Optional[bool] = betterproto.bool_field(102, optional=True, group="_unfriendly") diff --git a/src/nonebot/adapters/kritor/protos/kritor/guild/__init__.py b/src/nonebot/adapters/kritor/protos/kritor/guild/__init__.py index 2878cd1..c61bd1e 100644 --- a/src/nonebot/adapters/kritor/protos/kritor/guild/__init__.py +++ b/src/nonebot/adapters/kritor/protos/kritor/guild/__init__.py @@ -245,7 +245,7 @@ class SendChannelMessageResponse(betterproto.Message): """发送信息到子频道响应""" message_id: str = betterproto.string_field(1) - time: int = betterproto.int64_field(2) + message_time: int = betterproto.uint64_field(2) @dataclass(eq=False, repr=False) diff --git a/src/nonebot/adapters/kritor/protos/kritor/message/__init__.py b/src/nonebot/adapters/kritor/protos/kritor/message/__init__.py index 0f894e4..f120a87 100644 --- a/src/nonebot/adapters/kritor/protos/kritor/message/__init__.py +++ b/src/nonebot/adapters/kritor/protos/kritor/message/__init__.py @@ -33,7 +33,7 @@ class SendMessageRequest(betterproto.Message): @dataclass(eq=False, repr=False) class SendMessageResponse(betterproto.Message): message_id: str = betterproto.string_field(1) - message_time: int = betterproto.uint32_field(2) + message_time: int = betterproto.uint64_field(2) @dataclass(eq=False, repr=False) @@ -46,7 +46,7 @@ class SendMessageByResIdRequest(betterproto.Message): @dataclass(eq=False, repr=False) class SendMessageByResIdResponse(betterproto.Message): message_id: str = betterproto.string_field(1) - message_time: int = betterproto.uint32_field(2) + message_time: int = betterproto.uint64_field(2) @dataclass(eq=False, repr=False)