🐛 词条修复

This commit is contained in:
HibiKier 2024-08-26 20:12:54 +08:00
parent af198dcfaa
commit 8fbb574228
3 changed files with 58 additions and 32 deletions

View File

@ -1,32 +1,39 @@
from enum import Enum from enum import Enum
from zhenxun.configs.path_config import DATA_PATH from zhenxun.configs.path_config import DATA_PATH
data_dir = DATA_PATH / "word_bank" data_dir = DATA_PATH / "word_bank"
data_dir.mkdir(parents=True, exist_ok=True) data_dir.mkdir(parents=True, exist_ok=True)
class ScopeType(Enum): class ScopeType(Enum):
""" """
全局群聊私聊 全局群聊私聊
""" """
GLOBAL = 0 GLOBAL = 0
GROUP = 1 GROUP = 1
PRIVATE = 2 PRIVATE = 2
scope2int = { scope2int = {
"全局": ScopeType.GLOBAL, "全局": ScopeType.GLOBAL,
"群聊": ScopeType.GROUP, "群聊": ScopeType.GROUP,
"私聊": ScopeType.PRIVATE, "私聊": ScopeType.PRIVATE,
} }
class WordType(Enum): class WordType(Enum):
""" """
精准模糊正则图片 精准模糊正则图片
""" """
EXACT = 0 EXACT = 0
FUZZY = 1 FUZZY = 1
REGEX = 2 REGEX = 2
IMAGE = 3 IMAGE = 3
type2int = { type2int = {
"精准": WordType.EXACT, "精准": WordType.EXACT,
"模糊": WordType.FUZZY, "模糊": WordType.FUZZY,
@ -35,8 +42,8 @@ type2int = {
} }
int2type = { int2type = {
WordType.EXACT: "精准", "EXACT": "精准",
WordType.FUZZY: "模糊", "FUZZY": "模糊",
WordType.REGEX: "正则", "REGEX": "正则",
WordType.IMAGE: "图片", "IMAGE": "图片",
} }

View File

@ -19,7 +19,7 @@ from zhenxun.utils.http_utils import AsyncHttpx
from zhenxun.utils.image_utils import get_img_hash from zhenxun.utils.image_utils import get_img_hash
from zhenxun.utils.message import MessageUtils from zhenxun.utils.message import MessageUtils
from ._config import WordType, ScopeType, int2type from ._config import ScopeType, WordType, int2type
path = DATA_PATH / "word_bank" path = DATA_PATH / "word_bank"
@ -89,9 +89,9 @@ class WordBank(Model):
if answer: if answer:
query = query.filter(answer=answer) query = query.filter(answer=answer)
if word_type is not None: 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: 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() return await query.exists()
@classmethod @classmethod
@ -138,8 +138,8 @@ class WordBank(Model):
await cls.create( await cls.create(
user_id=user_id, user_id=user_id,
group_id=group_id, group_id=group_id,
word_scope=word_scope, word_scope=word_scope.value,
word_type=word_type, word_type=word_type.value,
status=True, status=True,
problem=str(problem).strip(), problem=str(problem).strip(),
answer=new_answer, answer=new_answer,
@ -262,16 +262,22 @@ class WordBank(Model):
query = cls query = cls
if group_id: if group_id:
if word_scope: if word_scope:
query = query.filter(word_scope=word_scope) query = query.filter(word_scope=word_scope.value)
else: 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: 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: if word_type:
query = query.filter(word_scope=word_type) query = query.filter(word_scope=word_type.value)
# 完全匹配 # 完全匹配
if data_list := await query.filter( 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(): ).all():
return data_list return data_list
db = Tortoise.get_connection("default") db = Tortoise.get_connection("default")
@ -282,7 +288,8 @@ class WordBank(Model):
return [cls(**data) for data in data_list] return [cls(**data) for data in data_list]
# 正则 # 正则
sql = ( 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]) data_list = await db.execute_query_dict(sql, [problem])
if data_list: if data_list:
@ -356,9 +363,9 @@ class WordBank(Model):
) )
else: else:
_problem = ( _problem = (
await cls.filter(word_scope=(word_scope or ScopeType.GLOBAL)).order_by( await cls.filter(
"create_time" word_scope=(word_scope or ScopeType.GLOBAL).value
) ).order_by("create_time")
# .group_by("problem") # .group_by("problem")
.values_list("problem", flat=True) .values_list("problem", flat=True)
) )
@ -371,7 +378,9 @@ class WordBank(Model):
if index > len(sort_problem) - 1: if index > len(sort_problem) - 1:
return "下标错误,必须小于问题数量...", [] return "下标错误,必须小于问题数量...", []
problem = sort_problem[index] # type: ignore 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: if group_id:
f = f.filter(group_id=group_id) f = f.filter(group_id=group_id)
answer_list = await f.all() answer_list = await f.all()
@ -399,21 +408,21 @@ class WordBank(Model):
if index is not None: if index is not None:
if group_id: if group_id:
query = await cls.filter( 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() ).all()
else: else:
query = await cls.filter( query = await cls.filter(
word_scope=word_scope, problem=problem word_scope=word_scope.value, problem=problem
).all() ).all()
await query[index].delete() await query[index].delete()
else: else:
if group_id: if group_id:
await WordBank.filter( 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() ).delete()
else: else:
await WordBank.filter( await WordBank.filter(
word_scope=word_scope, problem=problem word_scope=word_scope.value, problem=problem
).delete() ).delete()
return True return True
return False return False
@ -443,7 +452,9 @@ class WordBank(Model):
if group_id: if group_id:
query = await cls.filter(group_id=group_id, problem=problem).all() query = await cls.filter(group_id=group_id, problem=problem).all()
else: 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 tmp = query[index].problem
query[index].problem = replace_str query[index].problem = replace_str
await query[index].save(update_fields=["problem"]) await query[index].save(update_fields=["problem"])
@ -454,7 +465,7 @@ class WordBank(Model):
problem=replace_str problem=replace_str
) )
else: 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 problem=replace_str
) )
return problem return problem
@ -471,14 +482,14 @@ class WordBank(Model):
) )
@classmethod @classmethod
async def get_problem_by_scope(cls, word_scope: int): async def get_problem_by_scope(cls, word_scope: ScopeType):
"""通过词条范围获取词条 """通过词条范围获取词条
参数: 参数:
word_scope: 词条范围 word_scope: 词条范围
""" """
return cls._handle_problem( 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 @classmethod
@ -492,6 +503,13 @@ class WordBank(Model):
await cls.filter(word_type=word_type).order_by("create_time").all() # type: ignore 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 @classmethod
def _handle_problem(cls, problem_list: list["WordBank"]): def _handle_problem(cls, problem_list: list["WordBank"]):
"""格式化处理问题 """格式化处理问题
@ -503,10 +521,11 @@ class WordBank(Model):
result_list = [] result_list = []
for q in problem_list: for q in problem_list:
if q.problem not in _tmp: if q.problem not in _tmp:
word_type = cls.__type2int(q.word_type)
# TODO: 获取收录人名称 # TODO: 获取收录人名称
problem = ( problem = (
(path / q.image_path, 30, 30) if q.image_path else q.problem, (path / q.image_path, 30, 30) if q.image_path else q.problem,
int2type[q.word_type], int2type[word_type],
# q.author, # q.author,
"-", "-",
) )
@ -541,8 +560,8 @@ class WordBank(Model):
await cls.create( await cls.create(
user_id=user_id, user_id=user_id,
group_id=group_id, group_id=group_id,
word_scope=word_scope, word_scope=word_scope.value,
word_type=word_type, word_type=word_type.value,
status=True, status=True,
problem=problem, problem=problem,
answer=answer, answer=answer,

View File

@ -19,7 +19,7 @@ from zhenxun.configs.utils import PluginExtraData
from zhenxun.services.log import logger from zhenxun.services.log import logger
from zhenxun.utils.message import MessageUtils 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 ._data_source import WordBankManage, get_answer, get_img_and_at_list, get_problem
from ._model import WordBank from ._model import WordBank
from .command import _add_matcher, _del_matcher, _show_matcher, _update_matcher from .command import _add_matcher, _del_matcher, _show_matcher, _update_matcher
@ -43,7 +43,7 @@ __plugin_meta__ = PluginMetadata(
添加词条 ?[模糊|正则|图片]......添加问答词条可重复添加相同问题的不同回答 添加词条 ?[模糊|正则|图片]......添加问答词条可重复添加相同问题的不同回答
示例: 示例:
添加词条问你好答你也好 添加词条问你好答你也好
添加图片词条问答看看涩图 添加词条图片问答看看涩图
删除词条 ?[问题] ?[序号] ?[回答序号]删除指定词条指定或全部回答 删除词条 ?[问题] ?[序号] ?[回答序号]删除指定词条指定或全部回答
示例: 示例:
删除词条 谁是萝莉 : 删除文字是 谁是萝莉 的词条 删除词条 谁是萝莉 : 删除文字是 谁是萝莉 的词条