🚧 add some support for mirai basic events

This commit is contained in:
Mix 2021-01-29 17:38:39 +08:00
parent 8574b2ec72
commit 5a9798121c
5 changed files with 227 additions and 0 deletions

View File

View File

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

View File

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

View File

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

View File

@ -0,0 +1 @@
from .base import Event