🐛 fix message construct typing and plugins loading

This commit is contained in:
yanyongyu 2020-12-31 14:00:59 +08:00
parent a98417a878
commit 938b5bf275
4 changed files with 35 additions and 16 deletions

View File

@ -10,7 +10,7 @@ from copy import copy
from typing_extensions import Literal
from functools import reduce, partial
from dataclasses import dataclass, field
from typing import Any, Dict, Union, TypeVar, Optional, Callable, Iterable, Awaitable, TYPE_CHECKING
from typing import Any, Dict, Union, TypeVar, Mapping, Optional, Callable, Iterable, Iterator, Awaitable, TYPE_CHECKING
from pydantic import BaseModel
@ -211,8 +211,8 @@ class Message(list, abc.ABC):
"""消息数组"""
def __init__(self,
message: Union[str, list, dict, T_MessageSegment, T_Message,
Any] = None,
message: Union[str, Mapping, Iterable[Mapping],
T_MessageSegment, T_Message, Any] = None,
*args,
**kwargs):
"""
@ -242,7 +242,8 @@ class Message(list, abc.ABC):
@staticmethod
@abc.abstractmethod
def _construct(
msg: Union[str, list, dict, Any]) -> Iterable[T_MessageSegment]:
msg: Union[str, Mapping, Iterable[Mapping], Any]
) -> Iterable[T_MessageSegment]:
raise NotImplementedError
def __add__(self: T_Message, other: Union[str, T_MessageSegment,
@ -257,10 +258,20 @@ class Message(list, abc.ABC):
return result
def __radd__(self: T_Message, other: Union[str, T_MessageSegment,
T_Message]):
T_Message]) -> T_Message:
result = self.__class__(other)
return result.__add__(self)
def __iadd__(self: T_Message, other: Union[str, T_MessageSegment,
T_Message]) -> T_Message:
if isinstance(other, str):
self.extend(self._construct(other))
elif isinstance(other, MessageSegment):
self.append(other)
elif isinstance(other, Message):
self.extend(other)
return self
def append(self: T_Message, obj: Union[str, T_MessageSegment]) -> T_Message:
"""
:说明:

View File

@ -1,5 +1,5 @@
import re
from typing import Any, Dict, Union, Tuple, Iterable, Optional
from typing import Any, Dict, Union, Tuple, Mapping, Iterable, Optional
from nonebot.typing import overrides
from nonebot.adapters import Message as BaseMessage, MessageSegment as BaseMessageSegment
@ -212,11 +212,13 @@ class Message(BaseMessage):
@staticmethod
@overrides(BaseMessage)
def _construct(msg: Union[str, dict, list]) -> Iterable[MessageSegment]:
if isinstance(msg, dict):
def _construct(
msg: Union[str, Mapping,
Iterable[Mapping]]) -> Iterable[MessageSegment]:
if isinstance(msg, Mapping):
yield MessageSegment(msg["type"], msg.get("data") or {})
return
elif isinstance(msg, list):
elif isinstance(msg, Iterable) and not isinstance(msg, str):
for seg in msg:
yield MessageSegment(seg["type"], seg.get("data") or {})
return

View File

@ -1,5 +1,5 @@
from copy import copy
from typing import Any, Dict, Union, Iterable
from typing import Any, Dict, Union, Mapping, Iterable
from nonebot.typing import overrides
from nonebot.adapters import Message as BaseMessage, MessageSegment as BaseMessageSegment
@ -133,17 +133,20 @@ class Message(BaseMessage):
@staticmethod
@overrides(BaseMessage)
def _construct(msg: Union[str, dict, list]) -> Iterable[MessageSegment]:
if isinstance(msg, dict):
def _construct(
msg: Union[str, Mapping,
Iterable[Mapping]]) -> Iterable[MessageSegment]:
if isinstance(msg, Mapping):
yield MessageSegment(msg["type"], msg.get("data") or {})
elif isinstance(msg, list):
for seg in msg:
yield MessageSegment(seg["type"], seg.get("data") or {})
elif isinstance(msg, str):
yield MessageSegment.text(msg)
elif isinstance(msg, Iterable):
for seg in msg:
yield MessageSegment(seg["type"], seg.get("data") or {})
def _produce(self) -> dict:
data = {}
segment: MessageSegment
for segment in self:
# text 可以和 text 合并
if segment.type == "text" and data.get("msgtype") == 'text':

View File

@ -897,7 +897,10 @@ def load_plugins(*plugin_dir: str) -> Set[Plugin]:
return None
spec = module_info.module_finder.find_spec(name, None)
if spec.name in plugins:
if not spec:
logger.warning(
f"Module {name} cannot be loaded! Check module name first.")
elif spec.name in plugins:
return None
elif spec.name in sys.modules:
logger.warning(