From b59699dabe5b98b5ff10752fb8a57823a77caa05 Mon Sep 17 00:00:00 2001 From: HibiKier <775757368@qq.com> Date: Sun, 11 Dec 2022 00:10:07 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=8D=E6=9D=A1=E9=97=AE=E9=A2=98=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E7=9C=9F=E5=AF=BB=E7=9A=84=E6=98=B5=E7=A7=B0=E5=BC=80?= =?UTF-8?q?=E5=A4=B4=E4=B8=8Eat=E7=9C=9F=E5=AF=BB=E5=BC=80=E5=A4=B4?= =?UTF-8?q?=E5=B9=B6=E4=BC=98=E5=8C=96=E5=9B=9E=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +++ plugins/withdraw.py | 5 ++-- plugins/word_bank/__init__.py | 3 +++ plugins/word_bank/_data_source.py | 5 ++-- plugins/word_bank/_model.py | 44 ++++++++++++++++++------------- plugins/word_bank/_rule.py | 11 ++++++-- plugins/word_bank/word_handle.py | 9 ++++++- 7 files changed, 55 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 03092473..eca39c59 100644 --- a/README.md +++ b/README.md @@ -296,6 +296,10 @@ PS: **ARM平台** 请使用全量版 同时 **如果你的机器 RAM < 1G 可能 ## 更新 +### 2022/12/11 + +* 词条问题支持真寻的昵称开头与at真寻开头并优化回复 + ### 2022/12/10 * 重写帮助,删除 `详细帮助` 命令 diff --git a/plugins/withdraw.py b/plugins/withdraw.py index 068cf6ce..795c8cc7 100755 --- a/plugins/withdraw.py +++ b/plugins/withdraw.py @@ -25,6 +25,5 @@ withdraw_msg = on_command("撤回", priority=5, block=True) @withdraw_msg.handle() async def _(bot: Bot, event: GroupMessageEvent, state: T_State): - r = re.search(r"\[CQ:reply,id=(-?\d*)]", event.raw_message) - if r: - await bot.delete_msg(message_id=int(r.group(1))) + if event.reply: + await bot.delete_msg(message_id=event.reply.message_id) diff --git a/plugins/word_bank/__init__.py b/plugins/word_bank/__init__.py index 7494c3e3..fbb865c6 100644 --- a/plugins/word_bank/__init__.py +++ b/plugins/word_bank/__init__.py @@ -1,4 +1,5 @@ from configs.config import Config +from utils.utils import GDict import nonebot Config.add_plugin_config( @@ -10,4 +11,6 @@ Config.add_plugin_config( default_value=5 ) +GDict['run_sql'].append("ALTER TABLE word_bank2 ADD to_me VARCHAR(255);") + nonebot.load_plugins("plugins/word_bank") diff --git a/plugins/word_bank/_data_source.py b/plugins/word_bank/_data_source.py index 4c262dc4..c707ae3d 100644 --- a/plugins/word_bank/_data_source.py +++ b/plugins/word_bank/_data_source.py @@ -84,8 +84,9 @@ async def word_handle(params: str, group_id: Optional[int], type_: str, word_sco if not is_number(index) or int(index) < 0 or int(index) > answer_num: return "指定回答下标id必须为数字且在范围内" index = int(index) - await WordBank.delete_group_problem(problem, group_id, index, word_scope) - return "删除词条成功" + if await WordBank.delete_group_problem(problem, group_id, index, word_scope): + return "删除词条成功" + return "词条不存在" if type_ == "update": replace_str = params[1] await WordBank.update_group_problem(problem, replace_str, group_id, word_scope=word_scope) diff --git a/plugins/word_bank/_model.py b/plugins/word_bank/_model.py index d75ec908..223dd337 100644 --- a/plugins/word_bank/_model.py +++ b/plugins/word_bank/_model.py @@ -39,6 +39,7 @@ class WordBank(db.Model): answer = db.Column(db.String(), nullable=False) # 回答 placeholder = db.Column(db.String()) # 占位符 image_path = db.Column(db.String()) # 使用图片作为问题时图片存储的路径 + to_me = db.Column(db.String()) # 使用图片作为问题时图片存储的路径 create_time = db.Column(db.DateTime(), nullable=False) update_time = db.Column(db.DateTime(), nullable=False) @@ -85,6 +86,7 @@ class WordBank(db.Model): word_type: int, problem: Union[str, Message], answer: Union[str, Message], + to_me_nickname: str = None ): """ 说明: @@ -96,6 +98,7 @@ class WordBank(db.Model): :param word_type: 词条类型, :param problem: 问题 :param answer: 回答 + :param to_me_nickname: at真寻名称 """ # 对图片做额外处理 image_path = None @@ -122,6 +125,7 @@ class WordBank(db.Model): placeholder=",".join(_list), create_time=datetime.now().replace(microsecond=0), update_time=datetime.now().replace(microsecond=0), + to_me=to_me_nickname ) @classmethod @@ -203,6 +207,7 @@ class WordBank(db.Model): return MessageTemplate(temp_answer, Message).format(*seg_list) return answer + @classmethod async def check( cls, @@ -366,25 +371,28 @@ class WordBank(db.Model): :param index: 回答下标 :param word_scope: 词条范围 """ - if index is not None: - if group_id: - query = await cls.query.where( - (cls.group_id == group_id) & (cls.problem == problem) - ).gino.all() + if await cls.exists(None, group_id, problem, None, word_scope): + if index is not None: + if group_id: + query = await cls.query.where( + (cls.group_id == group_id) & (cls.problem == problem) + ).gino.all() + else: + query = await cls.query.where( + (cls.word_scope == 0) & (cls.problem == problem) + ).gino.all() + await query[index].delete() else: - query = await cls.query.where( - (cls.word_scope == 0) & (cls.problem == problem) - ).gino.all() - await query[index].delete() - else: - if group_id: - await WordBank.delete.where( - (cls.group_id == group_id) & (cls.problem == problem) - ).gino.status() - else: - await WordBank.delete.where( - (cls.word_scope == word_scope) & (cls.problem == problem) - ).gino.status() + if group_id: + await WordBank.delete.where( + (cls.group_id == group_id) & (cls.problem == problem) + ).gino.status() + else: + await WordBank.delete.where( + (cls.word_scope == word_scope) & (cls.problem == problem) + ).gino.status() + return True + return False @classmethod async def update_group_problem( diff --git a/plugins/word_bank/_rule.py b/plugins/word_bank/_rule.py index 5714368e..e82dd046 100644 --- a/plugins/word_bank/_rule.py +++ b/plugins/word_bank/_rule.py @@ -4,14 +4,14 @@ from io import BytesIO from services.log import logger from nonebot.typing import T_State -from nonebot.adapters.onebot.v11 import MessageEvent +from nonebot.adapters.onebot.v11 import MessageEvent, Bot from utils.utils import get_message_text, get_message_img, get_message_at from ._model import WordBank from utils.http_utils import AsyncHttpx -async def check(event: MessageEvent, state: T_State) -> bool: +async def check(bot: Bot, event: MessageEvent, state: T_State) -> bool: text = get_message_text(event.message) img = get_message_img(event.message) at = get_message_at(event.message) @@ -30,6 +30,13 @@ async def check(event: MessageEvent, state: T_State) -> bool: elif seg.type == 'text': temp += seg.data["text"] problem = temp + if event.to_me and bot.config.nickname: + if str(event.original_message).startswith("[CQ:at"): + problem = f"[at:{bot.self_id}]" + problem + else: + if problem and bot.config.nickname: + nickname = [nk for nk in bot.config.nickname if str(event.original_message).startswith(nk)] + problem = nickname[0] + problem if nickname else problem if problem and (await WordBank.check(event, problem) is not None): state["problem"] = problem return True diff --git a/plugins/word_bank/word_handle.py b/plugins/word_bank/word_handle.py index cd499712..10e50e83 100644 --- a/plugins/word_bank/word_handle.py +++ b/plugins/word_bank/word_handle.py @@ -139,6 +139,7 @@ async def _( @add_word.got("problem_image", prompt="请发送该回答设置的问题图片") async def _( + bot: Bot, event: MessageEvent, word_scope: Optional[str] = ArgStr("word_scope"), word_type: Optional[str] = ArgStr("word_type"), @@ -152,13 +153,19 @@ async def _( re.compile(problem) except re.error: await add_word.finish(f"添加词条失败,正则表达式 {problem} 非法!") + if str(event.user_id) in bot.config.superusers and isinstance(event, PrivateMessageEvent): + word_scope = "私聊" + nickname = None + if problem and bot.config.nickname: + nickname = [nk for nk in bot.config.nickname if problem.startswith(nk)] await WordBank.add_problem_answer( event.user_id, - event.group_id if isinstance(event, GroupMessageEvent) and (not word_scope or word_scope == '1') else 0, + event.group_id if isinstance(event, GroupMessageEvent) 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, problem or problem_image, answer, + nickname[0] if nickname else None ) except Exception as e: if isinstance(e, FinishedException):