From 8fbb5742285061f87547c588e7d5ca5e2a7ca675 Mon Sep 17 00:00:00 2001 From: HibiKier <775757368@qq.com> Date: Mon, 26 Aug 2024 20:12:54 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20=E8=AF=8D=E6=9D=A1=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- zhenxun/plugins/word_bank/_config.py | 15 +++-- zhenxun/plugins/word_bank/_model.py | 71 +++++++++++++++--------- zhenxun/plugins/word_bank/word_handle.py | 4 +- 3 files changed, 58 insertions(+), 32 deletions(-) diff --git a/zhenxun/plugins/word_bank/_config.py b/zhenxun/plugins/word_bank/_config.py index d4d22b2b..72c4c584 100644 --- a/zhenxun/plugins/word_bank/_config.py +++ b/zhenxun/plugins/word_bank/_config.py @@ -1,32 +1,39 @@ from enum import Enum + 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 = { "全局": ScopeType.GLOBAL, "群聊": ScopeType.GROUP, "私聊": ScopeType.PRIVATE, } + class WordType(Enum): """ 精准、模糊、正则、图片 """ + EXACT = 0 FUZZY = 1 REGEX = 2 IMAGE = 3 + type2int = { "精准": WordType.EXACT, "模糊": WordType.FUZZY, @@ -35,8 +42,8 @@ type2int = { } int2type = { - WordType.EXACT: "精准", - WordType.FUZZY: "模糊", - WordType.REGEX: "正则", - WordType.IMAGE: "图片", + "EXACT": "精准", + "FUZZY": "模糊", + "REGEX": "正则", + "IMAGE": "图片", } diff --git a/zhenxun/plugins/word_bank/_model.py b/zhenxun/plugins/word_bank/_model.py index d33cf893..0d9be0c0 100644 --- a/zhenxun/plugins/word_bank/_model.py +++ b/zhenxun/plugins/word_bank/_model.py @@ -19,7 +19,7 @@ from zhenxun.utils.http_utils import AsyncHttpx from zhenxun.utils.image_utils import get_img_hash from zhenxun.utils.message import MessageUtils -from ._config import WordType, ScopeType, int2type +from ._config import ScopeType, WordType, int2type path = DATA_PATH / "word_bank" @@ -89,9 +89,9 @@ class WordBank(Model): if answer: query = query.filter(answer=answer) if word_type is not None: - query = query.filter(word_type=word_type) + query = query.filter(word_type=word_type.value) if word_scope is not None: - query = query.filter(word_scope=word_scope) + query = query.filter(word_scope=word_scope.value) return await query.exists() @classmethod @@ -138,8 +138,8 @@ class WordBank(Model): await cls.create( user_id=user_id, group_id=group_id, - word_scope=word_scope, - word_type=word_type, + word_scope=word_scope.value, + word_type=word_type.value, status=True, problem=str(problem).strip(), answer=new_answer, @@ -262,16 +262,22 @@ class WordBank(Model): query = cls if group_id: if word_scope: - query = query.filter(word_scope=word_scope) + query = query.filter(word_scope=word_scope.value) else: - query = query.filter(Q(group_id=group_id) | Q(word_scope=WordType.EXACT)) + query = query.filter( + Q(group_id=group_id) | Q(word_scope=WordType.EXACT.value) + ) else: - query = query.filter(Q(word_scope=ScopeType.PRIVATE) | Q(word_scope=ScopeType.GLOBAL)) + query = query.filter( + Q(word_scope=ScopeType.PRIVATE.value) + | Q(word_scope=ScopeType.GLOBAL.value) + ) if word_type: - query = query.filter(word_scope=word_type) + query = query.filter(word_scope=word_type.value) # 完全匹配 if data_list := await query.filter( - Q(Q(word_type=WordType.EXACT) | Q(word_type=WordType.IMAGE)), Q(problem=problem) + Q(Q(word_type=WordType.EXACT.value) | Q(word_type=WordType.IMAGE.value)), + Q(problem=problem), ).all(): return data_list db = Tortoise.get_connection("default") @@ -282,7 +288,8 @@ class WordBank(Model): return [cls(**data) for data in data_list] # 正则 sql = ( - query.filter(word_type=WordType.REGEX, word_scope__not=999).sql() + " and $1 ~ problem;" + query.filter(word_type=WordType.REGEX.value, word_scope__not=999).sql() + + " and $1 ~ problem;" ) data_list = await db.execute_query_dict(sql, [problem]) if data_list: @@ -356,9 +363,9 @@ class WordBank(Model): ) else: _problem = ( - await cls.filter(word_scope=(word_scope or ScopeType.GLOBAL)).order_by( - "create_time" - ) + await cls.filter( + word_scope=(word_scope or ScopeType.GLOBAL).value + ).order_by("create_time") # .group_by("problem") .values_list("problem", flat=True) ) @@ -371,7 +378,9 @@ class WordBank(Model): if index > len(sort_problem) - 1: return "下标错误,必须小于问题数量...", [] problem = sort_problem[index] # type: ignore - f = cls.filter(problem=problem, word_scope=(word_scope or 0)) + f = cls.filter( + problem=problem, word_scope=(word_scope or ScopeType.GLOBAL).value + ) if group_id: f = f.filter(group_id=group_id) answer_list = await f.all() @@ -399,21 +408,21 @@ class WordBank(Model): if index is not None: if group_id: query = await cls.filter( - group_id=group_id, problem=problem, word_scope=word_scope + group_id=group_id, problem=problem, word_scope=word_scope.value ).all() else: query = await cls.filter( - word_scope=word_scope, problem=problem + word_scope=word_scope.value, problem=problem ).all() await query[index].delete() else: if group_id: await WordBank.filter( - group_id=group_id, problem=problem, word_scope=word_scope + group_id=group_id, problem=problem, word_scope=word_scope.value ).delete() else: await WordBank.filter( - word_scope=word_scope, problem=problem + word_scope=word_scope.value, problem=problem ).delete() return True return False @@ -443,7 +452,9 @@ class WordBank(Model): if group_id: query = await cls.filter(group_id=group_id, problem=problem).all() else: - query = await cls.filter(word_scope=word_scope, problem=problem).all() + query = await cls.filter( + word_scope=word_scope.value, problem=problem + ).all() tmp = query[index].problem query[index].problem = replace_str await query[index].save(update_fields=["problem"]) @@ -454,7 +465,7 @@ class WordBank(Model): problem=replace_str ) else: - await cls.filter(word_scope=word_scope, problem=problem).update( + await cls.filter(word_scope=word_scope.value, problem=problem).update( problem=replace_str ) return problem @@ -471,14 +482,14 @@ class WordBank(Model): ) @classmethod - async def get_problem_by_scope(cls, word_scope: int): + async def get_problem_by_scope(cls, word_scope: ScopeType): """通过词条范围获取词条 参数: word_scope: 词条范围 """ return cls._handle_problem( - await cls.filter(word_scope=word_scope).order_by("create_time").all() # type: ignore + await cls.filter(word_scope=word_scope.value).order_by("create_time").all() # type: ignore ) @classmethod @@ -492,6 +503,13 @@ class WordBank(Model): await cls.filter(word_type=word_type).order_by("create_time").all() # type: ignore ) + @classmethod + def __type2int(cls, value: int) -> str: + for key, member in WordType.__members__.items(): + if member.value == value: + return key + return "" + @classmethod def _handle_problem(cls, problem_list: list["WordBank"]): """格式化处理问题 @@ -503,10 +521,11 @@ class WordBank(Model): result_list = [] for q in problem_list: if q.problem not in _tmp: + word_type = cls.__type2int(q.word_type) # TODO: 获取收录人名称 problem = ( (path / q.image_path, 30, 30) if q.image_path else q.problem, - int2type[q.word_type], + int2type[word_type], # q.author, "-", ) @@ -541,8 +560,8 @@ class WordBank(Model): await cls.create( user_id=user_id, group_id=group_id, - word_scope=word_scope, - word_type=word_type, + word_scope=word_scope.value, + word_type=word_type.value, status=True, problem=problem, answer=answer, diff --git a/zhenxun/plugins/word_bank/word_handle.py b/zhenxun/plugins/word_bank/word_handle.py index a87e9e75..1fa28958 100644 --- a/zhenxun/plugins/word_bank/word_handle.py +++ b/zhenxun/plugins/word_bank/word_handle.py @@ -19,7 +19,7 @@ from zhenxun.configs.utils import PluginExtraData from zhenxun.services.log import logger from zhenxun.utils.message import MessageUtils -from ._config import WordType, ScopeType, scope2int, type2int +from ._config import ScopeType, WordType, 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 @@ -43,7 +43,7 @@ __plugin_meta__ = PluginMetadata( 添加词条 ?[模糊|正则|图片]问...答...:添加问答词条,可重复添加相同问题的不同回答 示例: 添加词条问你好答你也好 - 添加图片词条问答看看涩图 + 添加词条图片问答看看涩图 删除词条 ?[问题] ?[序号] ?[回答序号]:删除指定词条指定或全部回答 示例: 删除词条 谁是萝莉 : 删除文字是 谁是萝莉 的词条