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

32 lines
1.1 KiB
Python
Raw Normal View History

from core.builtins import Bot
from core.component import module
from core.utils.http import get_url
2023-05-04 12:58:21 +00:00
hitokoto_types = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"]
hitokoto = module(
'hitokoto',
2023-06-09 10:12:08 +00:00
developers=['bugungu', 'DoroWolf'],
desc='{hitokoto.help.desc}',
2023-06-01 17:08:10 +00:00
alias='htkt',
support_languages=['zh_cn'])
2023-05-27 11:37:21 +00:00
@hitokoto.handle()
2023-06-09 10:12:08 +00:00
@hitokoto.handle('[<msg_type>] {{hitokoto.help.type}}')
2023-06-16 08:52:09 +00:00
async def _(msg: Bot.MessageSession, msg_type: str = None):
2023-06-16 18:01:53 +00:00
url = "https://v1.hitokoto.cn/"
if msg_type:
2023-06-16 18:09:08 +00:00
if msg_type in hitokoto_types:
2023-06-16 18:01:53 +00:00
url += f"?c={msg_type}"
2023-05-04 12:58:21 +00:00
else:
2023-06-16 18:01:53 +00:00
await msg.finish(msg.locale.t('hitokoto.message.error.type'))
2023-06-16 17:47:31 +00:00
data = await get_url(url, 200, fmt=json)
2023-06-16 18:01:53 +00:00
from_who = data.get("from_who", "")
2023-06-16 08:52:09 +00:00
tp = msg.locale.t('hitokoto.message.type') + msg.locale.t('hitokoto.message.type.' + data['type'])
2023-06-16 18:01:53 +00:00
link = f"https://hitokoto.cn?id={data['id']}"
response = f"{data['hitokoto']}\n——{from_who}{data['from']}\n{tp}\n{link}"
await msg.finish(response)