2021-01-29 17:38:39 +08:00
|
|
|
from enum import Enum
|
2021-01-29 21:19:13 +08:00
|
|
|
from typing import Dict, Any, Optional, Type
|
|
|
|
from pydantic import BaseModel, Field, ValidationError
|
2021-01-29 17:38:39 +08:00
|
|
|
from typing_extensions import Literal
|
|
|
|
|
|
|
|
from nonebot.adapters import Event as BaseEvent
|
|
|
|
from nonebot.adapters import Message as BaseMessage
|
|
|
|
from nonebot.typing import overrides
|
2021-01-29 21:19:13 +08:00
|
|
|
from nonebot.log import logger
|
2021-01-29 17:38:39 +08:00
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2021-01-29 21:19:13 +08:00
|
|
|
class PrivateSenderInfo(BaseModel):
|
|
|
|
id: int
|
|
|
|
nickname: str
|
|
|
|
remark: str
|
|
|
|
|
|
|
|
|
2021-01-29 17:38:39 +08:00
|
|
|
class Event(BaseEvent):
|
|
|
|
type: str
|
|
|
|
|
2021-01-29 21:19:13 +08:00
|
|
|
@classmethod
|
|
|
|
def new(cls, data: Dict[str, Any]) -> "Event":
|
|
|
|
type = data['type']
|
|
|
|
|
|
|
|
def all_subclasses(cls: Type[Event]):
|
|
|
|
return set(cls.__subclasses__()).union(
|
|
|
|
[s for c in cls.__subclasses__() for s in all_subclasses(c)])
|
|
|
|
|
|
|
|
event_class: Optional[Type[Event]] = None
|
|
|
|
for subclass in all_subclasses(cls):
|
|
|
|
if subclass.__name__ != type:
|
|
|
|
continue
|
|
|
|
event_class = subclass
|
|
|
|
|
|
|
|
if event_class is None:
|
|
|
|
return Event.parse_obj(data)
|
|
|
|
|
|
|
|
while issubclass(event_class, Event):
|
|
|
|
try:
|
|
|
|
return event_class.parse_obj(data)
|
|
|
|
except ValidationError as e:
|
|
|
|
logger.info(
|
|
|
|
f'Failed to parse {data} to class {event_class.__name__}: {e}. '
|
|
|
|
'Fallback to parent class.')
|
|
|
|
event_class = event_class.__base__
|
|
|
|
|
|
|
|
raise ValueError(f'Failed to serialize {data}.')
|
|
|
|
|
2021-01-29 17:38:39 +08:00
|
|
|
@overrides(BaseEvent)
|
|
|
|
def get_type(self) -> Literal["message", "notice", "request", "meta_event"]:
|
2021-01-29 21:19:13 +08:00
|
|
|
from . import message, notice, request
|
|
|
|
if isinstance(self, message.MessageEvent):
|
|
|
|
return 'message'
|
|
|
|
elif isinstance(self, notice.NoticeEvent):
|
|
|
|
return 'notice'
|
|
|
|
elif isinstance(self, request.RequestEvent):
|
|
|
|
return 'request'
|
|
|
|
else:
|
|
|
|
return 'meta_event'
|
2021-01-29 17:38:39 +08:00
|
|
|
|
|
|
|
@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
|