Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复词库问答几个问题 #1012

Merged
merged 15 commits into from
Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugins/quotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
}


quotations = on_regex("^[语录|二次元]$", priority=5, block=True)
quotations = on_regex("^(语录|二次元)$", priority=5, block=True)

url = "https://international.v1.hitokoto.cn/?c=a"

Expand Down
19 changes: 15 additions & 4 deletions plugins/word_bank/_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,16 +234,27 @@ async def check(
query = query.where(cls.word_scope == word_type)
sql_text += f" and word_scope = {word_scope}"
# 完全匹配
if await query.where(cls.problem == problem).gino.first():
if await query.where(
((cls.word_type == 0) | (cls.word_type == 3)) & (cls.problem == problem)
).gino.first():
return query.where(cls.problem == problem)
# 模糊匹配
if await query.where(cls.problem.contains(problem)).gino.first():
if await query.where(
(cls.word_type == 1) & (cls.problem.contains(problem))
).gino.first():
return query.where(cls.problem.contains(problem))
# 正则匹配
if await db.first(
db.text(sql_text + f" and word_type = 2 and word_scope != 999 and '{problem}' ~ problem;")
db.text(
sql_text
+ f" and word_type = 2 and word_scope != 999 and :problem ~ problem;"
),
problem=problem,
):
return sql_text + f" and word_type = 2 and word_scope != 999 and '{problem}' ~ problem;"
return (
sql_text
+ f" and word_type = 2 and word_scope != 999 and '{problem}' ~ problem;"
)
# if await db.first(
# db.text(sql_text + f" and word_type = 1 and word_scope != 999 and '{problem}' ~ problem;")
# ):
Expand Down
27 changes: 17 additions & 10 deletions plugins/word_bank/_rule.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,38 @@
import random
import imagehash
from PIL import Image
from io import BytesIO
from httpx import TimeoutException

from nonebot.typing import T_State
from nonebot.adapters.onebot.v11 import MessageEvent

from configs.path_config import TEMP_PATH
from utils.image_utils import get_img_hash
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) -> bool:
async def check(event: MessageEvent, state: T_State) -> bool:
text = get_message_text(event.message)
img = get_message_img(event.message)
at = get_message_at(event.message)
rand = random.randint(1, 100)
problem = text
if not text and len(img) == 1:
if await AsyncHttpx.download_file(img[0], TEMP_PATH / f"{event.user_id}_{rand}_word_bank_check.jpg"):
problem = str(get_img_hash(TEMP_PATH / f"{event.user_id}_{rand}_word_bank_check.jpg"))
try:
r = await AsyncHttpx.get(img[0])
problem = str(imagehash.average_hash(Image.open(BytesIO(r.content))))
except TimeoutException:
pass
if at:
temp = ''
for seg in event.message:
if seg.type == 'at':
temp += f"[at:{seg.data['qq']}]"
else:
elif isinstance(seg, str):
temp += seg
elif seg.type == 'text':
temp += seg.data["text"]
problem = temp
if problem:
return await WordBank.check(event, problem) is not None
if problem and (await WordBank.check(event, problem) is not None):
state["problem"] = problem
return True
return False
35 changes: 5 additions & 30 deletions plugins/word_bank/message_handle.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import random

from services import logger
from utils.image_utils import get_img_hash
from ._rule import check
from ._model import WordBank
from configs.path_config import DATA_PATH, TEMP_PATH
from configs.path_config import DATA_PATH
from nonebot.adapters.onebot.v11 import GroupMessageEvent, MessageEvent
from utils.utils import get_message_img, get_message_text, get_message_at
from nonebot import on_message
from utils.http_utils import AsyncHttpx
from nonebot.typing import T_State

__zx_plugin_name__ = "词库问答回复操作 [Hidden]"

Expand All @@ -19,33 +15,12 @@


@message_handle.handle()
async def _(event: MessageEvent):
text = get_message_text(event.message)
img = get_message_img(event.message)
at = get_message_at(event.message)
problem = None
if not text and img and len(img) == 1:
rand = random.randint(1, 10000)
if await AsyncHttpx.download_file(img[0], TEMP_PATH / f"{event.user_id}_{rand}_word_bank.jpg"):
problem = str(get_img_hash(TEMP_PATH / f"{event.user_id}_{rand}_word_bank.jpg"))
elif at:
temp = ''
for seg in event.message:
if seg.type == 'at':
temp += f"[at:{seg.data['qq']}]"
else:
temp += seg
problem = temp
elif text:
problem = text
if problem:
async def _(event: MessageEvent, state: T_State):
if problem := state.get("problem"):
if msg := await WordBank.get_answer(event, problem):
await message_handle.send(msg)
logger.info(
f"(USER {event.user_id}, GROUP "
f"{event.group_id if isinstance(event, GroupMessageEvent) else 'private'})"
f" 触发词条 {problem}"
)



)
32 changes: 23 additions & 9 deletions plugins/word_bank/word_handle.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import re
from typing import Tuple, Any, Optional

from nonebot.internal.params import Arg, ArgStr
from nonebot.typing import T_State

