mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-11-24 00:55:07 +08:00
🚧 add some support for mirai basic events
This commit is contained in:
parent
8574b2ec72
commit
5a9798121c
0
nonebot/adapters/mirai/event/__init__.py
Normal file
0
nonebot/adapters/mirai/event/__init__.py
Normal file
65
nonebot/adapters/mirai/event/base.py
Normal file
65
nonebot/adapters/mirai/event/base.py
Normal 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
|
31
nonebot/adapters/mirai/event/constants.py
Normal file
31
nonebot/adapters/mirai/event/constants.py
Normal 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
|
130
nonebot/adapters/mirai/event/notice.py
Normal file
130
nonebot/adapters/mirai/event/notice.py
Normal 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
|
1
nonebot/adapters/mirai/event/request.py
Normal file
1
nonebot/adapters/mirai/event/request.py
Normal file
@ -0,0 +1 @@
|
||||
from .base import Event
|
Loading…
Reference in New Issue
Block a user