mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2025-12-14 21:52:56 +08:00
refactor🎨: 优化图库
This commit is contained in:
parent
48b720b00a
commit
38abb74c4c
@ -64,7 +64,7 @@
|
||||
* 通过Config配置项将所有插件配置统计保存至config.yaml,利于统一用户修改
|
||||
* 方便增删插件,原生nonebot2 matcher,不需要额外修改,仅仅通过简单的配置属性就可以生成`帮助图片`和`帮助信息`
|
||||
* 提供了cd,阻塞,每日次数等限制,仅仅通过简单的属性就可以生成一个限制,例如:`__plugin_cd_limit__`
|
||||
* __..... 更多详细请通过`传送门`查看文档!__
|
||||
* **..... 更多详细请通过`传送门`查看文档!**
|
||||
|
||||
## 功能列表
|
||||
|
||||
@ -331,6 +331,10 @@ PS: **ARM平台** 请使用全量版 同时 **如果你的机器 RAM < 1G 可能
|
||||
|
||||
## 更新
|
||||
|
||||
### 2023/8/7
|
||||
|
||||
* 添加 本地图库插件 防吞图特性 [@pull/1468](https://github.com/HibiKier/zhenxun_bot/pull/1468)
|
||||
|
||||
### 2023/5/28
|
||||
|
||||
* 修复群聊数据无法初始化
|
||||
|
||||
@ -51,8 +51,8 @@ cx = on_message(priority=9999, block=False, rule=lambda: False)
|
||||
# 早上好
|
||||
@scheduler.scheduled_job(
|
||||
"cron",
|
||||
hour=20,
|
||||
minute=44,
|
||||
hour=6,
|
||||
minute=1,
|
||||
)
|
||||
async def _():
|
||||
img = image(IMAGE_PATH / "zhenxun" / "zao.jpg")
|
||||
|
||||
@ -2,7 +2,7 @@ import time
|
||||
from typing import Any, Tuple
|
||||
|
||||
from nonebot import on_regex
|
||||
from nonebot.adapters.onebot.v11 import Bot, MessageEvent
|
||||
from nonebot.adapters.onebot.v11 import MessageEvent
|
||||
from nonebot.params import RegexGroup
|
||||
|
||||
from configs.config import Config
|
||||
|
||||
@ -1,18 +1,21 @@
|
||||
import os
|
||||
import random
|
||||
from typing import List
|
||||
|
||||
from nonebot import on_message, on_regex
|
||||
from nonebot.adapters.onebot.v11 import GroupMessageEvent, MessageEvent
|
||||
from nonebot.adapters.onebot.v11 import Message, MessageEvent
|
||||
from nonebot.params import CommandArg
|
||||
|
||||
from configs.config import Config
|
||||
from configs.path_config import IMAGE_PATH
|
||||
from services.log import logger
|
||||
from utils.depends import GetConfig
|
||||
from utils.manager import withdraw_message_manager
|
||||
from utils.message_builder import image
|
||||
from utils.utils import FreqLimiter, cn2py, get_message_text, is_number
|
||||
|
||||
from .rule import rule
|
||||
from .anti import pix_random_change_file
|
||||
from .rule import rule
|
||||
|
||||
__zx_plugin_name__ = "本地图库"
|
||||
__plugin_usage__ = f"""
|
||||
@ -51,9 +54,11 @@ _path = IMAGE_PATH / "image_management"
|
||||
|
||||
|
||||
@send_img.handle()
|
||||
async def _(event: MessageEvent):
|
||||
image_dir_list = Config.get_config("image_management", "IMAGE_DIR_LIST") or []
|
||||
msg = get_message_text(event.json()).split()
|
||||
async def _(
|
||||
event: MessageEvent,
|
||||
image_dir_list: List[str] = GetConfig("image_management", "IMAGE_DIR_LIST", []),
|
||||
):
|
||||
msg = get_message_text(event.message).split()
|
||||
gallery = msg[0]
|
||||
if gallery not in image_dir_list:
|
||||
return
|
||||
@ -67,17 +72,15 @@ async def _(event: MessageEvent):
|
||||
else:
|
||||
path.mkdir(parents=True, exist_ok=True)
|
||||
length = len(os.listdir(path))
|
||||
if length == 0:
|
||||
if not length:
|
||||
logger.warning(f"图库 {cn2py(gallery)} 为空,调用取消!")
|
||||
await send_img.finish("该图库中没有图片噢")
|
||||
await send_img.finish("该图库中没有图片噢...")
|
||||
index = img_id if img_id else str(random.randint(0, length - 1))
|
||||
if not is_number(index):
|
||||
return
|
||||
if int(index) > length - 1 or int(index) < 0:
|
||||
await send_img.finish(f"超过当前上下限!({length - 1})")
|
||||
abs_path = os.path.join(os.getcwd(), path / f"{index}.jpg")
|
||||
pix_random_change_file(abs_path)
|
||||
result = image(path / f"{index}.jpg")
|
||||
await send_img.finish(f"超过当前图库的上下限了哦!({length - 1})")
|
||||
result = image(pix_random_change_file(path / f"{index}.jpg"))
|
||||
if result:
|
||||
logger.info(
|
||||
f"发送{cn2py(gallery)}:" + str(path / f"{index}.jpg"),
|
||||
@ -97,7 +100,7 @@ async def _(event: MessageEvent):
|
||||
)
|
||||
else:
|
||||
logger.info(
|
||||
f"发送 {cn2py(gallery)} 失败",
|
||||
f"发送 {cn2py(gallery)} 不存在",
|
||||
"发送图片",
|
||||
event.user_id,
|
||||
getattr(event, "group_id", None),
|
||||
@ -109,6 +112,9 @@ async def _(event: MessageEvent):
|
||||
async def _(event: MessageEvent):
|
||||
if _flmt.check(event.user_id):
|
||||
_flmt.start_cd(event.user_id)
|
||||
path = IMAGE_PATH / "pa"
|
||||
if not path.exists() or not os.listdir(IMAGE_PATH / "pa"):
|
||||
await pa_reg.finish("该图库中没有图片噢...")
|
||||
await pa_reg.finish(
|
||||
image(IMAGE_PATH / "pa" / random.choice(os.listdir(IMAGE_PATH / "pa")))
|
||||
)
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
import cv2
|
||||
import random
|
||||
import warnings
|
||||
from pathlib import Path
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
from PIL import Image
|
||||
|
||||
@ -8,16 +10,17 @@ from PIL import Image
|
||||
def pix_random_change(img):
|
||||
# Image转cv2
|
||||
img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
|
||||
img[0, 0, 0] = random.randint(0, 0xfffffff)
|
||||
img[0, 0, 0] = random.randint(0, 0xFFFFFFF)
|
||||
# cv2转Image
|
||||
img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
|
||||
return img
|
||||
|
||||
|
||||
def pix_random_change_file(path: str):
|
||||
def pix_random_change_file(path: Path):
|
||||
# 注意:cv2.imread()不支持路径中文
|
||||
str_path = str(path.absolute())
|
||||
warnings.filterwarnings("ignore", category=Warning)
|
||||
img = cv2.imread(path)
|
||||
img[0, 0, 0] = random.randint(0, 0xfffffff)
|
||||
cv2.imwrite(path, img)
|
||||
|
||||
img = cv2.imread(str_path)
|
||||
img[0, 0, 0] = random.randint(0, 0xFFFFFFF)
|
||||
cv2.imwrite(str_path, img)
|
||||
return str_path
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
from typing import Callable, List, Optional, Tuple, Union
|
||||
from typing import Any, Callable, List, Optional, Tuple, Union
|
||||
|
||||
from nonebot.adapters.onebot.v11 import GroupMessageEvent, MessageEvent
|
||||
from nonebot.internal.matcher import Matcher
|
||||
@ -87,7 +87,7 @@ def CostGold(gold: int):
|
||||
def GetConfig(
|
||||
module: Optional[str] = None,
|
||||
config: str = "",
|
||||
default_value: Optional[str] = None,
|
||||
default_value: Any = None,
|
||||
prompt: Optional[str] = None,
|
||||
):
|
||||
"""
|
||||
|
||||
Loading…
Reference in New Issue
Block a user