mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-01-19 09:38:21 +08:00
change Event
This commit is contained in:
parent
9e33a605a6
commit
2d90c35df6
@ -4,6 +4,7 @@
|
||||
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
|
||||
@ -37,6 +38,46 @@ class BaseBot(abc.ABC):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
class BaseEvent(abc.ABC):
|
||||
|
||||
def __init__(self, raw_event: dict):
|
||||
self._raw_event = raw_event
|
||||
|
||||
def __repr__(self) -> str:
|
||||
# TODO: pretty print
|
||||
return f"<Event: >"
|
||||
|
||||
@property
|
||||
@abc.abstractmethod
|
||||
def type(self):
|
||||
raise NotImplementedError
|
||||
|
||||
@type.setter
|
||||
@abc.abstractmethod
|
||||
def type(self, value):
|
||||
raise NotImplementedError
|
||||
|
||||
@property
|
||||
@abc.abstractmethod
|
||||
def detail_type(self):
|
||||
raise NotImplementedError
|
||||
|
||||
@detail_type.setter
|
||||
@abc.abstractmethod
|
||||
def detail_type(self, value):
|
||||
raise NotImplementedError
|
||||
|
||||
@property
|
||||
@abc.abstractmethod
|
||||
def sub_type(self):
|
||||
raise NotImplementedError
|
||||
|
||||
@sub_type.setter
|
||||
@abc.abstractmethod
|
||||
def sub_type(self, value):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
@dataclass
|
||||
class BaseMessageSegment(abc.ABC):
|
||||
type: str
|
||||
@ -51,50 +92,6 @@ class BaseMessageSegment(abc.ABC):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
# class BaseMessageSegment(dict):
|
||||
|
||||
# def __init__(self,
|
||||
# type_: Optional[str] = None,
|
||||
# data: Optional[Dict[str, str]] = None):
|
||||
# super().__init__()
|
||||
# if type_:
|
||||
# self.type = type_
|
||||
# self.data = data
|
||||
# else:
|
||||
# raise ValueError('The "type" field cannot be empty')
|
||||
|
||||
# def __str__(self):
|
||||
# raise NotImplementedError
|
||||
|
||||
# def __getitem__(self, item):
|
||||
# if item not in ("type", "data"):
|
||||
# raise KeyError(f'Key "{item}" is not allowed')
|
||||
# return super().__getitem__(item)
|
||||
|
||||
# def __setitem__(self, key, value):
|
||||
# if key not in ("type", "data"):
|
||||
# raise KeyError(f'Key "{key}" is not allowed')
|
||||
# return super().__setitem__(key, value)
|
||||
|
||||
# # TODO: __eq__ __add__
|
||||
|
||||
# @property
|
||||
# def type(self) -> str:
|
||||
# return self["type"]
|
||||
|
||||
# @type.setter
|
||||
# def type(self, value: str):
|
||||
# self["type"] = value
|
||||
|
||||
# @property
|
||||
# def data(self) -> Dict[str, str]:
|
||||
# return self["data"]
|
||||
|
||||
# @data.setter
|
||||
# def data(self, data: Optional[Dict[str, str]]):
|
||||
# self["data"] = data or {}
|
||||
|
||||
|
||||
class BaseMessage(list, abc.ABC):
|
||||
|
||||
def __init__(self,
|
||||
|
@ -5,12 +5,12 @@ import re
|
||||
|
||||
import httpx
|
||||
|
||||
from nonebot.event import Event
|
||||
# from nonebot.event import Event
|
||||
from nonebot.config import Config
|
||||
from nonebot.message import handle_event
|
||||
from nonebot.exception import ApiNotAvailable
|
||||
from nonebot.adapters import BaseBot, BaseMessage, BaseMessageSegment
|
||||
from nonebot.typing import Tuple, Iterable, Optional, overrides, WebSocket
|
||||
from nonebot.adapters import BaseBot, BaseEvent, BaseMessage, BaseMessageSegment
|
||||
|
||||
|
||||
def escape(s: str, *, escape_comma: bool = True) -> str:
|
||||
@ -60,13 +60,13 @@ class Bot(BaseBot):
|
||||
@overrides(BaseBot)
|
||||
async def handle_message(self, message: dict):
|
||||
# TODO: convert message into event
|
||||
event = Event.from_payload(message)
|
||||
event = Event(message)
|
||||
|
||||
if not event:
|
||||
return
|
||||
|
||||
if "message" in event.keys():
|
||||
event["message"] = Message(event["message"])
|
||||
# if "message" in event.keys():
|
||||
# event["message"] = Message(event["message"])
|
||||
|
||||
await handle_event(self, event)
|
||||
|
||||
@ -96,6 +96,39 @@ class Bot(BaseBot):
|
||||
"<HttpFailed {0.status_code} for url: {0.url}>", response)
|
||||
|
||||
|
||||
class Event(BaseEvent):
|
||||
|
||||
@property
|
||||
@overrides(BaseEvent)
|
||||
def type(self):
|
||||
return self._raw_event["post_type"]
|
||||
|
||||
@type.setter
|
||||
@overrides(BaseEvent)
|
||||
def type(self, value):
|
||||
self._raw_event["post_type"] = value
|
||||
|
||||
@property
|
||||
@overrides(BaseEvent)
|
||||
def detail_type(self):
|
||||
return self._raw_event[f"{self.type}_type"]
|
||||
|
||||
@detail_type.setter
|
||||
@overrides(BaseEvent)
|
||||
def detail_type(self, value):
|
||||
self._raw_event[f"{self.type}_type"] = value
|
||||
|
||||
@property
|
||||
@overrides(BaseEvent)
|
||||
def sub_type(self):
|
||||
return self._raw_event["sub_type"]
|
||||
|
||||
@type.setter
|
||||
@overrides(BaseEvent)
|
||||
def sub_type(self, value):
|
||||
self._raw_event["sub_type"] = value
|
||||
|
||||
|
||||
class MessageSegment(BaseMessageSegment):
|
||||
|
||||
@overrides(BaseMessageSegment)
|
||||
|
@ -8,10 +8,9 @@ from typing import Union, TypeVar, Optional, Iterable, Callable, Awaitable
|
||||
|
||||
# import some modules needed when checking types
|
||||
if TYPE_CHECKING:
|
||||
from nonebot.event import Event as EventClass
|
||||
from nonebot.matcher import Matcher as MatcherClass
|
||||
from nonebot.drivers import BaseDriver, BaseWebSocket
|
||||
from nonebot.adapters import BaseBot, BaseMessage, BaseMessageSegment
|
||||
from nonebot.adapters import BaseBot, BaseEvent, BaseMessage, BaseMessageSegment
|
||||
|
||||
|
||||
def overrides(InterfaceClass: object):
|
||||
@ -28,7 +27,7 @@ Driver = TypeVar("Driver", bound="BaseDriver")
|
||||
WebSocket = TypeVar("WebSocket", bound="BaseWebSocket")
|
||||
|
||||
Bot = TypeVar("Bot", bound="BaseBot")
|
||||
Event = TypeVar("Event", bound="EventClass")
|
||||
Event = TypeVar("Event", bound="BaseEvent")
|
||||
Message = TypeVar("Message", bound="BaseMessage")
|
||||
MessageSegment = TypeVar("MessageSegment", bound="BaseMessageSegment")
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user