Compare commits

...

2 Commits

Author SHA1 Message Date
QAQ
dfd54bc96a Merge pull request '使用aiohttp' (#25) from QAQ into master
Reviewed-on: #25
2025-05-22 17:59:17 +08:00
“QAQ”
c769412f71 使用aiohttp 2025-05-22 17:58:49 +08:00

40
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 错误 if data.get('success', False):
return resp.json() image_url = data['sticker']['url']
chain = [
data = await asyncio.to_thread(fetch_sticker) Comp.At(qq=event.get_sender_id()), # At 消息发送者
Comp.Plain("来看这个图:"),
if data.get('success', False): Comp.Image.fromURL(image_url), # 使用API返回的图片URL
image_url = data['sticker']['url'] ]
chain = [ yield event.chain_result(chain)
Comp.At(qq=event.get_sender_id()), else:
Comp.Plain("Dora"), yield "获取表情包失败"
Comp.Image.fromURL(image_url), else:
] yield "API请求失败"
yield event.chain_result(chain)
else:
yield "获取表情包失败"
except Exception as e:
yield f"发生错误:{str(e)}"
@filter.command("部署") @filter.command("部署")
async def bushutest(self, event: AstrMessageEvent): async def bushutest(self, event: AstrMessageEvent):