Skip to content

Commit

Permalink
🐛 type convert for MessageSegment.data
Browse files Browse the repository at this point in the history
  • Loading branch information
RF-Tar-Railt committed May 31, 2024
1 parent 96e6052 commit 0000527
Showing 1 changed file with 59 additions and 12 deletions.
71 changes: 59 additions & 12 deletions nonebot/adapters/satori/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def image(
name: Optional[str] = None,
extra: Optional[dict] = None,
cache: Optional[bool] = None,
timeout: Optional[str] = None,
timeout: Optional[int] = None,
) -> "Image":
if url:
data = {"src": url}
Expand All @@ -116,7 +116,7 @@ def image(
if cache is not None:
data["cache"] = cache # type: ignore
if timeout is not None:
data["timeout"] = timeout
data["timeout"] = timeout # type: ignore
return Image("img", data, extra=extra) # type: ignore

@staticmethod
Expand All @@ -129,7 +129,7 @@ def audio(
poster: Optional[str] = None,
extra: Optional[dict] = None,
cache: Optional[bool] = None,
timeout: Optional[str] = None,
timeout: Optional[int] = None,
) -> "Audio":
if url:
data = {"src": url}
Expand All @@ -147,7 +147,7 @@ def audio(
if cache is not None:
data["cache"] = cache # type: ignore
if timeout is not None:
data["timeout"] = timeout
data["timeout"] = timeout # type: ignore
return Audio("audio", data, extra=extra) # type: ignore

@staticmethod
Expand All @@ -160,7 +160,7 @@ def video(
poster: Optional[str] = None,
extra: Optional[dict] = None,
cache: Optional[bool] = None,
timeout: Optional[str] = None,
timeout: Optional[int] = None,
) -> "Video":
if url:
data = {"src": url}
Expand All @@ -178,7 +178,7 @@ def video(
if cache is not None:
data["cache"] = cache # type: ignore
if timeout is not None:
data["timeout"] = timeout
data["timeout"] = timeout # type: ignore
return Video("video", data, extra=extra) # type: ignore

@staticmethod
Expand All @@ -191,7 +191,7 @@ def file(
poster: Optional[str] = None,
extra: Optional[dict] = None,
cache: Optional[bool] = None,
timeout: Optional[str] = None,
timeout: Optional[int] = None,
) -> "File":
if url:
data = {"src": url}
Expand All @@ -209,7 +209,7 @@ def file(
if cache is not None:
data["cache"] = cache # type: ignore
if timeout is not None:
data["timeout"] = timeout
data["timeout"] = timeout # type: ignore
return File("file", data, extra=extra) # type: ignore

@staticmethod
Expand Down Expand Up @@ -499,7 +499,7 @@ class ImageData(TypedDict):
src: str
title: NotRequired[str]
cache: NotRequired[bool]
timeout: NotRequired[str]
timeout: NotRequired[int]
width: NotRequired[int]
height: NotRequired[int]

Expand All @@ -512,6 +512,17 @@ class Image(MessageSegment):
def __post_init__(self, extra: Optional[dict]):
if extra:
self.data.update(extra) # type: ignore
if "cache" in self.data:
if str(self.data["cache"]) == "True":
self.data["cache"] = True
else:
self.data["cache"] = False
if "width" in self.data:
self.data["width"] = int(self.data["width"])
if "height" in self.data:
self.data["height"] = int(self.data["height"])
if "timeout" in self.data:
self.data["timeout"] = int(self.data["timeout"])


class AudioData(TypedDict):
Expand All @@ -520,7 +531,7 @@ class AudioData(TypedDict):
duration: NotRequired[float]
poster: NotRequired[str]
cache: NotRequired[bool]
timeout: NotRequired[str]
timeout: NotRequired[int]


@dataclass
Expand All @@ -531,6 +542,15 @@ class Audio(MessageSegment):
def __post_init__(self, extra: Optional[dict]):
if extra:
self.data.update(extra) # type: ignore
if "cache" in self.data:
if str(self.data["cache"]) == "True":
self.data["cache"] = True
else:
self.data["cache"] = False
if "duration" in self.data:
self.data["duration"] = float(self.data["duration"])
if "timeout" in self.data:
self.data["timeout"] = int(self.data["timeout"])


class VideoData(TypedDict):
Expand All @@ -541,7 +561,7 @@ class VideoData(TypedDict):
duration: NotRequired[float]
poster: NotRequired[str]
cache: NotRequired[bool]
timeout: NotRequired[str]
timeout: NotRequired[int]


@dataclass
Expand All @@ -552,14 +572,27 @@ class Video(MessageSegment):
def __post_init__(self, extra: Optional[dict]):
if extra:
self.data.update(extra) # type: ignore
if "cache" in self.data:
if str(self.data["cache"]) == "True":
self.data["cache"] = True
else:
self.data["cache"] = False
if "width" in self.data:
self.data["width"] = int(self.data["width"])
if "height" in self.data:
self.data["height"] = int(self.data["height"])
if "duration" in self.data:
self.data["duration"] = float(self.data["duration"])
if "timeout" in self.data:
self.data["timeout"] = int(self.data["timeout"])


class FileData(TypedDict):
src: str
title: NotRequired[str]
poster: NotRequired[str]
cache: NotRequired[bool]
timeout: NotRequired[str]
timeout: NotRequired[int]


@dataclass
Expand All @@ -570,6 +603,13 @@ class File(MessageSegment):
def __post_init__(self, extra: Optional[dict]):
if extra:
self.data.update(extra) # type: ignore
if "cache" in self.data:
if str(self.data["cache"]) == "True":
self.data["cache"] = True
else:
self.data["cache"] = False
if "timeout" in self.data:
self.data["timeout"] = int(self.data["timeout"])


class Br(MessageSegment):
Expand All @@ -591,6 +631,13 @@ class RenderMessageData(TypedDict):
class RenderMessage(MessageSegment):
data: RenderMessageData = field(default_factory=dict) # type: ignore

def __post_init__(self):
if "forward" in self.data:
if str(self.data["forward"]) == "True":
self.data["forward"] = True
else:
self.data["forward"] = False

@property
def content(self) -> Optional["Message"]:
return self._children or None
Expand Down

0 comments on commit 0000527

Please sign in to comment.