change event

This commit is contained in:
yanyongyu 2020-08-11 16:54:45 +08:00
parent b32d4a24d1
commit 879904a0f5
3 changed files with 19 additions and 21 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.8 KiB

After

Width:  |  Height:  |  Size: 9.2 KiB

View File

@ -4,10 +4,9 @@
import abc
from functools import reduce
from dataclasses import dataclass, field
from nonebot.rule import notice
from nonebot.config import Config
from nonebot.typing import Dict, Union, Iterable, WebSocket
from nonebot.typing import Dict, Union, Optional, Iterable, WebSocket
class BaseBot(abc.ABC):
@ -49,32 +48,32 @@ class BaseEvent(abc.ABC):
@property
@abc.abstractmethod
def type(self):
def type(self) -> str:
raise NotImplementedError
@type.setter
@abc.abstractmethod
def type(self, value):
def type(self, value) -> None:
raise NotImplementedError
@property
@abc.abstractmethod
def detail_type(self):
def detail_type(self) -> str:
raise NotImplementedError
@detail_type.setter
@abc.abstractmethod
def detail_type(self, value):
def detail_type(self, value) -> None:
raise NotImplementedError
@property
@abc.abstractmethod
def sub_type(self):
def sub_type(self) -> Optional[str]:
raise NotImplementedError
@sub_type.setter
@abc.abstractmethod
def sub_type(self, value):
def sub_type(self, value) -> None:
raise NotImplementedError

View File

@ -58,14 +58,11 @@ class Bot(BaseBot):
@overrides(BaseBot)
async def handle_message(self, message: dict):
# TODO: convert message into event
event = Event(message)
if not event:
if not message:
return
# if "message" in event.keys():
# event["message"] = Message(event["message"])
# TODO: convert message into event
event = Event(message)
await handle_event(self, event)
@ -98,37 +95,39 @@ class Bot(BaseBot):
class Event(BaseEvent):
def __init__(self, raw_event: dict):
if "message" in raw_event:
raw_event["message"] = Message(raw_event["message"])
super().__init__(raw_event)
@property
@overrides(BaseEvent)
def type(self):
def type(self) -> str:
return self._raw_event["post_type"]
@type.setter
@overrides(BaseEvent)
def type(self, value):
def type(self, value) -> None:
self._raw_event["post_type"] = value
@property
@overrides(BaseEvent)
def detail_type(self):
def detail_type(self) -> str:
return self._raw_event[f"{self.type}_type"]
@detail_type.setter
@overrides(BaseEvent)
def detail_type(self, value):
def detail_type(self, value) -> None:
self._raw_event[f"{self.type}_type"] = value
@property
@overrides(BaseEvent)
def sub_type(self):
return self._raw_event["sub_type"]
def sub_type(self) -> Optional[str]:
return self._raw_event.get("sub_type")
@type.setter
@overrides(BaseEvent)
def sub_type(self, value):
def sub_type(self, value) -> None:
self._raw_event["sub_type"] = value