zhenxun_bot/plugins/upload_img/__init__.py

104 lines
3.9 KiB
Python
Raw Normal View History

2021-05-20 19:25:51 +08:00
from nonebot import on_command
from nonebot.rule import to_me
from nonebot.typing import T_State
2021-07-30 21:21:51 +08:00
from nonebot.adapters.cqhttp import Bot, MessageEvent, GroupMessageEvent
2021-08-19 18:43:24 +08:00
from configs.config import IMAGE_DIR_LIST
2021-06-30 19:50:55 +08:00
from utils.utils import get_message_imgs, get_message_text
2021-08-17 23:17:08 +08:00
from .data_source import upload_image_to_local
2021-05-20 19:25:51 +08:00
2021-07-30 21:21:51 +08:00
__plugin_name__ = "上传图片"
__plugin_usage__ = (
"上传图片帮助:\n\t"
"1.查看列表 --> 指令: 上传图片 列表/目录\n\t"
"2.上传图片 [序号] [图片], 即在相应目录下添加图片\n\t\t示例: 上传图片 1 [图片]"
)
2021-05-20 19:25:51 +08:00
upload_img = on_command("上传图片", rule=to_me(), priority=5, block=True)
2021-08-17 23:17:08 +08:00
continuous_upload_img = on_command('连续上传图片', rule=to_me(), priority=5, block=True)
2021-05-20 19:25:51 +08:00
@upload_img.args_parser
2021-08-17 23:17:08 +08:00
async def _(bot: Bot, event: MessageEvent, state: T_State):
msg = get_message_text(event.json())
if msg in ["取消", "算了"]:
2021-05-20 19:25:51 +08:00
await upload_img.finish("已取消操作..", at_sender=True)
2021-07-30 21:21:51 +08:00
if state["_current_key"] in ["path"]:
2021-08-17 23:17:08 +08:00
if msg not in IMAGE_DIR_LIST:
2021-05-20 19:25:51 +08:00
await upload_img.reject("此目录不正确,请重新输入目录!")
2021-08-17 23:17:08 +08:00
state['path'] = msg
2021-07-30 21:21:51 +08:00
if state["_current_key"] in ["imgs"]:
2021-05-20 19:25:51 +08:00
if not get_message_imgs(event.json()):
await upload_img.reject("图呢图呢图呢图呢GKD")
2021-08-17 23:17:08 +08:00
state['imgs'] = get_message_imgs(event.json())
2021-05-20 19:25:51 +08:00
@upload_img.handle()
2021-07-30 21:21:51 +08:00
async def _(bot: Bot, event: MessageEvent, state: T_State):
2021-05-20 19:25:51 +08:00
raw_arg = get_message_text(event.json())
img_list = get_message_imgs(event.json())
if raw_arg:
2021-08-17 23:17:08 +08:00
if raw_arg in IMAGE_DIR_LIST:
state["path"] = raw_arg
2021-05-20 19:25:51 +08:00
if img_list:
2021-07-30 21:21:51 +08:00
state["imgs"] = img_list
2021-05-20 19:25:51 +08:00
2021-08-17 23:17:08 +08:00
@upload_img.got('path', prompt='要将图片上传至什么图库呢?')
async def _(bot: Bot, event: MessageEvent, state: T_State):
pass
2021-05-20 19:25:51 +08:00
@upload_img.got("imgs", prompt="图呢图呢图呢图呢GKD")
2021-07-30 21:21:51 +08:00
async def _(bot: Bot, event: MessageEvent, state: T_State):
2021-08-17 23:17:08 +08:00
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())
2021-05-20 19:25:51 +08:00
else:
2021-08-17 23:17:08 +08:00
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))