🐛修复词库问答查看全局词条问题+代码优化 (#1580)

This commit is contained in:
AkashiCoin 2024-08-26 17:03:57 +08:00 committed by GitHub
parent 073cab5a85
commit 6fbc87b53f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 44 additions and 25 deletions

View File

@ -1,24 +1,43 @@
from enum import Enum
from pickle import GLOBAL
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):
"""
全局群聊私聊
"""
GLOBAL = 0
GROUP = 1
PRIVATE = 2
scope2int = { scope2int = {
"全局": 0, "全局": ScopeType.GLOBAL,
"群聊": 1, "群聊": ScopeType.GROUP,
"私聊": 2, "私聊": ScopeType.PRIVATE,
} }
class WordType(Enum):
"""
精准模糊正则图片
"""
EXACT = 0
FUZZY = 1
REGEX = 2
IMAGE = 3
type2int = { type2int = {
"精准": 0, "精准": WordType.EXACT,
"模糊": 1, "模糊": WordType.FUZZY,
"正则": 2, "正则": WordType.REGEX,
"图片": 3, "图片": WordType.IMAGE,
} }
int2type = { int2type = {
0: "精准", WordType.EXACT: "精准",
1: "模糊", WordType.FUZZY: "模糊",
2: "正则", WordType.REGEX: "正则",
3: "图片", WordType.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 int2type from ._config import WordType, ScopeType, int2type
path = DATA_PATH / "word_bank" path = DATA_PATH / "word_bank"
@ -309,7 +309,7 @@ class WordBank(Model):
data_list = await cls.check_problem(group_id, problem, word_scope, word_type) data_list = await cls.check_problem(group_id, problem, word_scope, word_type)
if data_list: if data_list:
random_answer = random.choice(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) r = re.search(random_answer.problem, problem)
has_placeholder = re.search(rf"\$(\d)", random_answer.answer) has_placeholder = re.search(rf"\$(\d)", random_answer.answer)
if r and r.groups() and has_placeholder: if r and r.groups() and has_placeholder:
@ -348,7 +348,7 @@ class WordBank(Model):
""" """
if index is not None: if index is not None:
# TODO: group_by和order_by不能同时使用 # TODO: group_by和order_by不能同时使用
if group_id: if group_id and word_scope != ScopeType.GLOBAL:
_problem = ( _problem = (
await cls.filter(group_id=group_id).order_by("create_time") await cls.filter(group_id=group_id).order_by("create_time")
# .group_by("problem") # .group_by("problem")
@ -356,7 +356,7 @@ class WordBank(Model):
) )
else: else:
_problem = ( _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" "create_time"
) )
# .group_by("problem") # .group_by("problem")

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 scope2int, type2int from ._config import WordType, ScopeType, 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
@ -172,8 +172,8 @@ async def _(
if group_id and (not word_scope or word_scope == "私聊") if group_id and (not word_scope or word_scope == "私聊")
else "0" else "0"
), ),
scope2int[word_scope] if word_scope else 1, scope2int[word_scope] if word_scope else ScopeType.GROUP,
type2int[word_type] if word_type else 0, type2int[word_type] if word_type else WordType.EXACT,
problem, problem,
answer, answer,
nickname[0] if nickname else None, nickname[0] if nickname else None,
@ -220,9 +220,9 @@ async def _(
await MessageUtils.build_message( await MessageUtils.build_message(
"此命令之后需要跟随指定词条或id通过“显示词条“查看" "此命令之后需要跟随指定词条或id通过“显示词条“查看"
).finish(reply_to=True) ).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: if all.result:
word_scope = 0 word_scope = ScopeType.GLOBAL
if gid := session.id3 or session.id2: if gid := session.id3 or session.id2:
result, _ = await WordBankManage.delete_word( result, _ = await WordBankManage.delete_word(
problem.result, problem.result,
@ -259,9 +259,9 @@ async def _(
await MessageUtils.build_message( await MessageUtils.build_message(
"此命令之后需要跟随指定词条或id通过“显示词条“查看" "此命令之后需要跟随指定词条或id通过“显示词条“查看"
).finish(reply_to=True) ).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: if all.result:
word_scope = 0 word_scope = ScopeType.GLOBAL
if gid := session.id3 or session.id2: if gid := session.id3 or session.id2:
result, old_problem = await WordBankManage.update_word( result, old_problem = await WordBankManage.update_word(
replace, replace,
@ -297,16 +297,16 @@ async def _(
arparma: Arparma, arparma: Arparma,
all: Query[bool] = AlconnaQuery("all.value", False), 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: if all.result:
word_scope = 0 word_scope = 0
group_id = session.id3 or session.id2
if gid.available: if gid.available:
group_id = gid.result group_id = gid.result
if problem.available: if problem.available:
if index.available: if index.available:
if index.result < 0 or index.result > len( 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( await MessageUtils.build_message("id必须在范围内...").finish(
reply_to=True reply_to=True