From c769412f71f977381b869498e8ced7333b08daa5 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:58:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8aiohttp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/main.py b/main.py index cf2945c..6bc4c99 100644 --- a/main.py +++ b/main.py @@ -306,32 +306,26 @@ class MyPlugin(Star): yield event.plain_result(f"累积结果: {testNumber}") import astrbot.api.message_components as Comp - import requests - import asyncio + import aiohttp # 需要异步HTTP客户端 @filter.command("Dora") async def Dora(self, event: AstrMessageEvent): - 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)}" + 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("来看这个图:"), + Comp.Image.fromURL(image_url), # 使用API返回的图片URL + ] + yield event.chain_result(chain) + else: + yield "获取表情包失败" + else: + yield "API请求失败" @filter.command("部署") async def bushutest(self, event: AstrMessageEvent):