all fix a bug

This commit is contained in:
xtaodada 2022-09-01 20:30:34 +08:00
parent 19831f783c
commit 9c1228c801
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
4 changed files with 16 additions and 15 deletions

View File

@ -1,20 +1,20 @@
from pagermaid.listener import listener
from pagermaid.single_utils import Message
from pagermaid.enums import Message, AsyncClient
from pagermaid.utils import lang
@listener(command="hitokoto",
description=lang('hitokoto_des'))
async def hitokoto(message: Message):
async def hitokoto(request: AsyncClient, message: Message):
hitokoto_while = 1
hitokoto_json = None
try:
hitokoto_json = (await message.request.get("https://v1.hitokoto.cn/?charset=utf-8")).json()
hitokoto_json = (await request.get("https://v1.hitokoto.cn/?charset=utf-8")).json()
except ValueError:
while hitokoto_while < 10:
hitokoto_while += 1
try:
hitokoto_json = (await message.request.get("https://v1.hitokoto.cn/?charset=utf-8")).json()
hitokoto_json = (await request.get("https://v1.hitokoto.cn/?charset=utf-8")).json()
break
except Exception:
continue

View File

@ -1,13 +1,13 @@
from pagermaid.listener import listener
from pagermaid.single_utils import Message
from pagermaid.enums import Message, AsyncClient
@listener(command="netease_comment",
description="随机一条网易云音乐评论。")
async def netease(message: Message):
async def netease(request: AsyncClient, message: Message):
for _ in range(5):
try:
req = await message.request.get("https://api.66mz8.com/api/music.163.php?format=json")
req = await request.get("https://api.66mz8.com/api/music.163.php?format=json")
assert req.status_code == 200
data = req.json()
res = data['comments'] + '\n\n来自 @' + data[

View File

@ -6,7 +6,7 @@ from httpx import ReadTimeout
from pagermaid.listener import listener
from pagermaid.single_utils import safe_remove
from pagermaid.enums import Client, Message
from pagermaid.enums import Client, Message, AsyncClient
from pagermaid.utils import lang, pip_install
pip_install("speedtest-cli", alias="speedtest")
@ -31,7 +31,7 @@ def unit_convert(byte):
return f"{round(byte, 2)} {units[zero]}"
async def run_speedtest(message: Message):
async def run_speedtest(request: AsyncClient, message: Message):
test = Speedtest()
server = int(message.arguments) if len(message.parameter) == 1 else None
if server:
@ -57,7 +57,7 @@ async def run_speedtest(message: Message):
f"Timestamp: `{result['timestamp']}`"
)
if result["share"]:
data = await message.request.get(result["share"].replace("http:", "https:"), follow_redirects=True)
data = await request.get(result["share"].replace("http:", "https:"), follow_redirects=True)
with open("speedtest.png", mode="wb") as f:
f.write(data.content)
with contextlib.suppress(Exception):
@ -87,7 +87,7 @@ async def get_all_ids():
@listener(command="speedtest",
description=lang('speedtest_des'),
parameters="(Server ID/测速点列表)")
async def speedtest(client: Client, message: Message):
async def speedtest(client: Client, message: Message, request: AsyncClient):
""" Tests internet speed using speedtest. """
if message.arguments == "测速点列表":
msg = message
@ -97,7 +97,7 @@ async def speedtest(client: Client, message: Message):
if message.arguments == "测速点列表":
des, photo = await get_all_ids()
else:
des, photo = await run_speedtest(message)
des, photo = await run_speedtest(request, message)
except SpeedtestHTTPError:
return await msg.edit(lang('speedtest_ConnectFailure'))
except ValueError:

View File

@ -1,17 +1,18 @@
from pagermaid.listener import listener
from pagermaid.single_utils import Message, safe_remove
from pagermaid.enums import Message, AsyncClient
from pagermaid.single_utils import safe_remove
@listener(command="weather_lite",
description="查询天气",
parameters="<城市>")
async def weather_lite(message: Message):
async def weather_lite(request: AsyncClient, message: Message):
if not message.arguments:
return await message.edit("请输入城市名称")
if message.arguments.startswith("_"):
return await message.edit("请输入正确的城市名称")
city = message.arguments.strip()
data = await message.request.get(f"https://zh.wttr.in/{city}.png")
data = await request.get(f"https://zh.wttr.in/{city}.png")
if data.status_code != 200:
return await message.edit("请输入正确的城市名称")
with open("weather.png", "wb") as f: