Skip to content

Commit

Permalink
✨ bot.ensure_url
Browse files Browse the repository at this point in the history
  • Loading branch information
RF-Tar-Railt committed Sep 10, 2024
1 parent 2646f94 commit f8d7830
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion nonebot/adapters/satori/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ async def _loop(self, info: ClientInfo, ws: WebSocket):
continue
if isinstance(event, (MessageEvent, InteractionEvent)):
event = event.convert()
asyncio.create_task(bot.handle_event(event))
_t = asyncio.create_task(bot.handle_event(event))
elif isinstance(payload, PongPayload):
log("TRACE", "Pong")
continue
Expand Down
17 changes: 16 additions & 1 deletion nonebot/adapters/satori/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing_extensions import override
from typing import TYPE_CHECKING, Any, Union, Literal, Optional, overload

from yarl import URL
from nonebot.message import handle_event
from nonebot.drivers import Request, Response
from nonebot.compat import model_dump, type_validate_python
Expand Down Expand Up @@ -238,9 +239,23 @@ async def _request(self, request: Request) -> Any:

return self._handle_response(response)

def ensure_url(self, url: str) -> URL:
"""确定链接形式。
若链接符合以下条件之一,则返回链接的代理形式 ({host}/{path}/{version}/proxy/{url}):
- 链接以 "upload://" 开头
- 链接开头出现在 self_info.proxy_urls 中的某一项
"""
if url.startswith("upload"):
return self.info.api_base / "proxy" / url.lstrip("/")
for proxy_url in self._self_info.proxy_urls:
if url.startswith(proxy_url):
return self.info.api_base / "proxy" / url.lstrip("/")
return URL(url)

async def download(self, url: str) -> bytes:
"""访问内部链接。"""
request = Request("GET", self.info.api_base / "proxy" / url.lstrip("/"))
request = Request("GET", self.ensure_url(url))
try:
response = await self.adapter.request(request)
except Exception as e:
Expand Down

0 comments on commit f8d7830

Please sign in to comment.