From 627a29f57e43cfdbe8e887deba2f2a4cc90cc6d9 Mon Sep 17 00:00:00 2001 From: passthem Date: Thu, 6 Nov 2025 00:12:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=20Discord=20Proxy=20?= =?UTF-8?q?=E9=80=89=E9=A1=B9=E6=9D=A5=E4=B8=8B=E8=BD=BD=E5=9B=BE=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- konabot/common/nb/extract_image.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/konabot/common/nb/extract_image.py b/konabot/common/nb/extract_image.py index 67e8592..6dfa856 100644 --- a/konabot/common/nb/extract_image.py +++ b/konabot/common/nb/extract_image.py @@ -9,6 +9,7 @@ from nonebot.matcher import Matcher from nonebot.adapters import Bot, Event, Message from nonebot.adapters.discord import Bot as DiscordBot from nonebot.adapters.discord import MessageEvent as DiscordMessageEvent +from nonebot.adapters.discord.config import Config as DiscordConfig from nonebot.adapters.onebot.v11 import Bot as OnebotV11Bot from nonebot.adapters.onebot.v11 import Message as OnebotV11Message from nonebot.adapters.onebot.v11 import MessageEvent as OnebotV11MessageEvent @@ -18,11 +19,14 @@ from PIL import UnidentifiedImageError from returns.result import Failure, Result, Success -async def download_image_bytes(url: str) -> Result[bytes, str]: +discordConfig = nonebot.get_plugin_config(DiscordConfig) + + +async def download_image_bytes(url: str, proxy: str | None = None) -> Result[bytes, str]: # if "/matcha/cache/" in url: # url = url.replace('127.0.0.1', '10.126.126.101') logger.debug(f"开始从 {url} 下载图片") - async with httpx.AsyncClient() as c: + async with httpx.AsyncClient(proxy=proxy) as c: try: response = await c.get(url) except (httpx.ConnectError, httpx.RemoteProtocolError) as e: @@ -128,7 +132,7 @@ async def extract_image_from_message( if "image/" not in a.content_type: continue url = a.proxy_url - return (await download_image_bytes(url)).bind(bytes_to_pil) + return (await download_image_bytes(url, discordConfig.discord_proxy)).bind(bytes_to_pil) for seg in UniMessage.of(msg, bot): logger.info(seg)