mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-04 03:10:01 +08:00
update 0.0.6.2
This commit is contained in:
+126
@@ -0,0 +1,126 @@
|
||||
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 Config
|
||||
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": Config.get_config("image_management", "DELETE_IMAGE_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(Config.get_config("image_management", "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 Config.get_config("image_management", "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 Config.get_config("image_management", "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 Config.get_config("image_management", "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 Config.get_config("image_management", "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)
|
||||
)
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
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
|
||||
from utils.http_utils import AsyncHttpx
|
||||
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 = ""
|
||||
for img_url in img_list:
|
||||
if await AsyncHttpx.download_file(img_url, path / f"{img_id}.jpg"):
|
||||
success_id += str(img_id) + ","
|
||||
img_id += 1
|
||||
else:
|
||||
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