zhenxun_bot/zhenxun/builtin_plugins/hooks/auth/auth_ban.py

157 lines
4.1 KiB
Python
Raw Normal View History

2025-04-08 17:11:44 +08:00
from nonebot.adapters import Bot
from nonebot.matcher import Matcher
from nonebot_plugin_alconna import At
from nonebot_plugin_uninfo import Uninfo
from zhenxun.configs.config import Config
from zhenxun.models.ban_console import BanConsole
from zhenxun.models.plugin_info import PluginInfo
2025-07-10 17:10:07 +08:00
from zhenxun.services.data_access import DataAccess
from zhenxun.utils.enum import PluginType
2025-04-08 17:11:44 +08:00
from zhenxun.utils.utils import EntityIDs, get_entity_ids
from .exception import SkipPluginException
2025-04-11 16:46:24 +08:00
from .utils import freq, send_message
2025-04-08 17:11:44 +08:00
Config.add_plugin_config(
"hook",
"BAN_RESULT",
"才不会给你发消息.",
help="对被ban用户发送的消息",
)
async def is_ban(user_id: str | None, group_id: str | None) -> int:
2025-05-11 22:52:13 +08:00
if not user_id and not group_id:
return 0
2025-07-10 17:10:07 +08:00
ban_dao = DataAccess(BanConsole)
# 分别获取用户在群组中的ban记录和全局ban记录
group_user = None
user = None
if user_id and group_id:
group_user = await ban_dao.safe_get_or_none(user_id=user_id, group_id=group_id)
if user_id:
user = await ban_dao.safe_get_or_none(user_id=user_id, group_id="")
2025-05-11 22:52:13 +08:00
results = []
if group_user:
results.append(group_user)
2025-05-11 22:52:13 +08:00
if user:
results.append(user)
2025-04-08 17:11:44 +08:00
if not results:
return 0
for result in results:
2025-04-16 10:52:55 +08:00
if result.duration > 0 or result.duration == -1:
2025-04-08 17:11:44 +08:00
return await BanConsole.check_ban_time(user_id, group_id)
return 0
def check_plugin_type(matcher: Matcher) -> bool:
"""判断插件类型是否是隐藏插件
参数:
matcher: Matcher
返回:
bool: 是否为隐藏插件
"""
if plugin := matcher.plugin:
if metadata := plugin.metadata:
extra = metadata.extra
if extra.get("plugin_type") in [PluginType.HIDDEN]:
return False
return True
def format_time(time: float) -> str:
"""格式化时间
参数:
time: ban时长
返回:
str: 格式化时间文本
"""
if time == -1:
return ""
time = abs(int(time))
if time < 60:
time_str = f"{time!s}"
else:
minute = int(time / 60)
if minute > 60:
hours = minute // 60
minute %= 60
time_str = f"{hours} 小时 {minute}分钟"
else:
time_str = f"{minute} 分钟"
return time_str
2025-07-10 17:10:07 +08:00
async def group_handle(group_id: str):
2025-04-08 17:11:44 +08:00
"""群组ban检查
参数:
2025-07-10 17:10:07 +08:00
ban_dao: BanConsole数据访问对象
2025-04-08 17:11:44 +08:00
group_id: 群组id
异常:
SkipPluginException: 群组处于黑名单
"""
2025-07-10 17:10:07 +08:00
if await is_ban(None, group_id):
raise SkipPluginException("群组处于黑名单中...")
2025-04-08 17:11:44 +08:00
2025-07-10 17:10:07 +08:00
async def user_handle(module: str, entity: EntityIDs, session: Uninfo):
2025-04-08 17:11:44 +08:00
"""用户ban检查
参数:
module: 插件模块名
2025-07-10 17:10:07 +08:00
ban_dao: BanConsole数据访问对象
entity: 实体ID信息
2025-04-08 17:11:44 +08:00
session: Uninfo
异常:
SkipPluginException: 用户处于黑名单
"""
ban_result = Config.get_config("hook", "BAN_RESULT")
2025-07-10 17:10:07 +08:00
time = await is_ban(entity.user_id, entity.group_id)
if not time:
return
time_str = format_time(time)
plugin_dao = DataAccess(PluginInfo)
db_plugin = await plugin_dao.safe_get_or_none(module=module)
if (
db_plugin
and not db_plugin.ignore_prompt
and time != -1
and ban_result
and freq.is_send_limit_message(db_plugin, entity.user_id, False)
):
await send_message(
session,
[
At(flag="user", target=entity.user_id),
f"{ban_result}\n在..在 {time_str} 后才会理你喔",
],
entity.user_id,
2025-04-08 17:11:44 +08:00
)
2025-07-10 17:10:07 +08:00
raise SkipPluginException("用户处于黑名单中...")
2025-04-08 17:11:44 +08:00
async def auth_ban(matcher: Matcher, bot: Bot, session: Uninfo):
if not check_plugin_type(matcher):
return
if not matcher.plugin_name:
return
entity = get_entity_ids(session)
if entity.user_id in bot.config.superusers:
return
if entity.group_id:
2025-07-10 17:10:07 +08:00
await group_handle(entity.group_id)
2025-04-08 17:11:44 +08:00
if entity.user_id:
2025-07-10 17:10:07 +08:00
await user_handle(matcher.plugin_name, entity, session)