修改HTTP请求 #24

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

40
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')
if data.get('success', False): resp.raise_for_status() # 检查 HTTP 错误
image_url = data['sticker']['url'] return resp.json()
chain = [
Comp.At(qq=event.get_sender_id()), # At 消息发送者 data = await asyncio.to_thread(fetch_sticker)
Comp.Plain("Dora"),
Comp.Image.fromURL(image_url), # 使用API返回的图片URL if data.get('success', False):
] image_url = data['sticker']['url']
yield event.chain_result(chain) chain = [
else: Comp.At(qq=event.get_sender_id()),
yield "获取表情包失败" Comp.Plain("Dora"),
else: Comp.Image.fromURL(image_url),
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):