diff --git a/zhenxun/plugins/word_bank/_config.py b/zhenxun/plugins/word_bank/_config.py index 3d074c182..d003b1394 100644 --- a/zhenxun/plugins/word_bank/_config.py +++ b/zhenxun/plugins/word_bank/_config.py @@ -1,24 +1,43 @@ +from enum import Enum +from pickle import GLOBAL from zhenxun.configs.path_config import DATA_PATH data_dir = DATA_PATH / "word_bank" data_dir.mkdir(parents=True, exist_ok=True) +class ScopeType(Enum): + """ + 全局、群聊、私聊 + """ + GLOBAL = 0 + GROUP = 1 + PRIVATE = 2 + scope2int = { - "全局": 0, - "群聊": 1, - "私聊": 2, + "全局": ScopeType.GLOBAL, + "群聊": ScopeType.GROUP, + "私聊": ScopeType.PRIVATE, } +class WordType(Enum): + """ + 精准、模糊、正则、图片 + """ + EXACT = 0 + FUZZY = 1 + REGEX = 2 + IMAGE = 3 + type2int = { - "精准": 0, - "模糊": 1, - "正则": 2, - "图片": 3, + "精准": WordType.EXACT, + "模糊": WordType.FUZZY, + "正则": WordType.REGEX, + "图片": WordType.IMAGE, } int2type = { - 0: "精准", - 1: "模糊", - 2: "正则", - 3: "图片", + WordType.EXACT: "精准", + WordType.FUZZY: "模糊", + WordType.REGEX: "正则", + WordType.IMAGE: "图片", } diff --git a/zhenxun/plugins/word_bank/_model.py b/zhenxun/plugins/word_bank/_model.py index f31620a2b..abccdc618 100644 --- a/zhenxun/plugins/word_bank/_model.py +++ b/zhenxun/plugins/word_bank/_model.py @@ -19,7 +19,7 @@ from zhenxun.utils.image_utils import get_img_hash from zhenxun.utils.message import MessageUtils -from ._config import int2type +from ._config import WordType, ScopeType, int2type path = DATA_PATH / "word_bank" @@ -309,7 +309,7 @@ async def get_answer( data_list = await cls.check_problem(group_id, problem, word_scope, word_type) if data_list: random_answer = random.choice(data_list) - if random_answer.word_type == 2: + if random_answer.word_type == WordType.REGEX: r = re.search(random_answer.problem, problem) has_placeholder = re.search(rf"\$(\d)", random_answer.answer) if r and r.groups() and has_placeholder: @@ -348,7 +348,7 @@ async def get_problem_all_answer( """ if index is not None: # TODO: group_by和order_by不能同时使用 - if group_id: + if group_id and word_scope != ScopeType.GLOBAL: _problem = ( await cls.filter(group_id=group_id).order_by("create_time") # .group_by("problem") @@ -356,7 +356,7 @@ async def get_problem_all_answer( ) else: _problem = ( - await cls.filter(word_scope=(word_scope or 0)).order_by( + await cls.filter(word_scope=(word_scope or ScopeType.GLOBAL)).order_by( "create_time" ) # .group_by("problem") diff --git a/zhenxun/plugins/word_bank/word_handle.py b/zhenxun/plugins/word_bank/word_handle.py index 39894a459..a87e9e75f 100644 --- a/zhenxun/plugins/word_bank/word_handle.py +++ b/zhenxun/plugins/word_bank/word_handle.py @@ -19,7 +19,7 @@ from zhenxun.services.log import logger from zhenxun.utils.message import MessageUtils -from ._config import scope2int, type2int +from ._config import WordType, ScopeType, scope2int, type2int from ._data_source import WordBankManage, get_answer, get_img_and_at_list, get_problem from ._model import WordBank from .command import _add_matcher, _del_matcher, _show_matcher, _update_matcher @@ -172,8 +172,8 @@ async def _( if group_id and (not word_scope or word_scope == "私聊") else "0" ), - scope2int[word_scope] if word_scope else 1, - type2int[word_type] if word_type else 0, + scope2int[word_scope] if word_scope else ScopeType.GROUP, + type2int[word_type] if word_type else WordType.EXACT, problem, answer, nickname[0] if nickname else None, @@ -220,9 +220,9 @@ async def _( await MessageUtils.build_message( "此命令之后需要跟随指定词条或id,通过“显示词条“查看" ).finish(reply_to=True) - word_scope = 1 if session.id3 or session.id2 else 2 + word_scope = ScopeType.GROUP if session.id3 or session.id2 else ScopeType.PRIVATE if all.result: - word_scope = 0 + word_scope = ScopeType.GLOBAL if gid := session.id3 or session.id2: result, _ = await WordBankManage.delete_word( problem.result, @@ -259,9 +259,9 @@ async def _( await MessageUtils.build_message( "此命令之后需要跟随指定词条或id,通过“显示词条“查看" ).finish(reply_to=True) - word_scope = 1 if session.id3 or session.id2 else 2 + word_scope = ScopeType.GROUP if session.id3 or session.id2 else ScopeType.PRIVATE if all.result: - word_scope = 0 + word_scope = ScopeType.GLOBAL if gid := session.id3 or session.id2: result, old_problem = await WordBankManage.update_word( replace, @@ -297,16 +297,16 @@ async def _( arparma: Arparma, all: Query[bool] = AlconnaQuery("all.value", False), ): - word_scope = 1 if session.id3 or session.id2 else 2 + word_scope = ScopeType.GROUP if session.id3 or session.id2 else ScopeType.PRIVATE + group_id = session.id3 or session.id2 if all.result: word_scope = 0 - group_id = session.id3 or session.id2 if gid.available: group_id = gid.result if problem.available: if index.available: if index.result < 0 or index.result > len( - await WordBank.get_problem_by_scope(2) + await WordBank.get_problem_by_scope(word_scope) ): await MessageUtils.build_message("id必须在范围内...").finish( reply_to=True