from utils.utils import get_message_at, is_number, get_message_img
from nonebot.params import CommandArg, RegexGroup, Command
from nonebot.exception import FinishedException
from services.log import logger
from configs.path_config import DATA_PATH
from utils.message_builder import custom_forward_msg
from ._model import WordBank
from nonebot.adapters.onebot.v11 import Bot, GroupMessageEvent, Message, MessageEvent, PrivateMessageEvent
from nonebot.adapters.onebot.v11 import Bot, GroupMessageEvent, Message, MessageEvent, PrivateMessageEvent, unescape
from nonebot import on_command, on_regex
from configs.config import Config
from ._data_source import delete_word, update_word, show_word
Expand All @@ -23,7 +25,7 @@
更推荐使用id方式删除
问题回答支持的CQ:at, face, image
查看词条命令:群聊时为 群词条+全局词条,私聊时为 私聊词条+全局词条
添加词条正则:添加词条(模糊|正则|图片)?问\s*?(\S*)\s*?答\s?(\S*)
添加词条正则:添加词条(模糊|正则|图片)?问\s*?(\S*\s?\S*)\s*?答\s?(\S*)
指令:
添加词条 ?[模糊|正则|图片]问...答...:添加问答词条,可重复添加相同问题的不同回答
删除词条 [问题/下标] ?[下标]:删除指定词条指定或全部回答
Expand All @@ -47,7 +49,7 @@
usage:
在私聊中超级用户额外设置
指令:
(全局|私聊)?添加词条\s*?(模糊|正则|图片)?问\s*?(\S*)\s*?答\s?(\S*):添加问答词条,可重复添加相同问题的不同回答
(全局|私聊)?添加词条\s*?(模糊|正则|图片)?问\s*?(\S*\s?\S*)\s*?答\s?(\S*):添加问答词条,可重复添加相同问题的不同回答
全局添加词条
私聊添加词条
(私聊情况下)删除词条: 删除私聊词条
Expand All @@ -74,7 +76,7 @@
data_dir.mkdir(parents=True, exist_ok=True)

add_word = on_regex(
r"^(全局|私聊)?添加词条\s*?(模糊|正则|图片)?问\s*?(\S*)\s*?答\s?(\S*)", priority=5, block=True
r"^(全局|私聊)?添加词条\s*?(模糊|正则|图片)?问\s*?(\S*\s?\S*)\s*?答\s?(\S*)", priority=5, block=True
)

delete_word_matcher = on_command("删除词条", aliases={'删除全局词条'}, priority=5, block=True)
Expand Down Expand Up @@ -113,16 +115,21 @@ async def _(
if seg.type == 'text' and '答' in str(seg):
_problem = event.message[:index]
answer = event.message[index:]
answer[0] = str(answer[0])[str(answer).index('答')+1:]
_problem[0] = str(_problem[0])[str(_problem).index('问')+1:]
answer[0] = str(answer[0])[str(answer[0]).index('答')+1:]
_problem[0] = str(_problem[0])[str(_problem[0]).index('问')+1:]
if _problem[-1].type != 'at' or seg.data['text'][:seg.data['text'].index('答')].lstrip():
_problem.append(seg.data['text'][:seg.data['text'].index('答')])
temp = ''
for g in _problem:
if isinstance(g, str) or g.type == 'text':
if isinstance(g, str):
temp += g
elif g.type == 'text':
temp += g.data['text']
elif g.type == 'at':
temp += f"[at:{g.data['qq']}]"
problem = temp
break
problem = unescape(problem)
index = len((word_scope or "") + "添加词条" + (word_type or "") + problem) + 1
event.message[0] = event.message[0].data["text"][index + 1 :].strip()
state["word_scope"] = word_scope
Expand All @@ -141,6 +148,11 @@ async def _(
problem_image: Message = Arg("problem_image"),
):
try:
if word_type == "正则":
try:
re.compile(problem)
except re.error:
await add_word.finish(f"添加词条失败,正则表达式 {problem} 非法!")
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,
Expand All @@ -150,6 +162,8 @@ async def _(
answer,
)
except Exception as e:
if isinstance(e, FinishedException):
await add_word.finish()
logger.error(
f"(USER {event.user_id}, GROUP "
f"{event.group_id if isinstance(event, GroupMessageEvent) else 'private'})"
Expand Down Expand Up @@ -233,7 +247,7 @@ async def _(bot: Bot, event: GroupMessageEvent, arg: Message = CommandArg()):
not is_number(id_)
or int(id_) < 0
or int(id_)
> len(await WordBank.get_group_all_problem(event.group_id))
>= len(await WordBank.get_group_all_problem(event.group_id))
):
await show_word_matcher.finish("id必须为数字且在范围内")
id_ = int(id_)
Expand All @@ -243,7 +257,7 @@ async def _(bot: Bot, event: GroupMessageEvent, arg: Message = CommandArg()):
not is_number(gid)
or int(gid) < 0
or int(gid)
> len(await WordBank.get_problem_by_scope(0))
>= len(await WordBank.get_problem_by_scope(0))
):
await show_word_matcher.finish("gid必须为数字且在范围内")
gid = int(gid)
Expand Down