diff options
author | Kyomotoi <1172294279@qq.com> | 2021-01-26 18:43:13 +0800 |
---|---|---|
committer | Kyomotoi <1172294279@qq.com> | 2021-01-26 18:43:13 +0800 |
commit | 7640568a42493bc5a5e44bc82b1ecfa87e51c5f1 (patch) | |
tree | 60f29b756d58b085c95573515d3e4bd6f41426a8 /ATRI/plugins/hitokoto.py | |
parent | d0d31b2630697c97e848f00142f06b81f63b255a (diff) | |
download | ATRI-7640568a42493bc5a5e44bc82b1ecfa87e51c5f1.tar.gz ATRI-7640568a42493bc5a5e44bc82b1ecfa87e51c5f1.tar.bz2 ATRI-7640568a42493bc5a5e44bc82b1ecfa87e51c5f1.zip |
[Update]
Diffstat (limited to 'ATRI/plugins/hitokoto.py')
-rw-r--r-- | ATRI/plugins/hitokoto.py | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/ATRI/plugins/hitokoto.py b/ATRI/plugins/hitokoto.py index e69de29..cf03b6f 100644 --- a/ATRI/plugins/hitokoto.py +++ b/ATRI/plugins/hitokoto.py @@ -0,0 +1,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() |