zhenxun_bot/plugins/send_img/__init__.py

136 lines
4.3 KiB
Python
Raw Normal View History

2021-06-30 19:50:55 +08:00
from nonebot import on_command, on_keyword, on_regex
2021-05-20 19:23:32 +08:00
from configs.path_config import IMAGE_PATH
2021-07-30 21:21:51 +08:00
from utils.message_builder import image
2021-06-30 19:50:55 +08:00
from utils.utils import get_message_text, is_number
2021-05-20 19:23:32 +08:00
import os
import random
from services.log import logger
from nonebot.typing import T_State
2021-10-03 14:24:07 +08:00
from nonebot.adapters.cqhttp import Bot, MessageEvent, GroupMessageEvent
2021-06-30 19:50:55 +08:00
from utils.utils import FreqLimiter, cn2py
2021-05-20 19:23:32 +08:00
from configs.config import IMAGE_DIR_LIST
2021-10-03 14:24:07 +08:00
from utils.manager import group_manager
2021-05-20 19:23:32 +08:00
2021-06-30 19:50:55 +08:00
try:
import ujson as json
except ModuleNotFoundError:
import json
2021-05-20 19:23:32 +08:00
2021-08-18 09:56:53 +08:00
if "色图" in IMAGE_DIR_LIST:
IMAGE_DIR_LIST.remove("色图")
2021-08-17 23:49:07 +08:00
2021-10-03 14:24:07 +08:00
__zx_plugin_name__ = "发送本地图库图片"
__plugin_usage__ = f"""
usage
发送指定图库下的随机或指定id图片
指令
{IMAGE_DIR_LIST} ?[id]
示例美图
示例: 萝莉 2
""".strip()
__plugin_des__ = "让看看我的私藏,指[图片]"
__plugin_cmd__ = IMAGE_DIR_LIST
__plugin_type__ = ("来点好康的",)
__plugin_version__ = 0.1
__plugin_author__ = "HibiKier"
__plugin_settings__ = {
"level": 5,
"default_status": True,
"limit_superuser": False,
"cmd": ["发送图片"] + IMAGE_DIR_LIST,
}
__plugin_task__ = {"pa": "丢人爬"}
_flmt = FreqLimiter(1)
2021-05-20 19:23:32 +08:00
cmd = set(IMAGE_DIR_LIST)
# print(cmd)
send_img = on_command("img", aliases=cmd, priority=5, block=True)
2021-08-18 09:56:53 +08:00
pa = on_keyword({"丢人爬", "爪巴"}, priority=5, block=True)
pa_reg = on_regex("^爬$", priority=5, block=True)
2021-06-30 19:50:55 +08:00
2021-07-30 21:21:51 +08:00
search_url = "https://api.fantasyzone.cc/tu/search.php"
2021-05-20 19:23:32 +08:00
@send_img.handle()
2021-06-30 19:50:55 +08:00
async def _(bot: Bot, event: MessageEvent, state: T_State):
img_id = get_message_text(event.json())
2021-07-30 21:21:51 +08:00
path = cn2py(state["_prefix"]["raw_command"]) + "/"
2021-08-19 19:12:33 +08:00
if state["_prefix"]["raw_command"] in IMAGE_DIR_LIST:
2021-07-30 21:21:51 +08:00
if not os.path.exists(f"{IMAGE_PATH}/{path}/"):
os.mkdir(f"{IMAGE_PATH}/{path}/")
2021-05-20 19:23:32 +08:00
length = len(os.listdir(IMAGE_PATH + path)) - 1
2021-06-23 15:57:03 +08:00
if length < 1:
2021-07-30 21:21:51 +08:00
await send_img.finish("该图库中没有图片噢")
logger.warning(f"图库 {path} 为空,调用取消!")
2021-05-20 20:37:26 +08:00
return
2021-05-20 19:23:32 +08:00
index = img_id if img_id else str(random.randint(0, length))
if not is_number(index):
await send_img.finish("id错误")
if int(index) > length or int(index) < 0:
await send_img.finish(f"超过当前上下限!({length - 1})")
2021-07-30 21:21:51 +08:00
result = image(f"{index}.jpg", path)
2021-05-20 19:23:32 +08:00
if result:
logger.info(
2021-10-03 14:24:07 +08:00
f"(USER {event.user_id}, GROUP "
f"{event.group_id if isinstance(event, GroupMessageEvent) else 'private'}) 发送{path}:"
2021-07-30 21:21:51 +08:00
+ result
)
2021-05-20 19:23:32 +08:00
await send_img.finish(f"id{index}" + result)
else:
logger.info(
2021-10-03 14:24:07 +08:00
f"(USER {event.user_id}, GROUP "
f"{event.group_id if isinstance(event, GroupMessageEvent) else 'private'}) 发送 {path} 失败"
2021-07-30 21:21:51 +08:00
)
2021-05-20 19:23:32 +08:00
await send_img.finish(f"不想给你看Ov|")
@pa.handle()
2021-06-30 19:50:55 +08:00
async def _(bot: Bot, event: MessageEvent, state: T_State):
2021-10-03 14:24:07 +08:00
if (
isinstance(event, GroupMessageEvent)
and not await group_manager.check_group_task_status(event.group_id, "pa")
or get_message_text(event.json()).startswith("开启")
or get_message_text(event.json()).startswith("关闭")
2021-08-18 09:56:53 +08:00
):
return
msg = get_message_text(event.json())
if not msg or str(event.get_message()[:2]) in ["开启", "关闭"]:
return
if _flmt.check(event.user_id):
_flmt.start_cd(event.user_id)
await pa.finish(image(random.choice(os.listdir(IMAGE_PATH + "pa")), "pa"))
@pa_reg.handle()
async def _(bot: Bot, event: MessageEvent, state: T_State):
2021-10-03 14:24:07 +08:00
if (
(
isinstance(event, GroupMessageEvent)
and not await group_manager.check_group_task_status(event.group_id, "pa")
or get_message_text(event.json()).startswith("开启")
or get_message_text(event.json()).startswith("关闭")
)
2021-08-18 09:56:53 +08:00
):
return
if _flmt.check(event.user_id):
_flmt.start_cd(event.user_id)
await pa.finish(image(random.choice(os.listdir(IMAGE_PATH + "pa")), "pa"))
2021-06-30 19:50:55 +08:00
num_key = {
2021-07-30 21:21:51 +08:00
"": 1,
"": 2,
"": 2,
"": 2,
"": 3,
"": 4,
"": 5,
"": 6,
"": 7,
"": 8,
"": 9,
2021-06-30 19:50:55 +08:00
}