mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-06 04:10:02 +08:00
refactor code
This commit is contained in:
+21
-15
@@ -1,8 +1,8 @@
|
||||
from nonebot.adapters.cqhttp.permission import GROUP
|
||||
from configs.path_config import IMAGE_PATH
|
||||
from utils.img_utils import get_img_hash
|
||||
from utils.image_utils import get_img_hash
|
||||
import random
|
||||
from utils.init_result import image
|
||||
from utils.message_builder import image
|
||||
from nonebot import on_message
|
||||
from utils.utils import get_message_text, get_message_imgs, get_local_proxy
|
||||
from nonebot.typing import T_State
|
||||
@@ -10,6 +10,7 @@ from nonebot.adapters.cqhttp import Bot, GroupMessageEvent
|
||||
import aiohttp
|
||||
import aiofiles
|
||||
from collections import defaultdict
|
||||
from asyncio.exceptions import TimeoutError
|
||||
from configs.config import FUDU_PROBABILITY
|
||||
|
||||
|
||||
@@ -44,8 +45,8 @@ async def _(bot: Bot, event: GroupMessageEvent, state: T_State):
|
||||
if event.is_tome() or state["_prefix"]["raw_command"]:
|
||||
return
|
||||
if get_message_text(event.json()):
|
||||
if get_message_text(event.json()).find('@可爱的小真寻') != -1:
|
||||
await fudu.finish('复制粘贴的虚空艾特?', at_sender=True)
|
||||
if get_message_text(event.json()).find("@可爱的小真寻") != -1:
|
||||
await fudu.finish("复制粘贴的虚空艾特?", at_sender=True)
|
||||
imgs = get_message_imgs(event.json())
|
||||
msg = get_message_text(event.json())
|
||||
if not imgs and not msg:
|
||||
@@ -53,8 +54,8 @@ async def _(bot: Bot, event: GroupMessageEvent, state: T_State):
|
||||
if imgs:
|
||||
img_hash = await get_fudu_img_hash(imgs[0], event.group_id)
|
||||
else:
|
||||
img_hash = ''
|
||||
add_msg = msg + '|-|' + img_hash
|
||||
img_hash = ""
|
||||
add_msg = msg + "|-|" + img_hash
|
||||
if _fudulist.size(event.group_id) == 0:
|
||||
_fudulist.append(event.group_id, add_msg)
|
||||
elif _fudulist.check(event.group_id, add_msg):
|
||||
@@ -67,22 +68,27 @@ async def _(bot: Bot, event: GroupMessageEvent, state: T_State):
|
||||
if random.random() < 0.2:
|
||||
await fudu.finish("打断施法!")
|
||||
if imgs and msg:
|
||||
rst = msg + image(f'compare_{event.group_id}_img.jpg', 'temp')
|
||||
rst = msg + image(f"compare_{event.group_id}_img.jpg", "temp")
|
||||
elif imgs:
|
||||
rst = image(f'compare_{event.group_id}_img.jpg', 'temp')
|
||||
rst = image(f"compare_{event.group_id}_img.jpg", "temp")
|
||||
elif msg:
|
||||
rst = msg
|
||||
else:
|
||||
rst = ''
|
||||
rst = ""
|
||||
if rst:
|
||||
await fudu.send(rst)
|
||||
_fudulist.clear(event.group_id)
|
||||
|
||||
|
||||
async def get_fudu_img_hash(url, group_id):
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get(url, proxy=get_local_proxy(), timeout=5) as response:
|
||||
async with aiofiles.open(IMAGE_PATH + f"temp/compare_{group_id}_img.jpg", 'wb') as f:
|
||||
await f.write(await response.read())
|
||||
img_hash = get_img_hash(IMAGE_PATH + f"temp/compare_{group_id}_img.jpg")
|
||||
return str(img_hash)
|
||||
try:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get(url, proxy=get_local_proxy(), timeout=5) as response:
|
||||
async with aiofiles.open(
|
||||
IMAGE_PATH + f"temp/compare_{group_id}_img.jpg", "wb"
|
||||
) as f:
|
||||
await f.write(await response.read())
|
||||
img_hash = get_img_hash(IMAGE_PATH + f"temp/compare_{group_id}_img.jpg")
|
||||
return str(img_hash)
|
||||
except TimeoutError:
|
||||
return ''
|
||||
|
||||
Reference in New Issue
Block a user