From 5c6cec0d397cea758dbfc657a1fc20b7db088bf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CQAQ=E2=80=9D?= <“1540691861@qq.com”> Date: Thu, 22 May 2025 17:53:47 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9HTTP=E8=AF=B7=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/main.py b/main.py index 999b0a6..cf2945c 100644 --- a/main.py +++ b/main.py @@ -306,26 +306,32 @@ class MyPlugin(Star): yield event.plain_result(f"累积结果: {testNumber}") import astrbot.api.message_components as Comp - import aiohttp # 需要异步HTTP客户端 + import requests + import asyncio @filter.command("Dora") async def Dora(self, event: AstrMessageEvent): - async with aiohttp.ClientSession() as session: - async with session.get('https://www.doro.asia/api/random-sticker') as resp: - if resp.status == 200: - data = await resp.json() - if data.get('success', False): - image_url = data['sticker']['url'] - chain = [ - Comp.At(qq=event.get_sender_id()), # At 消息发送者 - Comp.Plain("Dora:"), - Comp.Image.fromURL(image_url), # 使用API返回的图片URL - ] - yield event.chain_result(chain) - else: - yield "获取表情包失败" - else: - yield "API请求失败" + try: + # 使用 asyncio.to_thread 在异步环境中运行同步 requests + def fetch_sticker(): + resp = requests.get('https://www.doro.asia/api/random-sticker') + resp.raise_for_status() # 检查 HTTP 错误 + return resp.json() + + data = await asyncio.to_thread(fetch_sticker) + + if data.get('success', False): + image_url = data['sticker']['url'] + chain = [ + Comp.At(qq=event.get_sender_id()), + Comp.Plain("Dora:"), + Comp.Image.fromURL(image_url), + ] + yield event.chain_result(chain) + else: + yield "获取表情包失败" + except Exception as e: + yield f"发生错误:{str(e)}" @filter.command("部署") async def bushutest(self, event: AstrMessageEvent):