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 import abc
from functools import reduce from functools import reduce
from dataclasses import dataclass, field from dataclasses import dataclass, field
from nonebot.rule import notice
from nonebot.config import Config 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): class BaseBot(abc.ABC):
@ -49,32 +48,32 @@ class BaseEvent(abc.ABC):
@property @property
@abc.abstractmethod @abc.abstractmethod
def type(self): def type(self) -> str:
raise NotImplementedError raise NotImplementedError
@type.setter @type.setter
@abc.abstractmethod @abc.abstractmethod
def type(self, value): def type(self, value) -> None:
raise NotImplementedError raise NotImplementedError
@property @property
@abc.abstractmethod @abc.abstractmethod
def detail_type(self): def detail_type(self) -> str:
raise NotImplementedError raise NotImplementedError
@detail_type.setter @detail_type.setter
@abc.abstractmethod @abc.abstractmethod
def detail_type(self, value): def detail_type(self, value) -> None:
raise NotImplementedError raise NotImplementedError
@property @property
@abc.abstractmethod @abc.abstractmethod
def sub_type(self): def sub_type(self) -> Optional[str]:
raise NotImplementedError raise NotImplementedError
@sub_type.setter @sub_type.setter
@abc.abstractmethod @abc.abstractmethod
def sub_type(self, value): def sub_type(self, value) -> None:
raise NotImplementedError raise NotImplementedError

View File

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