PagerMaid_Plugins_Pyro/hitokoto/main.py

37 lines
1.6 KiB
Python
Raw Normal View History

2022-07-22 07:35:40 +00:00
from pagermaid.listener import listener
2022-09-01 12:30:34 +00:00
from pagermaid.enums import Message, AsyncClient
2022-07-22 07:35:40 +00:00
from pagermaid.utils import lang
@listener(command="hitokoto",
description=lang('hitokoto_des'))
2022-09-01 12:30:34 +00:00
async def hitokoto(request: AsyncClient, message: Message):
2022-07-22 07:35:40 +00:00
hitokoto_while = 1
hitokoto_json = None
try:
2022-09-01 12:30:34 +00:00
hitokoto_json = (await request.get("https://v1.hitokoto.cn/?charset=utf-8")).json()
2022-07-22 07:35:40 +00:00
except ValueError:
while hitokoto_while < 10:
hitokoto_while += 1
try:
2022-09-01 12:30:34 +00:00
hitokoto_json = (await request.get("https://v1.hitokoto.cn/?charset=utf-8")).json()
2022-07-22 07:35:40 +00:00
break
except Exception:
continue
if not hitokoto_json:
return
hitokoto_type = {'a': lang('hitokoto_type_anime'), 'b': lang('hitokoto_type_manga'),
'c': lang('hitokoto_type_game'), 'd': lang('hitokoto_type_article'),
'e': lang('hitokoto_type_original'), 'f': lang('hitokoto_type_web'),
'g': lang('hitokoto_type_other'), 'h': lang('hitokoto_type_movie'),
'i': lang('hitokoto_type_poem'), 'j': lang('hitokoto_type_netease_music'),
'k': lang('hitokoto_type_philosophy'), 'l': lang('hitokoto_type_meme')}
add = ""
if works := hitokoto_json["from"]:
add += f"{works}"
if works_type := hitokoto_json["type"]:
add += f"{hitokoto_type[works_type]}"
if from_who := hitokoto_json["from_who"]:
add += f"{from_who}"
await message.edit(f"{hitokoto_json['hitokoto']} {add}")