使用aiohttp

This commit is contained in:
“QAQ” 2025-05-22 17:58:49 +08:00
parent 5c6cec0d39
commit c769412f71

26
main.py
View File

@ -306,32 +306,26 @@ class MyPlugin(Star):
yield event.plain_result(f"累积结果: {testNumber}") yield event.plain_result(f"累积结果: {testNumber}")
import astrbot.api.message_components as Comp import astrbot.api.message_components as Comp
import requests import aiohttp # 需要异步HTTP客户端
import asyncio
@filter.command("Dora") @filter.command("Dora")
async def Dora(self, event: AstrMessageEvent): async def Dora(self, event: AstrMessageEvent):
try: async with aiohttp.ClientSession() as session:
# 使用 asyncio.to_thread 在异步环境中运行同步 requests async with session.get('https://www.doro.asia/api/random-sticker') as resp:
def fetch_sticker(): if resp.status == 200:
resp = requests.get('https://www.doro.asia/api/random-sticker') data = await resp.json()
resp.raise_for_status() # 检查 HTTP 错误
return resp.json()
data = await asyncio.to_thread(fetch_sticker)
if data.get('success', False): if data.get('success', False):
image_url = data['sticker']['url'] image_url = data['sticker']['url']
chain = [ chain = [
Comp.At(qq=event.get_sender_id()), Comp.At(qq=event.get_sender_id()), # At 消息发送者
Comp.Plain("Dora"), Comp.Plain("来看这个图:"),
Comp.Image.fromURL(image_url), Comp.Image.fromURL(image_url), # 使用API返回的图片URL
] ]
yield event.chain_result(chain) yield event.chain_result(chain)
else: else:
yield "获取表情包失败" yield "获取表情包失败"
except Exception as e: else:
yield f"发生错误:{str(e)}" yield "API请求失败"
@filter.command("部署") @filter.command("部署")
async def bushutest(self, event: AstrMessageEvent): async def bushutest(self, event: AstrMessageEvent):