mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-05 11:49:59 +08:00
fit nonebot.beta2
This commit is contained in:
@@ -4,10 +4,10 @@ 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 utils.utils import is_number, cn2py, get_message_text
|
||||
from nonebot.adapters.onebot.v11 import MessageEvent, GroupMessageEvent, Message
|
||||
from utils.utils import is_number, cn2py
|
||||
from configs.config import Config
|
||||
from pathlib import Path
|
||||
from nonebot.params import CommandArg, Arg
|
||||
import os
|
||||
|
||||
__zx_plugin_name__ = "删除图片 [Admin]"
|
||||
@@ -31,60 +31,47 @@ __plugin_settings__ = {
|
||||
delete_img = on_command("删除图片", priority=5, rule=to_me(), block=True)
|
||||
|
||||
|
||||
_path = Path(IMAGE_PATH) / "image_management"
|
||||
|
||||
|
||||
@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 Config.get_config(
|
||||
"image_management", "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())
|
||||
_path = IMAGE_PATH / "image_management"
|
||||
|
||||
|
||||
@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 Config.get_config("image_management", "IMAGE_DIR_LIST")
|
||||
and is_number(args[1])
|
||||
):
|
||||
async def _(state: T_State, arg: Message = CommandArg()):
|
||||
args = arg.extract_plain_text().strip().split()
|
||||
if args:
|
||||
if args[0] in Config.get_config("image_management", "IMAGE_DIR_LIST"):
|
||||
state["path"] = args[0]
|
||||
if len(args) > 1 and is_number(args[1]):
|
||||
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):
|
||||
img_id = state["id"]
|
||||
path = _path / cn2py(state["path"])
|
||||
async def arg_handle(
|
||||
event: MessageEvent,
|
||||
state: T_State,
|
||||
path: str = Arg("path"),
|
||||
img_id: str = Arg("id"),
|
||||
):
|
||||
if path in ["取消", "算了"] or img_id in ["取消", "算了"]:
|
||||
await delete_img.finish("已取消操作...")
|
||||
if path not in Config.get_config("image_management", "IMAGE_DIR_LIST"):
|
||||
await delete_img.reject_arg("path", "此目录不正确,请重新输入目录!")
|
||||
if not is_number(img_id):
|
||||
await delete_img.reject_arg("id", "id不正确!请重新输入数字...")
|
||||
path = _path / cn2py(path)
|
||||
if not path.exists() and (path.parent.parent / cn2py(state["path"])).exists():
|
||||
path = path.parent.parent / cn2py(state["path"])
|
||||
temp = Path(TEMP_PATH)
|
||||
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 (temp / "delete.jpg").exists():
|
||||
(temp / "delete.jpg").unlink()
|
||||
if (TEMP_PATH / "delete.jpg").exists():
|
||||
(TEMP_PATH / "delete.jpg").unlink()
|
||||
logger.info(f"删除{cn2py(state['path'])}图片 {img_id}.jpg 成功")
|
||||
except Exception as e:
|
||||
logger.warning(f"删除图片 delete.jpg 失败 e{e}")
|
||||
try:
|
||||
os.rename(path / f"{img_id}.jpg", temp / "delete.jpg")
|
||||
os.rename(path / f"{img_id}.jpg", TEMP_PATH / f"{event.user_id}_delete.jpg")
|
||||
logger.info(f"移动 {path}/{img_id}.jpg 移动成功")
|
||||
except Exception as e:
|
||||
logger.warning(f"{path}/{img_id}.jpg --> 移动失败 e:{e}")
|
||||
@@ -100,6 +87,6 @@ async def arg_handle(bot: Bot, event: MessageEvent, state: T_State):
|
||||
f" -> id: {img_id} 删除成功"
|
||||
)
|
||||
await delete_img.finish(
|
||||
f"id: {img_id} 删除成功" + image("delete.jpg", TEMP_PATH), at_sender=True
|
||||
f"id: {img_id} 删除成功" + image(TEMP_PATH / f"{event.user_id}_delete.jpg",), at_sender=True
|
||||
)
|
||||
await delete_img.finish(f"id: {img_id} 删除失败!")
|
||||
|
||||
Reference in New Issue
Block a user