Skip to content

Commit

Permalink
🐛修复词库问答查看全局词条问题+代码优化 (#1580)
Browse files Browse the repository at this point in the history
  • Loading branch information
AkashiCoin authored Aug 26, 2024
1 parent 073cab5 commit 6fbc87b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 25 deletions.
41 changes: 30 additions & 11 deletions zhenxun/plugins/word_bank/_config.py
Original file line number Diff line number Diff line change
@@ -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: "图片",
}
8 changes: 4 additions & 4 deletions zhenxun/plugins/word_bank/_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -348,15 +348,15 @@ 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")
.values_list("problem", flat=True)
)
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")
Expand Down
20 changes: 10 additions & 10 deletions zhenxun/plugins/word_bank/word_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6fbc87b

Please sign in to comment.