zhenxun_bot/zhenxun/builtin_plugins/admin/ban/__init__.py

294 lines
9.4 KiB
Python
Raw Normal View History

2024-02-25 03:18:34 +08:00
from arclet.alconna import Args
from nonebot.adapters import Bot
from nonebot.permission import SUPERUSER
from nonebot.plugin import PluginMetadata
from nonebot_plugin_alconna import (
Alconna,
Arparma,
2024-02-25 03:18:34 +08:00
At,
Match,
Option,
on_alconna,
store_true,
)
from nonebot_plugin_session import EventSession
2024-02-25 03:18:34 +08:00
from zhenxun.configs.config import BotConfig, Config
from zhenxun.configs.utils import PluginExtraData, RegisterConfig
2024-02-25 03:18:34 +08:00
from zhenxun.services.log import logger
from zhenxun.utils.enum import PluginType
from zhenxun.utils.message import MessageUtils
from zhenxun.utils.rules import admin_check
2024-02-25 03:18:34 +08:00
from ._data_source import BanManage
base_config = Config.get("ban")
__plugin_meta__ = PluginMetadata(
2024-05-15 23:24:35 +08:00
name="Ban",
2024-02-25 03:18:34 +08:00
description="你被逮捕了!丢进小黑屋!封禁用户以及群组,屏蔽消息",
usage="""
2024-02-27 02:30:01 +08:00
普通管理员
格式:
2024-10-05 19:07:18 +08:00
ban [At用户] ?[-t [时长(分钟)]]
2024-02-27 02:30:01 +08:00
示例:
2024-08-04 18:49:39 +08:00
ban @用户 : 永久拉黑用户
ban @用户 -t 100 : 拉黑用户100分钟
unban @用户 : 从小黑屋中拉出来
2024-05-15 23:24:35 +08:00
""".strip(),
extra=PluginExtraData(
author="HibiKier",
version="0.1",
plugin_type=PluginType.SUPER_AND_ADMIN,
superuser_help="""
超级管理员额外命令
2024-02-27 02:30:01 +08:00
格式:
2024-10-05 19:07:18 +08:00
ban [At用户/用户Id] ?[-t [时长]]
unban --id [idx] : 通过id来进行unban操作
2024-02-27 02:30:01 +08:00
ban列表: 获取所有Ban数据
2024-08-04 18:49:39 +08:00
2024-02-27 02:30:01 +08:00
群组ban列表: 获取群组Ban数据
用户ban列表: 获取用户Ban数据
2024-08-04 18:49:39 +08:00
ban列表 -u [用户Id]: 查找指定用户ban数据
ban列表 -g [群组Id]: 查找指定群组ban数据
示例:
ban列表 -u 123456789 : 查找用户123456789的ban数据
ban列表 -g 123456789 : 查找群组123456789的ban数据
2024-02-27 02:30:01 +08:00
私聊下:
示例:
2024-08-04 18:49:39 +08:00
ban 123456789 : 永久拉黑用户123456789
ban 123456789 -t 100 : 拉黑用户123456789 100分钟
2024-02-27 02:30:01 +08:00
2024-08-04 18:49:39 +08:00
ban -g 999999 : 拉黑群组为999999的群组
ban -g 999999 -t 100 : 拉黑群组为999999的群组 100分钟
2024-02-27 02:30:01 +08:00
unban 123456789 : 从小黑屋中拉出来
unban -g 999999 : 将群组9999999从小黑屋中拉出来
2024-05-15 23:24:35 +08:00
""",
2024-02-25 03:18:34 +08:00
admin_level=base_config.get("BAN_LEVEL", 5),
configs=[
RegisterConfig(
key="BAN_LEVEL",
value=5,
help="ban/unban所需要的管理员权限等级",
default_value=5,
type=int,
)
],
).to_dict(),
2024-02-25 03:18:34 +08:00
)
2024-02-27 02:30:01 +08:00
_ban_matcher = on_alconna(
Alconna(
"ban",
2024-08-04 18:49:39 +08:00
Args["user?", [str, At]],
2024-02-27 02:30:01 +08:00
Option("-g|--group", Args["group_id", str]),
2024-08-04 18:49:39 +08:00
Option("-t|--time", Args["duration", int]),
2024-02-27 02:30:01 +08:00
),
rule=admin_check("ban", "BAN_LEVEL"),
priority=5,
block=True,
)
_unban_matcher = on_alconna(
2024-02-25 03:18:34 +08:00
Alconna(
2024-02-27 02:30:01 +08:00
"unban",
Args["user?", [str, At]],
Option("-g|--group", Args["group_id", str]),
2024-10-05 19:07:18 +08:00
Option("--id", Args["idx", int]),
2024-02-25 03:18:34 +08:00
),
rule=admin_check("ban", "BAN_LEVEL"),
priority=5,
block=True,
)
_status_matcher = on_alconna(
Alconna(
2024-08-04 18:49:39 +08:00
"ban列表",
Option("-u", Args["user_id", str], help_text="查找用户"),
Option("-g", Args["group_id", str], help_text="查找群组"),
Option("--user", action=store_true, help_text="过滤用户"),
Option("--group", action=store_true, help_text="过滤群组"),
2024-02-25 03:18:34 +08:00
),
permission=SUPERUSER,
priority=1,
block=True,
)
2024-02-27 02:30:01 +08:00
_status_matcher.shortcut(
"用户ban列表",
2024-08-04 18:49:39 +08:00
command="ban列表",
2024-02-27 02:30:01 +08:00
arguments=["--user"],
prefix=True,
)
_status_matcher.shortcut(
"群组ban列表",
2024-08-04 18:49:39 +08:00
command="ban列表",
2024-02-27 02:30:01 +08:00
arguments=["--group"],
prefix=True,
)
2024-02-25 03:18:34 +08:00
@_status_matcher.handle()
async def _(
arparma: Arparma,
user_id: Match[str],
group_id: Match[str],
):
2024-02-27 02:30:01 +08:00
filter_type = None
if arparma.find("user"):
filter_type = "user"
if arparma.find("group"):
filter_type = "group"
2024-02-25 03:18:34 +08:00
_user_id = user_id.result if user_id.available else None
_group_id = group_id.result if group_id.available else None
2024-08-04 18:49:39 +08:00
if image := await BanManage.build_ban_image(filter_type, _user_id, _group_id):
2024-08-10 02:25:04 +08:00
await MessageUtils.build_message(image).finish(reply_to=True)
2024-02-25 03:18:34 +08:00
else:
2024-08-10 02:25:04 +08:00
await MessageUtils.build_message("数据为空捏...").finish(reply_to=True)
2024-02-25 03:18:34 +08:00
2024-02-27 02:30:01 +08:00
@_ban_matcher.handle()
2024-02-25 03:18:34 +08:00
async def _(
bot: Bot,
session: EventSession,
arparma: Arparma,
user: Match[str | At],
duration: Match[int],
group_id: Match[str],
):
user_id = ""
if not session.id1:
await MessageUtils.build_message("用户id为空...").finish(reply_to=True)
2024-02-25 03:18:34 +08:00
if user.available:
if isinstance(user.result, At):
user_id = user.result.target
else:
2024-08-04 18:49:39 +08:00
if session.id1 not in bot.config.superusers:
2024-08-10 02:25:04 +08:00
await MessageUtils.build_message("权限不足捏...").finish(reply_to=True)
2024-02-25 03:18:34 +08:00
user_id = user.result
_duration = duration.result * 60 if duration.available else -1
_duration_text = f"{duration.result} 分钟" if duration.available else " 到世界湮灭"
2024-08-04 18:49:39 +08:00
if (gid := session.id3 or session.id2) and not group_id.available:
if not user_id or (
user_id == bot.self_id and session.id1 not in bot.config.superusers
):
_duration = 0.5
await MessageUtils.build_message("倒反天罡,小小管理速速退下!").send()
await BanManage.ban(session.id1, gid, 30, session, True)
_duration_text = "半 分钟"
logger.info(
f"尝试ban {BotConfig.self_nickname} 反被拿下",
arparma.header_result,
session=session,
)
await MessageUtils.build_message(
[
"",
At(flag="user", target=session.id1),
" 狠狠惩戒了一番,一脚踢进了小黑屋!"
f" 在里面乖乖呆 {_duration_text}吧!",
]
).finish(reply_to=True)
2024-02-25 03:18:34 +08:00
await BanManage.ban(
user_id, gid, _duration, session, session.id1 in bot.config.superusers
)
logger.info(
"管理员Ban",
2024-02-25 03:18:34 +08:00
arparma.header_result,
session=session,
target=f"{gid}:{user_id}",
)
2024-08-10 02:25:04 +08:00
await MessageUtils.build_message(
2024-02-25 03:18:34 +08:00
[
2024-08-10 02:25:04 +08:00
"",
(
At(flag="user", target=user_id)
if isinstance(user.result, At)
else user_id
), # type: ignore
" 狠狠惩戒了一番,一脚踢进了小黑屋!",
f" 在里面乖乖呆 {_duration_text} 吧!",
2024-02-25 03:18:34 +08:00
]
2024-08-10 02:25:04 +08:00
).finish(reply_to=True)
2024-02-25 03:18:34 +08:00
elif session.id1 in bot.config.superusers:
_group_id = group_id.result if group_id.available else None
await BanManage.ban(user_id, _group_id, _duration, session, True)
logger.info(
"超级用户Ban",
2024-02-25 03:18:34 +08:00
arparma.header_result,
session=session,
target=f"{_group_id}:{user_id}",
)
at_msg = user_id or f"群组:{_group_id}"
2024-08-10 02:25:04 +08:00
await MessageUtils.build_message(
f"{at_msg} 狠狠惩戒了一番,一脚踢进了小黑屋!"
).finish(reply_to=True)
2024-02-25 03:18:34 +08:00
2024-02-27 02:30:01 +08:00
@_unban_matcher.handle()
2024-02-25 03:18:34 +08:00
async def _(
bot: Bot,
session: EventSession,
arparma: Arparma,
user: Match[str | At],
group_id: Match[str],
2024-10-05 19:07:18 +08:00
idx: Match[int],
2024-02-25 03:18:34 +08:00
):
user_id = ""
2024-10-05 19:07:18 +08:00
_idx = idx.result if idx.available else None
2024-02-25 03:18:34 +08:00
if user.available:
if isinstance(user.result, At):
user_id = user.result.target
else:
2024-08-04 18:49:39 +08:00
if session.id1 not in bot.config.superusers:
2024-08-10 02:25:04 +08:00
await MessageUtils.build_message("权限不足捏...").finish(reply_to=True)
2024-02-25 03:18:34 +08:00
user_id = user.result
if gid := session.id3 or session.id2:
if group_id.available:
gid = group_id.result
2024-10-05 19:07:18 +08:00
is_unban, result = await BanManage.unban(
user_id, gid, session, _idx, session.id1 in bot.config.superusers
)
if not is_unban:
await MessageUtils.build_message(result).finish(reply_to=True)
logger.info(
"管理员UnBan",
arparma.header_result,
session=session,
target=f"{gid}:{result}",
)
await MessageUtils.build_message(
[
"",
(
At(flag="user", target=user_id)
if isinstance(user.result, At)
else result
), # type: ignore
" 从黑屋中拉了出来并急救了一下!",
]
).finish(reply_to=True)
2024-02-25 03:18:34 +08:00
elif session.id1 in bot.config.superusers:
_group_id = group_id.result if group_id.available else None
2024-10-05 19:07:18 +08:00
is_unban, result = await BanManage.unban(
user_id, _group_id, session, _idx, True
)
if not is_unban:
await MessageUtils.build_message(result).finish(reply_to=True)
logger.info(
"超级用户UnBan",
arparma.header_result,
session=session,
target=f"{_group_id}:{user_id}",
)
at_msg = user_id or f"群组:{result}"
await MessageUtils.build_message(
f"{at_msg} 从黑屋中拉了出来并急救了一下!"
).finish(reply_to=True)