diff options
author | Kyomotoi <[email protected]> | 2021-02-06 00:32:26 +0800 |
---|---|---|
committer | Kyomotoi <[email protected]> | 2021-02-06 00:32:26 +0800 |
commit | f5ceb8927f2e7f2a9e29d62c8e4cef876f917249 (patch) | |
tree | 40b9dcd6b7d3db486054e3aa9b5a04d25fa2284e /ATRI/plugins/hitokoto.py | |
parent | eb52fab79ada7efe6191e3a5f90179766feaded0 (diff) | |
download | ATRI-f5ceb8927f2e7f2a9e29d62c8e4cef876f917249.tar.gz ATRI-f5ceb8927f2e7f2a9e29d62c8e4cef876f917249.tar.bz2 ATRI-f5ceb8927f2e7f2a9e29d62c8e4cef876f917249.zip |
🏗 💩 更改项目结构,修复啥b BUG
Diffstat (limited to 'ATRI/plugins/hitokoto.py')
-rw-r--r-- | ATRI/plugins/hitokoto.py | 106 |
1 files changed, 43 insertions, 63 deletions
diff --git a/ATRI/plugins/hitokoto.py b/ATRI/plugins/hitokoto.py index cf03b6f..7f7eae6 100644 --- a/ATRI/plugins/hitokoto.py +++ b/ATRI/plugins/hitokoto.py @@ -1,76 +1,56 @@ +import os 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 nonebot.adapters.cqhttp import Bot, MessageEvent -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 +from ATRI.rule import ( + is_in_banlist, + is_in_dormant, + is_in_service, + to_bot +) +from ATRI.exceptions import LoadingError +from ATRI.utils.list import count_list, del_list_aim -# ===========================[Begin Command Processing]=========================== + +HITOKOTO_DIR = Path('.') / 'ATRI' / 'data' / 'database' / 'hitokoto' +sick_list = [] __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 = on_command( + "一言", + aliases={"抑郁一下", "网抑云"}, + rule=is_in_banlist() & 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() +async def _hitokoto(bot: Bot, event: MessageEvent) -> None: + global sick_list + user = event.get_user_id() + + if count_list(sick_list, user) == 3: + sick_list.append(user) + await hitokoto.finish("额......需要咱安慰一下嘛~?") + elif count_list(sick_list, user) == 6: + sick_list = del_list_aim(sick_list, user) + msg = ( + "如果心里感到难受就赶快去睡觉!别再憋自己了!\n" + "我...我会守在你身边的!...嗯..一定" + ) + await hitokoto.finish(msg) + else: + sick_list.append(user) + rd = choice(os.listdir(HITOKOTO_DIR)) + path = HITOKOTO_DIR / rd + data = {} + try: + data = json.loads(path.read_bytes()) + except LoadingError: + raise LoadingError("Loading error!") + await hitokoto.finish(data[randint(1, len(data) - 1)]['hitokoto']) |