Archived
1
0
Fork 0
This repository has been archived on 2024-04-26. You can view files and clone it, but cannot push or open issues or pull requests.
akari-bot/modules/hitokoto/__init__.py
2023-05-05 09:44:05 +08:00

29 lines
No EOL
1.2 KiB
Python

from core.builtins import Bot
from core.component import module
from core.builtins.message import MessageSession
from core.dirty_check import check
from core.utils.http import get_url
hitokoto_types = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"]
hitokoto = module('hitokoto', developers=['bugungu', 'DoroWolf'], desc='{hitokoto.help.desc}', support_languages=['zh_cn'])
@hitokoto.handle()
@hitokoto.handle('[<type>] {{hitokoto.help.type}}')
async def _(msg: Bot.MessageSession):
try:
set_type = msg.parsed_msg.get('<type>')
except AttributeError:
set_type = None
url = 'https://v1.hitokoto.cn/?'
if set_type is not None:
if set_type not in hitokoto_types:
await msg.finish(msg.locale.t('hitokoto.message.error.type'))
else:
url += "c=" + set_type
data = await get_url(url, 200, fmt = 'json')
from_who = data["from_who"] or ""
type = msg.locale.t('hitokoto.message.type') + msg.locale.t('hitokoto.message.type.' + data['type'])
link = 'https://hitokoto.cn?id='
await msg.finish(f'''{data["hitokoto"]}\n——{from_who}{data["from"]}\n{type}\n{link}{data["id"]}''')