summaryrefslogtreecommitdiff
path: root/ATRI/plugins/hitokoto.py
blob: cf03b6f68954d3431462e4ed1ec5222b7dbfc1ce (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import json
from pathlib import Path
from random import choice, randint

from nonebot.plugin import on_command
from nonebot.adapters.cqhttp import Bot, Event

from ATRI.exceptions import InvalidLoad
from ATRI.rule import is_in_ban_list, is_in_dormant, is_in_service, to_bot
from ATRI.utils import del_list_aim, count_list
from ATRI.request import Request
from ATRI.config import HITOKOTO_CONFIG
from ATRI.service.plugin import Plugin

# ===========================[Begin Command Processing]===========================


__plugin_name__ = 'hitokoto'
__doc__ = """一言"""
Plugin.register(__plugin_name__, "func", __doc__,
         HITOKOTO_CONFIG['hitokoto']['command'])

hitokoto = on_command(HITOKOTO_CONFIG['hitokoto']['command'][0],
                        aliases=set(HITOKOTO_CONFIG['hitokoto']['command']),
                        rule=is_in_ban_list() & is_in_dormant()
                        & is_in_service(__plugin_name__)
                        & to_bot())

@hitokoto.handle()
async def _(bot: Bot, event: Event) -> None:
    await bot.send(event, await Function().hitokoto(str(event.get_user_id())))


# ===========================[End Command Processing]=============================

hitokoto_list = []
local_path = Path('.') / 'ATRI' / 'data' / 'database' / 'hitokoto'

class Function:
    async def hitokoto(self, user: str):
        def local() -> str:
            rd = choice(HITOKOTO_CONFIG['hitokoto']['local']['file'])
            path = local_path / f"{rd}"
            data = {}
            try:
                data = json.loads(path.read_bytes())
            except InvalidLoad:
                raise InvalidLoad('Failed to read file!')
            return data[randint(1, len(data)) - 1]['hitokoto']
        
        async def link() -> str:
            url = HITOKOTO_CONFIG['hitokoto']['link']['url']
            return str(
                await Request.get_text(
                    url=url
                )
            )
        
        global hitokoto_list

        if count_list(hitokoto_list, user) == 3:
            hitokoto_list.append(user)
            return choice(HITOKOTO_CONFIG['hitokoto']['times'][3]['repo'])
        elif count_list(hitokoto_list, user) == 6:
            hitokoto_list = del_list_aim(hitokoto_list, user)
            return choice(HITOKOTO_CONFIG['hitokoto']['times'][6]['repo'])
        else:
            hitokoto_list.append(user)
            if HITOKOTO_CONFIG['hitokoto']['link']['use']:
                rd = randint(1,2)
                if rd == 1:
                    return await link()
                else:
                    return local()
            else:
                return local()