修改HTTP请求 #24

Merged
QAQ merged 1 commits from QAQ into master 2025-05-22 17:54:19 +08:00

24
main.py
View File

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