mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2025-12-15 14:22:55 +08:00
157 lines
4.1 KiB
Python
157 lines
4.1 KiB
Python
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
|
|
from zhenxun.services.data_access import DataAccess
|
|
from zhenxun.utils.enum import PluginType
|
|
from zhenxun.utils.utils import EntityIDs, get_entity_ids
|
|
|
|
from .exception import SkipPluginException
|
|
from .utils import freq, send_message
|
|
|
|
Config.add_plugin_config(
|
|
"hook",
|
|
"BAN_RESULT",
|
|
"才不会给你发消息.",
|
|
help="对被ban用户发送的消息",
|
|
)
|
|
|
|
|
|
async def is_ban(user_id: str | None, group_id: str | None) -> int:
|
|
if not user_id and not group_id:
|
|
return 0
|
|
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="")
|
|
|
|
results = []
|
|
if group_user:
|
|
results.append(group_user)
|
|
if user:
|
|
results.append(user)
|
|
if not results:
|
|
return 0
|
|
for result in results:
|
|
if result.duration > 0 or result.duration == -1:
|
|
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
|
|
|
|
|
|
async def group_handle(group_id: str):
|
|
"""群组ban检查
|
|
|
|
参数:
|
|
ban_dao: BanConsole数据访问对象
|
|
group_id: 群组id
|
|
|
|
异常:
|
|
SkipPluginException: 群组处于黑名单
|
|
"""
|
|
if await is_ban(None, group_id):
|
|
raise SkipPluginException("群组处于黑名单中...")
|
|
|
|
|
|
async def user_handle(module: str, entity: EntityIDs, session: Uninfo):
|
|
"""用户ban检查
|
|
|
|
参数:
|
|
module: 插件模块名
|
|
ban_dao: BanConsole数据访问对象
|
|
entity: 实体ID信息
|
|
session: Uninfo
|
|
|
|
异常:
|
|
SkipPluginException: 用户处于黑名单
|
|
"""
|
|
ban_result = Config.get_config("hook", "BAN_RESULT")
|
|
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,
|
|
)
|
|
raise SkipPluginException("用户处于黑名单中...")
|
|
|
|
|
|
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:
|
|
await group_handle(entity.group_id)
|
|
if entity.user_id:
|
|
await user_handle(matcher.plugin_name, entity, session)
|