From 5a9798121c7a8e8861d298b39870824d7f5fa9fb Mon Sep 17 00:00:00 2001 From: Mix Date: Fri, 29 Jan 2021 17:38:39 +0800 Subject: [PATCH] :construction: add some support for mirai basic events --- nonebot/adapters/mirai/event/__init__.py | 0 nonebot/adapters/mirai/event/base.py | 65 +++++++++++ nonebot/adapters/mirai/event/constants.py | 31 ++++++ nonebot/adapters/mirai/event/notice.py | 130 ++++++++++++++++++++++ nonebot/adapters/mirai/event/request.py | 1 + 5 files changed, 227 insertions(+) create mode 100644 nonebot/adapters/mirai/event/__init__.py create mode 100644 nonebot/adapters/mirai/event/base.py create mode 100644 nonebot/adapters/mirai/event/constants.py create mode 100644 nonebot/adapters/mirai/event/notice.py create mode 100644 nonebot/adapters/mirai/event/request.py diff --git a/nonebot/adapters/mirai/event/__init__.py b/nonebot/adapters/mirai/event/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/nonebot/adapters/mirai/event/base.py b/nonebot/adapters/mirai/event/base.py new file mode 100644 index 00000000..7b18c39a --- /dev/null +++ b/nonebot/adapters/mirai/event/base.py @@ -0,0 +1,65 @@ +from enum import Enum + +from pydantic import BaseModel, Field +from typing_extensions import Literal + +from nonebot.adapters import Event as BaseEvent +from nonebot.adapters import Message as BaseMessage +from nonebot.typing import overrides + +from .constants import EVENT_TYPES + + +class SenderPermission(str, Enum): + OWNER = 'OWNER' + ADMINISTRATOR = 'ADMINISTRATOR' + MEMBER = 'MEMBER' + + +class SenderGroup(BaseModel): + id: int + name: str + permission: SenderPermission + + +class SenderInfo(BaseModel): + id: int + name: str = Field(alias='memberName') + permission: SenderPermission + group: SenderGroup + + +class Event(BaseEvent): + type: str + + @overrides(BaseEvent) + def get_type(self) -> Literal["message", "notice", "request", "meta_event"]: + return EVENT_TYPES.get(self.type, 'meta_event') + + @overrides(BaseEvent) + def get_event_name(self) -> str: + return self.type + + @overrides(BaseEvent) + def get_event_description(self) -> str: + return str(self.dict()) + + @overrides(BaseEvent) + def get_message(self) -> BaseMessage: + raise ValueError("Event has no message!") + + @overrides(BaseEvent) + def get_plaintext(self) -> str: + raise ValueError("Event has no message!") + + @overrides(BaseEvent) + def get_user_id(self) -> str: + raise ValueError("Event has no message!") + + @overrides(BaseEvent) + def get_session_id(self) -> str: + raise ValueError("Event has no message!") + + @overrides(BaseEvent) + def is_tome(self) -> bool: + return False diff --git a/nonebot/adapters/mirai/event/constants.py b/nonebot/adapters/mirai/event/constants.py new file mode 100644 index 00000000..1ede39a8 --- /dev/null +++ b/nonebot/adapters/mirai/event/constants.py @@ -0,0 +1,31 @@ +from typing import List, Dict +from typing_extensions import Literal + +EventType = Literal["message", "notice", "request", "meta_event"] + +_EVENT_CLASSIFY: Dict[EventType, List[str]] = { + # XXX Reference: https://github.com/project-mirai/mirai-api-http/blob/v1.9.7/docs/EventType.md + 'meta_event': [ + 'BotOnlineEvent', 'BotOfflineEventActive', 'BotOfflineEventForce', + 'BotOfflineEventDropped', 'BotReloginEvent' + ], + 'notice': [ + 'BotGroupPermissionChangeEvent', 'BotMuteEvent', 'BotUnmuteEvent', + 'BotJoinGroupEvent', 'BotLeaveEventActive', 'BotLeaveEventKick', + 'GroupRecallEvent', 'FriendRecallEvent', 'GroupNameChangeEvent', + 'GroupEntranceAnnouncementChangeEvent', 'GroupMuteAllEvent', + 'GroupAllowAnonymousChatEvent', 'GroupAllowConfessTalkEvent', + 'GroupAllowMemberInviteEvent', 'MemberJoinEvent', + 'MemberLeaveEventKick', 'MemberLeaveEventQuit', 'MemberCardChangeEvent', + 'MemberSpecialTitleChangeEvent', 'MemberPermissionChangeEvent', + 'MemberMuteEvent', 'MemberUnmuteEvent' + ], + 'request': [ + 'NewFriendRequestEvent', 'MemberJoinRequestEvent', + 'BotInvitedJoinGroupRequestEvent' + ], + 'message': ['GroupMessage', 'FriendMessage', 'TempMessage'] +} +EVENT_TYPES: Dict[str, EventType] = {} +for event_type, events in _EVENT_CLASSIFY.items(): + _EVENT_TYPES.update({k: event_type for k in events}) # type: ignore diff --git a/nonebot/adapters/mirai/event/notice.py b/nonebot/adapters/mirai/event/notice.py new file mode 100644 index 00000000..536bc12b --- /dev/null +++ b/nonebot/adapters/mirai/event/notice.py @@ -0,0 +1,130 @@ +from typing import Optional, Any + +from pydantic import Field + +from .base import Event, SenderGroup, SenderInfo, SenderPermission + + +class BaseNoticeEvent(Event): + pass + + +class BaseMuteEvent(BaseNoticeEvent): + operator: SenderInfo + + +class BotMuteEvent(BaseMuteEvent): + pass + + +class BotUnmuteEvent(BaseMuteEvent): + pass + + +class MemberMuteEvent(BaseMuteEvent): + duration_seconds: int = Field(alias='durationSeconds') + member: SenderInfo + operator: Optional[SenderInfo] = None + + +class MemberUnmuteEvent(BaseMuteEvent): + member: SenderInfo + operator: Optional[SenderInfo] = None + + +class BotJoinGroupEvent(BaseNoticeEvent): + group: SenderGroup + + +class BotLeaveEventActive(BotJoinGroupEvent): + pass + + +class BotLeaveEventKick(BotJoinGroupEvent): + pass + + +class MemberJoinEvent(BaseNoticeEvent): + member: SenderInfo + + +class MemberLeaveEventQuit(MemberJoinEvent): + pass + + +class MemberLeaveEventKick(MemberJoinEvent): + operator: Optional[SenderInfo] = None + + +class FriendRecallEvent(BaseNoticeEvent): + author_id: int = Field(alias='authorId') + message_id: int = Field(alias='messageId') + time: int + operator: int + + +class GroupRecallEvent(FriendRecallEvent): + group: SenderGroup + operator: Optional[SenderInfo] = None + + +class GroupStateChangeEvent(BaseNoticeEvent): + origin: Any + current: Any + group: SenderGroup + operator: Optional[SenderInfo] = None + + +class GroupNameChangeEvent(GroupStateChangeEvent): + origin: str + current: str + + +class GroupEntranceAnnouncementChangeEvent(GroupStateChangeEvent): + origin: str + current: str + + +class GroupMuteAllEvent(GroupStateChangeEvent): + origin: bool + current: bool + + +class GroupAllowAnonymousChatEvent(GroupStateChangeEvent): + origin: bool + current: bool + + +class GroupAllowConfessTalkEvent(GroupStateChangeEvent): + origin: bool + current: bool + + +class GroupAllowMemberInviteEvent(GroupStateChangeEvent): + origin: bool + current: bool + + +class MemberStateChangeEvent(BaseNoticeEvent): + member: SenderInfo + operator: Optional[SenderInfo] = None + + +class MemberCardChangeEvent(MemberStateChangeEvent): + origin: str + current: str + + +class MemberSpecialTitleChangeEvent(MemberStateChangeEvent): + origin: str + current: str + + +class BotGroupPermissionChangeEvent(MemberStateChangeEvent): + origin: SenderPermission + current: SenderPermission + + +class MemberPermissionChangeEvent(MemberStateChangeEvent): + origin: SenderPermission + current: SenderPermission diff --git a/nonebot/adapters/mirai/event/request.py b/nonebot/adapters/mirai/event/request.py new file mode 100644 index 00000000..7e813f29 --- /dev/null +++ b/nonebot/adapters/mirai/event/request.py @@ -0,0 +1 @@ +from .base import Event