mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-15 01:00:00 +08:00
update v0.0.5.0
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
import nonebot
|
||||
|
||||
nonebot.load_plugins("plugins/image_management")
|
||||
@@ -0,0 +1,97 @@
|
||||
from configs.path_config import IMAGE_PATH, TEMP_PATH
|
||||
from utils.message_builder import image
|
||||
from services.log import logger
|
||||
from nonebot import on_command
|
||||
from nonebot.rule import to_me
|
||||
from nonebot.typing import T_State
|
||||
from nonebot.adapters.cqhttp import Bot, MessageEvent, GroupMessageEvent
|
||||
from configs.config import DELETE_IMG_LEVEL
|
||||
from configs.config import IMAGE_DIR_LIST
|
||||
from utils.utils import is_number, cn2py, get_message_text
|
||||
from pathlib import Path
|
||||
import os
|
||||
|
||||
__zx_plugin_name__ = "删除图片 [Admin]"
|
||||
__plugin_usage__ = """
|
||||
usage:
|
||||
删除图库指定图片
|
||||
指令:
|
||||
删除图片 [图库] [id]
|
||||
查看图库
|
||||
示例:删除图片 美图 666
|
||||
""".strip()
|
||||
__plugin_des__ = "不好看的图片删掉删掉!"
|
||||
__plugin_cmd__ = ["删除图片 [图库] [id]", "查看公开图库"]
|
||||
__plugin_version__ = 0.1
|
||||
__plugin_author__ = "HibiKier"
|
||||
__plugin_settings__ = {
|
||||
"admin_level": DELETE_IMG_LEVEL
|
||||
}
|
||||
|
||||
|
||||
delete_img = on_command("删除图片", priority=5, rule=to_me(), block=True)
|
||||
|
||||
|
||||
@delete_img.args_parser
|
||||
async def parse(bot: Bot, event: MessageEvent, state: T_State):
|
||||
if get_message_text(event.json()) in ["取消", "算了"]:
|
||||
await delete_img.finish("已取消操作..", at_sender=True)
|
||||
if state["_current_key"] in ["path"]:
|
||||
if get_message_text(event.json()) not in IMAGE_DIR_LIST:
|
||||
await delete_img.reject("此目录不正确,请重新输入目录!")
|
||||
state[state["_current_key"]] = get_message_text(event.json())
|
||||
if state["_current_key"] == "id":
|
||||
if not is_number(get_message_text(event.json())):
|
||||
await delete_img.reject("id不正确!请重新输入数字...")
|
||||
state[state["_current_key"]] = get_message_text(event.json())
|
||||
|
||||
|
||||
@delete_img.handle()
|
||||
async def _(bot: Bot, event: MessageEvent, state: T_State):
|
||||
raw_arg = get_message_text(event.json()).strip()
|
||||
if raw_arg:
|
||||
args = raw_arg.split(" ")
|
||||
if args[0] in ["帮助"]:
|
||||
await delete_img.finish(__plugin_usage__)
|
||||
if len(args) >= 2 and args[0] in IMAGE_DIR_LIST and is_number(args[1]):
|
||||
state["path"] = args[0]
|
||||
state["id"] = args[1]
|
||||
|
||||
|
||||
@delete_img.got("path", prompt="请输入要删除的目标图库?")
|
||||
@delete_img.got("id", prompt="请输入要删除的图片id?")
|
||||
async def arg_handle(bot: Bot, event: MessageEvent, state: T_State):
|
||||
path = cn2py(state["path"])
|
||||
img_id = state["id"]
|
||||
# path = IMAGE_PATH + path
|
||||
path = Path(IMAGE_PATH) / path
|
||||
temp = Path(IMAGE_PATH) / "temp"
|
||||
max_id = len(os.listdir(path)) - 1
|
||||
if int(img_id) > max_id or int(img_id) < 0:
|
||||
await delete_img.finish(f"Id超过上下限,上限:{max_id}", at_sender=True)
|
||||
try:
|
||||
if os.path.exists(temp / "delete.jpg"):
|
||||
os.remove(temp / "delete.jpg")
|
||||
logger.info("删除图片 delete.jpg 成功")
|
||||
except Exception as e:
|
||||
logger.warning(f"删除图片 delete.jpg 失败 e{e}")
|
||||
try:
|
||||
os.rename(path / f"{img_id}.jpg", temp / "delete.jpg")
|
||||
logger.info(f"移动 {path}/{img_id}.jpg 移动成功")
|
||||
except Exception as e:
|
||||
logger.warning(f"{path}/{img_id}.jpg --> 移动失败 e:{e}")
|
||||
if not os.path.exists(path / f"{img_id}.jpg"):
|
||||
try:
|
||||
if int(img_id) != max_id:
|
||||
os.rename(path / f"{max_id}.jpg", path / f"{img_id}.jpg")
|
||||
except FileExistsError as e:
|
||||
logger.error(f"{path}/{max_id}.jpg 替换 {path}/{img_id}.jpg 失败 e:{e}")
|
||||
logger.info(f"{path}/{max_id}.jpg 替换 {path}/{img_id}.jpg 成功")
|
||||
logger.info(
|
||||
f"USER {event.user_id} GROUP {event.group_id if isinstance(event, GroupMessageEvent) else 'private'}"
|
||||
f" -> id: {img_id} 删除成功"
|
||||
)
|
||||
await delete_img.finish(
|
||||
f"id: {img_id} 删除成功" + image("delete.jpg", TEMP_PATH), at_sender=True
|
||||
)
|
||||
await delete_img.finish(f"id: {img_id} 删除失败!")
|
||||
@@ -0,0 +1,105 @@
|
||||
import os
|
||||
from services.log import logger
|
||||
from nonebot import on_command
|
||||
from nonebot.rule import to_me
|
||||
from nonebot.typing import T_State
|
||||
from nonebot.adapters.cqhttp import Bot, MessageEvent, GroupMessageEvent
|
||||
from configs.config import IMAGE_DIR_LIST, MOVE_IMG_LEVEL
|
||||
from utils.utils import is_number, cn2py
|
||||
from configs.path_config import IMAGE_PATH
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
__zx_plugin_name__ = "移动图片 [Admin]"
|
||||
__plugin_usage__ = """
|
||||
usage:
|
||||
图库间的图片移动操作
|
||||
指令:
|
||||
移动图片 [源图库] [目标图库] [id]
|
||||
查看图库
|
||||
示例:移动图片 萝莉 美图 234
|
||||
""".strip()
|
||||
__plugin_des__ = "图库间的图片移动操作"
|
||||
__plugin_cmd__ = ["移动图片 [源图库] [目标图库] [id]", "查看公开图库"]
|
||||
__plugin_version__ = 0.1
|
||||
__plugin_author__ = "HibiKier"
|
||||
__plugin_settings__ = {
|
||||
"admin_level": MOVE_IMG_LEVEL
|
||||
}
|
||||
|
||||
|
||||
move_img = on_command("移动图片", priority=5, rule=to_me(), block=True)
|
||||
|
||||
|
||||
@move_img.args_parser
|
||||
async def parse(bot: Bot, event: MessageEvent, state: T_State):
|
||||
if str(event.get_message()) in ["取消", "算了"]:
|
||||
await move_img.finish("已取消操作..", at_sender=True)
|
||||
if state["_current_key"] in ["source_path", "destination_path"]:
|
||||
if str(event.get_message()) not in IMAGE_DIR_LIST:
|
||||
await move_img.reject("此目录不正确,请重新输入目录!")
|
||||
state[state["_current_key"]] = str(event.get_message())
|
||||
if state["_current_key"] == "id":
|
||||
if not is_number(str(event.get_message())):
|
||||
await move_img.reject("id不正确!请重新输入数字...")
|
||||
state[state["_current_key"]] = str(event.get_message())
|
||||
|
||||
|
||||
@move_img.handle()
|
||||
async def _(bot: Bot, event: MessageEvent, state: T_State):
|
||||
raw_arg = str(event.get_message()).strip()
|
||||
if raw_arg:
|
||||
args = raw_arg.split(" ")
|
||||
if args[0] in ["帮助"]:
|
||||
await move_img.finish(__plugin_usage__)
|
||||
if (
|
||||
len(args) >= 3
|
||||
and args[0] in IMAGE_DIR_LIST
|
||||
and args[1] in IMAGE_DIR_LIST
|
||||
and is_number(args[2])
|
||||
):
|
||||
state["source_path"] = args[0]
|
||||
state["destination_path"] = args[1]
|
||||
state["id"] = args[2]
|
||||
else:
|
||||
await move_img.finish("参数错误,请重试", at_sender=True)
|
||||
|
||||
|
||||
@move_img.got("source_path", prompt="要从哪个图库移出?")
|
||||
@move_img.got("destination_path", prompt="要移动到哪个图库?")
|
||||
@move_img.got("id", prompt="要移动的图片id是?")
|
||||
async def _(bot: Bot, event: MessageEvent, state: T_State):
|
||||
img_id = state["id"]
|
||||
source_path = Path(IMAGE_PATH) / cn2py(state["source_path"])
|
||||
destination_path = Path(IMAGE_PATH) / cn2py(state["destination_path"])
|
||||
destination_path.mkdir(parents=True, exist_ok=True)
|
||||
max_id = len(os.listdir(source_path)) - 1
|
||||
des_max_id = len(os.listdir(destination_path))
|
||||
if int(img_id) > max_id or int(img_id) < 0:
|
||||
await move_img.finish(f"Id超过上下限,上限:{max_id}", at_sender=True)
|
||||
try:
|
||||
os.rename(
|
||||
source_path / f"{img_id}.jpg", destination_path / f"{des_max_id}.jpg"
|
||||
)
|
||||
logger.info(
|
||||
f"移动 {source_path}/{img_id}.jpg ---> {destination_path}/{des_max_id} 移动成功"
|
||||
)
|
||||
except Exception as e:
|
||||
logger.warning(
|
||||
f"移动 {source_path}/{img_id}.jpg ---> {destination_path}/{des_max_id} 移动失败 e:{e}"
|
||||
)
|
||||
await move_img.finish(f"移动图片id:{img_id} 失败了...", at_sender=True)
|
||||
if max_id > 0:
|
||||
try:
|
||||
os.rename(source_path / f"{max_id}.jpg", source_path / f"{img_id}.jpg")
|
||||
logger.info(f"{source_path}/{max_id}.jpg 替换 {source_path}/{img_id}.jpg 成功")
|
||||
except Exception as e:
|
||||
logger.warning(
|
||||
f"{source_path}/{max_id}.jpg 替换 {source_path}/{img_id}.jpg 失败 e:{e}"
|
||||
)
|
||||
await move_img.finish(f"替换图片id:{max_id} -> {img_id} 失败了...", at_sender=True)
|
||||
logger.info(
|
||||
f"USER {event.user_id} GROUP {event.group_id if isinstance(event, GroupMessageEvent) else 'private'} ->"
|
||||
f" {source_path} --> {destination_path} (id:{img_id}) 移动图片成功"
|
||||
)
|
||||
await move_img.finish(f"移动图片 id:{img_id} --> id:{des_max_id}成功", at_sender=True)
|
||||
@@ -0,0 +1,127 @@
|
||||
from nonebot import on_command
|
||||
from nonebot.rule import to_me
|
||||
from nonebot.typing import T_State
|
||||
from nonebot.adapters.cqhttp import Bot, MessageEvent, GroupMessageEvent
|
||||
|
||||
from configs.config import IMAGE_DIR_LIST, UPLOAD_IMG_LEVEL
|
||||
from utils.utils import get_message_imgs, get_message_text
|
||||
from .data_source import upload_image_to_local
|
||||
|
||||
|
||||
__zx_plugin_name__ = "上传图片 [Admin]"
|
||||
__plugin_usage__ = """
|
||||
usage:
|
||||
上传图片至指定图库
|
||||
指令:
|
||||
查看图库
|
||||
上传图片 [图库] [图片]
|
||||
连续上传图片 [图库]
|
||||
示例:上传图片 美图 [图片]
|
||||
* 连续上传图片可以通过发送 “stop” 表示停止收集发送的图片,可以开始上传 *
|
||||
""".strip()
|
||||
__plugin_des__ = "指定图库图片上传"
|
||||
__plugin_cmd__ = ["上传图片 [图库] [图片]", "连续上传图片 [图库]", "查看公开图库"]
|
||||
__plugin_version__ = 0.1
|
||||
__plugin_author__ = "HibiKier"
|
||||
__plugin_settings__ = {"admin_level": UPLOAD_IMG_LEVEL}
|
||||
|
||||
upload_img = on_command("上传图片", rule=to_me(), priority=5, block=True)
|
||||
|
||||
continuous_upload_img = on_command("连续上传图片", rule=to_me(), priority=5, block=True)
|
||||
|
||||
show_gallery = on_command("查看公开图库", priority=1, block=True)
|
||||
|
||||
|
||||
@show_gallery.handle()
|
||||
async def _(bot: Bot, event: MessageEvent, state: T_State):
|
||||
x = '公开图库列表:\n'
|
||||
for i, e in enumerate(IMAGE_DIR_LIST):
|
||||
x += f'\t{i+1}.{e}\n'
|
||||
await show_gallery.send(x[:-1])
|
||||
|
||||
|
||||
@upload_img.args_parser
|
||||
async def _(bot: Bot, event: MessageEvent, state: T_State):
|
||||
msg = get_message_text(event.json())
|
||||
if msg in ["取消", "算了"]:
|
||||
await upload_img.finish("已取消操作..", at_sender=True)
|
||||
if state["_current_key"] in ["path"]:
|
||||
if msg not in IMAGE_DIR_LIST:
|
||||
await upload_img.reject("此目录不正确,请重新输入目录!")
|
||||
state["path"] = msg
|
||||
if state["_current_key"] in ["imgs"]:
|
||||
if not get_message_imgs(event.json()):
|
||||
await upload_img.reject("图呢图呢图呢图呢!GKD!")
|
||||
state["imgs"] = get_message_imgs(event.json())
|
||||
|
||||
|
||||
@upload_img.handle()
|
||||
async def _(bot: Bot, event: MessageEvent, state: T_State):
|
||||
raw_arg = get_message_text(event.json())
|
||||
img_list = get_message_imgs(event.json())
|
||||
if raw_arg:
|
||||
if raw_arg in IMAGE_DIR_LIST:
|
||||
state["path"] = raw_arg
|
||||
if img_list:
|
||||
state["imgs"] = img_list
|
||||
|
||||
|
||||
@upload_img.got("path", prompt="要将图片上传至什么图库呢?")
|
||||
async def _(bot: Bot, event: MessageEvent, state: T_State):
|
||||
pass
|
||||
|
||||
|
||||
@upload_img.got("imgs", prompt="图呢图呢图呢图呢!GKD!")
|
||||
async def _(bot: Bot, event: MessageEvent, state: T_State):
|
||||
path = state["path"]
|
||||
img_list = state["imgs"]
|
||||
group_id = 0
|
||||
if isinstance(event, GroupMessageEvent):
|
||||
group_id = event.group_id
|
||||
await upload_img.send(
|
||||
await upload_image_to_local(img_list, path, event.user_id, group_id)
|
||||
)
|
||||
|
||||
|
||||
@continuous_upload_img.args_parser
|
||||
async def _(bot: Bot, event: MessageEvent, state: T_State):
|
||||
if str(event.get_message()) in ["取消", "算了"]:
|
||||
await continuous_upload_img.finish("已取消操作..", at_sender=True)
|
||||
if state["_current_key"] in ["path"]:
|
||||
if str(event.get_message()) not in IMAGE_DIR_LIST:
|
||||
await continuous_upload_img.reject("此目录不正确,请重新输入目录!")
|
||||
state[state["_current_key"]] = str(event.get_message())
|
||||
else:
|
||||
if get_message_text(event.json()) not in ["stop"]:
|
||||
img = get_message_imgs(event.json())
|
||||
if img:
|
||||
state["tmp"].extend(img)
|
||||
await continuous_upload_img.reject("图再来!!")
|
||||
else:
|
||||
state["imgs"] = state["tmp"]
|
||||
|
||||
|
||||
@continuous_upload_img.handle()
|
||||
async def _(bot: Bot, event: MessageEvent, state: T_State):
|
||||
path = get_message_imgs(event.json())
|
||||
if path in IMAGE_DIR_LIST:
|
||||
state["path"] = path
|
||||
await continuous_upload_img.send("图来!!")
|
||||
state["tmp"] = []
|
||||
|
||||
|
||||
@continuous_upload_img.got("path", prompt="要将图片上传至什么图库呢?")
|
||||
async def _(bot: Bot, event: MessageEvent, state: T_State):
|
||||
pass
|
||||
|
||||
|
||||
@continuous_upload_img.got("imgs", prompt="图呢图呢图呢图呢!GKD!")
|
||||
async def _(bot: Bot, event: MessageEvent, state: T_State):
|
||||
path = state["path"]
|
||||
img_list = state["imgs"]
|
||||
group_id = 0
|
||||
if isinstance(event, GroupMessageEvent):
|
||||
group_id = event.group_id
|
||||
await continuous_upload_img.send(
|
||||
await upload_image_to_local(img_list, path, event.user_id, group_id)
|
||||
)
|
||||
@@ -0,0 +1,51 @@
|
||||
from configs.config import NICKNAME
|
||||
from typing import List
|
||||
from configs.path_config import IMAGE_PATH
|
||||
from services.log import logger
|
||||
from utils.utils import cn2py
|
||||
from pathlib import Path
|
||||
import aiofiles
|
||||
import aiohttp
|
||||
import os
|
||||
|
||||
|
||||
async def upload_image_to_local(
|
||||
img_list: List[str], path: str, user_id: int, group_id: int = 0
|
||||
) -> str:
|
||||
_path = path
|
||||
path = Path(IMAGE_PATH) / cn2py(path)
|
||||
path.mkdir(parents=True, exist_ok=True)
|
||||
img_id = len(os.listdir(path))
|
||||
failed_list = []
|
||||
success_id = ""
|
||||
async with aiohttp.ClientSession() as session:
|
||||
for img_url in img_list:
|
||||
try:
|
||||
async with session.get(img_url, timeout=7) as response:
|
||||
if response.status == 200:
|
||||
async with aiofiles.open(path / f"{img_id}.jpg", "wb") as f:
|
||||
await f.write(await response.read())
|
||||
success_id += str(img_id) + ","
|
||||
img_id += 1
|
||||
else:
|
||||
failed_list.append(img_url)
|
||||
logger.warning(f"图片:{img_url} 下载失败....")
|
||||
except TimeoutError as e:
|
||||
logger.warning(f"图片:{img_url} 下载超时....e:{e}")
|
||||
if img_url not in failed_list:
|
||||
failed_list.append(img_url)
|
||||
failed_result = ""
|
||||
for img in failed_list:
|
||||
failed_result += str(img) + "\n"
|
||||
logger.info(
|
||||
f"USER {user_id} GROUP {group_id}"
|
||||
f" 上传图片至 {_path} 共 {len(img_list)} 张,失败 {len(failed_list)} 张,id={success_id[:-1]}"
|
||||
)
|
||||
if failed_list:
|
||||
return (
|
||||
f"这次一共为 {_path}库 添加了 {len(img_list) - len(failed_list)} 张图片\n"
|
||||
f"依次的Id为:{success_id[:-1]}\n上传失败:{failed_result[:-1]}\n{NICKNAME}感谢您对图库的扩充!WW"
|
||||
)
|
||||
else:
|
||||
return f"这次一共为 {_path}库 添加了 {len(img_list)} 张图片\n依次的Id为:" \
|
||||
f"{success_id[:-1]}\n{NICKNAME}感谢您对图库的扩充!WW"
|
||||
Reference in New Issue
Block a user