-
Notifications
You must be signed in to change notification settings - Fork 607
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1012 from LambdaYH/patch-4
修复词库问答几个问题
- Loading branch information
Showing
5 changed files
with
61 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters