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):