From 4a62f877dfc2288437d7094c3c474aa1a4c38ca7 Mon Sep 17 00:00:00 2001 From: Soulter <905617992@qq.com> Date: Fri, 9 May 2025 10:45:50 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix:=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E5=8D=95=E7=8B=AC=E6=96=87=E4=BB=B6=E5=8F=91=E9=80=81=E6=97=B6?= =?UTF-8?q?=E8=A2=AB=E8=AE=A4=E4=B8=BA=E6=98=AF=E7=A9=BA=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E6=96=87=E4=BB=B6=E6=97=A0=E6=B3=95=E5=8F=91?= =?UTF-8?q?=E9=80=81=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- astrbot/core/message/components.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/astrbot/core/message/components.py b/astrbot/core/message/components.py index 74538d097..73bfa7279 100644 --- a/astrbot/core/message/components.py +++ b/astrbot/core/message/components.py @@ -559,12 +559,13 @@ class File(BaseMessageComponent): type: ComponentType = "File" name: T.Optional[str] = "" # 名字 - _file: T.Optional[str] = "" # 本地路径 + file_: T.Optional[str] = "" # 本地路径 url: T.Optional[str] = "" # url _downloaded: bool = False # 是否已经下载 - def __init__(self, name: str = "", file: str = "", url: str = ""): - super().__init__(name=name, _file=file, url=url) + def __init__(self, name: str, file: str, url: str = ""): + """文件消息段。一般情况下请直接使用 file 参数即可,可以传入文件路径或 URL,AstrBot 会自动识别。""" + super().__init__(name=name, file_=file, url=url) @property def file(self) -> str: @@ -574,8 +575,8 @@ class File(BaseMessageComponent): Returns: str: 文件路径 """ - if self._file and os.path.exists(self._file): - return self._file + if self.file_ and os.path.exists(self.file_): + return self.file_ if self.url and not self._downloaded: try: @@ -589,8 +590,8 @@ class File(BaseMessageComponent): # 等待下载完成 loop.run_until_complete(self._download_file()) - if self._file and os.path.exists(self._file): - return self._file + if self.file_ and os.path.exists(self.file_): + return self.file_ except Exception as e: logger.error(f"文件下载失败: {e}") @@ -607,7 +608,7 @@ class File(BaseMessageComponent): if value.startswith("http://") or value.startswith("https://"): self.url = value else: - self._file = value + self.file_ = value async def get_file(self) -> str: """ @@ -617,12 +618,12 @@ class File(BaseMessageComponent): Returns: str: 文件路径 """ - if self._file and os.path.exists(self._file): - return self._file + if self.file_ and os.path.exists(self.file_): + return self.file_ if self.url: await self._download_file() - return self._file + return self.file_ return "" @@ -637,7 +638,7 @@ class File(BaseMessageComponent): await download_file(self.url, file_path) - self._file = file_path + self.file_ = file_path self._downloaded = True