summaryrefslogtreecommitdiff
path: root/ATRI/plugins/hitokoto.py
blob: 36a5f33689247a41689b746fb34b67422a30a9b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# -*- coding:utf-8 -*-
import json
from nonebot import on_command, on_natural_language, CommandSession
from nonebot import NLPSession, NLPResult
from ATRI.modules import response # type: ignore


url = f'https://api.imjad.cn/hitokoto/?cat=a&charset=utf-8&length=50&encode=json&fun=sync&source='


HITOKOTO_REPLY = """{hitokoto}
                from {author}"""


@on_command('hitokoto', aliases = ['一言'], only_to_me = False)
async def hitokoto(session: CommandSession):
    rep = response.request_api(url)
    
    if not rep:
        session.finish('获取失败')
    
    dc = json.loads(response.request_api(url))

    await session.send(
        HITOKOTO_REPLY.format(
            hitokoto = dc["hitokoto"],
            author = dc["author"]
        )
    )

@on_natural_language('一言', only_to_me = False)
async def _(session: NLPSession):
    return NLPResult(60.0, ('hitokoto'), None)