update search_type

This commit is contained in:
yajiwa 2022-04-05 22:15:47 +08:00
parent 188f6f0f99
commit a45b95d5d4
3 changed files with 45 additions and 31 deletions

View File

@ -28,13 +28,13 @@ class WordBankBuilder:
"""
self._data["answer"] = answer
async def save(self):
async def save(self,search_type):
user_id = self._data["user_id"]
group_id = self._data["group_id"]
problem = self._data["problem"]
answer = self._data["answer"]
placeholder = self._data.get("placeholder")
await WordBank.add_problem_answer(user_id, group_id, problem, answer, placeholder)
return await WordBank.add_problem_answer(user_id, group_id, search_type,problem, answer, placeholder)
async def update(self, index):
user_id = self._data["user_id"]

View File

@ -25,6 +25,7 @@ class WordBank(db.Model):
cls,
user_id: int,
group_id: Optional[int],
search_type: [int],
problem: str,
answer: str,
format_: Optional[List[Tuple[int, Union[int, str]]]],
@ -33,6 +34,7 @@ class WordBank(db.Model):
添加或新增一个问答
:param user_id: 用户id
:param group_id: 群号
:search_type: 问题类型,
:param problem: 问题
:param answer: 回答
:param format_: 格式化数据
@ -43,7 +45,7 @@ class WordBank(db.Model):
for x, y in format_:
_str += f"{x}<_s>{y}<format>"
return await cls._problem_answer_handle(
user_id, group_id, problem, "add", answer=answer, format_=_str
user_id, group_id, problem, "add", search_type=search_type, answer=answer, format_=_str
)
@classmethod
@ -159,22 +161,23 @@ class WordBank(db.Model):
if problem:
FUZZY = Config.get_config("word_bank", "WORD_BANK_FUZZY")
KEY = Config.get_config("word_bank", "WORD_BANK_KEY")
q = await cls.query.where(
(cls.group_id == group_id) & (cls.problem == problem)
).gino.all()
if KEY and FUZZY:
q_fuzzy = await cls.query.where(
(cls.group_id == group_id) & (cls.problem.contains(f'{problem}'))).gino.all()
q_key = await cls.query.where(cls.group_id == group_id).gino.all()
(cls.group_id == group_id) & (cls.search_type == 2) & (
cls.problem.contains(f'{problem}'))).gino.all()
q_key = await cls.query.where((cls.group_id == group_id) & (cls.search_type == 1)).gino.all()
q_key = [x for x in q_key if str(x.problem) in (problem)]
q = q_fuzzy + q_key
q += q_fuzzy + q_key
elif FUZZY:
q = await cls.query.where(
(cls.group_id == group_id) & (cls.problem.contains(f'{problem}'))).gino.all()
(cls.group_id == group_id) & (cls.search_type == 2) & (
cls.problem.contains(f'{problem}'))).gino.all()
elif KEY:
q = await cls.query.where(cls.group_id == group_id).gino.all()
q = await cls.query.where((cls.group_id == group_id) & (cls.search_type == 1)).gino.all()
q = [x for x in q if str(x.problem) in (problem)]
else:
q = await cls.query.where(
(cls.group_id == group_id) & (cls.problem == problem)
).gino.all()
else:
return None
@ -188,6 +191,7 @@ class WordBank(db.Model):
problem: str,
type_: str,
*,
search_type: [int] = 0,
answer: Optional[str] = None,
index: Optional[int] = None,
format_: Optional[str] = None,
@ -213,17 +217,21 @@ class WordBank(db.Model):
else:
q = cls.query.where((cls.user_qq == user_id) & (cls.problem == problem))
if type_ == "add":
q = await q.where(cls.answer == answer).gino.all()
if not q or ".jpg" in format_:
await cls.create(
user_qq=user_id,
group_id=group_id,
problem=problem,
answer=answer,
format=format_,
create_time=datetime.now().replace(microsecond=0),
update_time=datetime.now().replace(microsecond=0),
)
q = await q.where((cls.answer == answer) & (cls.search_type == search_type)).gino.all()
try:
if not q or ".jpg" in format_:
await cls.create(
user_qq=user_id,
group_id=group_id,
search_type=search_type,
problem=problem,
answer=answer,
format=format_,
create_time=datetime.now().replace(microsecond=0),
update_time=datetime.now().replace(microsecond=0),
)
except:
return False
return True
elif type_ == "delete":
q = await q.with_for_update().gino.all()

View File

@ -19,7 +19,6 @@ from nonebot import on_command
import random
import os
import re
from configs.config import NICKNAME, Config
__zx_plugin_name__ = "词库问答 [Admin]"
__plugin_usage__ = """
@ -27,7 +26,7 @@ usage
对指定问题的随机回答对相同问题可以设置多个不同回答
删除词条后每个词条的id可能会变化请查看后再删除
指令
添加词条......添加问答词条可重复添加相同问题的不同回答
添加词条 ?[模糊/关键字]......添加问答词条可重复添加相同问题的不同回答
删除词条 [问题/下标] ?[下标]删除指定词条指定或全部回答
修改词条 [问题/下标] ?[下标/新回答] [新回答]修改指定词条指定回答默认修改为第一条
查看词条 ?[问题/下标]查看全部词条或对应词条回答
@ -44,12 +43,12 @@ usage
""".strip()
__plugin_des__ = "自定义词条内容随机回复"
__plugin_cmd__ = [
"添加词条问...答..",
"添加词条 ?[模糊/关键字]问...答..",
"删除词条 [问题/下标] ?[下标]",
"修改词条 [问题/下标] ?[下标/新回答] [新回答]",
"查看词条 ?[问题/下标]",
]
__plugin_version__ = 0.1
__plugin_version__ = 0.2
__plugin_author__ = "HibiKier"
__plugin_settings__ = {
"admin_level": Config.get_config("word_bank", "WORD_BANK_LEVEL [LEVEL]"),
@ -71,7 +70,7 @@ show_word = on_command("显示词条", aliases={"查看词条"}, priority=5, blo
@add_word.handle()
async def _(bot: Bot, event: GroupMessageEvent, state: T_State, arg: Message = CommandArg()):
msg = str(arg)
r = re.search(r"^问(.+)\s?答([\s\S]*)", msg)
r = re.search(r"问(.+)\s?答([\s\S]*)", msg)
if not r:
await add_word.finish("未检测到词条问题...")
problem = r.group(1).strip()
@ -87,10 +86,17 @@ async def _(bot: Bot, event: GroupMessageEvent, state: T_State, arg: Message = C
break
else:
_problem = problem
search_type = 0
if re.search("^关键字(.*)", msg):
search_type = 1
elif re.search("^模糊(.*)", msg):
search_type = 2
_builder = await get__builder(event, _problem, answer, idx)
await _builder.save()
logger.info(f"已保存词条 问:{problem} 答:{msg}")
await add_word.send(f"已保存词条:{problem}")
if await _builder.save(search_type):
logger.info(f"已保存词条 问:{problem} 答:{msg}")
await add_word.send(f"已保存词条:{problem}")
else:
await delete_word.send("保存失败,可能是回答重复")
@delete_word.handle()