2025-07-14 22:35:29 +08:00
|
|
|
|
import asyncio
|
|
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
|
|
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-12-23 15:15:35 +08:00
|
|
|
|
from zhenxun.services.cache import CacheRoot
|
2025-07-14 22:35:29 +08:00
|
|
|
|
from zhenxun.services.data_access import DataAccess
|
|
|
|
|
|
from zhenxun.services.db_context import DB_TIMEOUT_SECONDS
|
|
|
|
|
|
from zhenxun.services.log import logger
|
2025-12-23 15:15:35 +08:00
|
|
|
|
from zhenxun.utils.enum import CacheType, PluginType
|
2025-07-14 22:35:29 +08:00
|
|
|
|
from zhenxun.utils.utils import EntityIDs, get_entity_ids
|
|
|
|
|
|
|
|
|
|
|
|
from .config import LOGGER_COMMAND, WARNING_THRESHOLD
|
|
|
|
|
|
from .exception import SkipPluginException
|
|
|
|
|
|
from .utils import freq, send_message
|
|
|
|
|
|
|
|
|
|
|
|
Config.add_plugin_config(
|
|
|
|
|
|
"hook",
|
|
|
|
|
|
"BAN_RESULT",
|
|
|
|
|
|
"才不会给你发消息.",
|
|
|
|
|
|
help="对被ban用户发送的消息",
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-07-29 17:22:27 +08:00
|
|
|
|
async def calculate_ban_time(ban_record: BanConsole | None) -> int:
|
2025-07-14 22:35:29 +08:00
|
|
|
|
"""根据ban记录计算剩余ban时间
|
|
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
|
ban_record: BanConsole记录
|
|
|
|
|
|
|
|
|
|
|
|
返回:
|
|
|
|
|
|
int: ban剩余时长,-1时为永久ban,0表示未被ban
|
|
|
|
|
|
"""
|
|
|
|
|
|
if not ban_record:
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
if ban_record.duration == -1:
|
|
|
|
|
|
return -1
|
|
|
|
|
|
|
|
|
|
|
|
_time = time.time() - (ban_record.ban_time + ban_record.duration)
|
2025-07-29 17:22:27 +08:00
|
|
|
|
if _time < 0:
|
|
|
|
|
|
return int(abs(_time))
|
|
|
|
|
|
await ban_record.delete()
|
|
|
|
|
|
return 0
|
2025-07-14 22:35:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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_val: float) -> str:
|
|
|
|
|
|
"""格式化时间
|
|
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
|
time_val: ban时长
|
|
|
|
|
|
|
|
|
|
|
|
返回:
|
|
|
|
|
|
str: 格式化时间文本
|
|
|
|
|
|
"""
|
|
|
|
|
|
if time_val == -1:
|
|
|
|
|
|
return "∞"
|
|
|
|
|
|
time_val = abs(int(time_val))
|
|
|
|
|
|
if time_val < 60:
|
|
|
|
|
|
time_str = f"{time_val!s} 秒"
|
|
|
|
|
|
else:
|
|
|
|
|
|
minute = int(time_val / 60)
|
|
|
|
|
|
if minute > 60:
|
|
|
|
|
|
hours = minute // 60
|
|
|
|
|
|
minute %= 60
|
|
|
|
|
|
time_str = f"{hours} 小时 {minute}分钟"
|
|
|
|
|
|
else:
|
|
|
|
|
|
time_str = f"{minute} 分钟"
|
|
|
|
|
|
return time_str
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-12-23 15:15:35 +08:00
|
|
|
|
async def user_handle(
|
|
|
|
|
|
plugin: PluginInfo, entity: EntityIDs, session: Uninfo, time_val: int
|
|
|
|
|
|
) -> None:
|
2025-07-14 22:35:29 +08:00
|
|
|
|
"""用户ban检查
|
|
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
|
module: 插件模块名
|
|
|
|
|
|
entity: 实体ID信息
|
|
|
|
|
|
session: Uninfo
|
2025-12-23 15:15:35 +08:00
|
|
|
|
time_val: 剩余ban时间
|
2025-07-14 22:35:29 +08:00
|
|
|
|
异常:
|
|
|
|
|
|
SkipPluginException: 用户处于黑名单
|
|
|
|
|
|
"""
|
|
|
|
|
|
start_time = time.time()
|
|
|
|
|
|
try:
|
|
|
|
|
|
ban_result = Config.get_config("hook", "BAN_RESULT")
|
|
|
|
|
|
if not time_val:
|
|
|
|
|
|
return
|
|
|
|
|
|
time_str = format_time(time_val)
|
|
|
|
|
|
|
|
|
|
|
|
if (
|
2025-10-09 08:46:08 +08:00
|
|
|
|
plugin
|
2025-07-14 22:35:29 +08:00
|
|
|
|
and time_val != -1
|
|
|
|
|
|
and ban_result
|
2025-10-09 08:46:08 +08:00
|
|
|
|
and freq.is_send_limit_message(plugin, entity.user_id, False)
|
2025-07-14 22:35:29 +08:00
|
|
|
|
):
|
|
|
|
|
|
try:
|
|
|
|
|
|
await asyncio.wait_for(
|
|
|
|
|
|
send_message(
|
|
|
|
|
|
session,
|
|
|
|
|
|
[
|
|
|
|
|
|
At(flag="user", target=entity.user_id),
|
|
|
|
|
|
f"{ban_result}\n在..在 {time_str} 后才会理你喔",
|
|
|
|
|
|
],
|
|
|
|
|
|
entity.user_id,
|
|
|
|
|
|
),
|
|
|
|
|
|
timeout=DB_TIMEOUT_SECONDS,
|
|
|
|
|
|
)
|
|
|
|
|
|
except asyncio.TimeoutError:
|
|
|
|
|
|
logger.error(f"发送消息超时: {entity.user_id}", LOGGER_COMMAND)
|
|
|
|
|
|
raise SkipPluginException("用户处于黑名单中...")
|
|
|
|
|
|
finally:
|
|
|
|
|
|
# 记录执行时间
|
|
|
|
|
|
elapsed = time.time() - start_time
|
|
|
|
|
|
if elapsed > WARNING_THRESHOLD: # 记录耗时超过500ms的检查
|
|
|
|
|
|
logger.warning(
|
|
|
|
|
|
f"user_handle 耗时: {elapsed:.3f}s",
|
|
|
|
|
|
LOGGER_COMMAND,
|
|
|
|
|
|
session=session,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-10-09 08:46:08 +08:00
|
|
|
|
async def auth_ban(
|
|
|
|
|
|
matcher: Matcher, bot: Bot, session: Uninfo, plugin: PluginInfo
|
|
|
|
|
|
) -> None:
|
2025-07-14 22:35:29 +08:00
|
|
|
|
"""权限检查 - ban 检查
|
|
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
|
matcher: Matcher
|
|
|
|
|
|
bot: Bot
|
|
|
|
|
|
session: Uninfo
|
|
|
|
|
|
"""
|
|
|
|
|
|
start_time = time.time()
|
|
|
|
|
|
try:
|
|
|
|
|
|
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
|
|
|
|
|
|
|
2025-12-23 15:15:35 +08:00
|
|
|
|
cache_key = f"{entity.user_id}_{entity.group_id}"
|
|
|
|
|
|
|
|
|
|
|
|
results = await CacheRoot.get(CacheType.BAN, cache_key)
|
|
|
|
|
|
if not results:
|
|
|
|
|
|
results = await BanConsole.is_ban(entity.user_id, entity.group_id)
|
|
|
|
|
|
await CacheRoot.set(
|
|
|
|
|
|
CacheType.BAN,
|
|
|
|
|
|
cache_key,
|
|
|
|
|
|
results or DataAccess._NULL_RESULT,
|
|
|
|
|
|
)
|
|
|
|
|
|
else:
|
2025-12-23 15:26:32 +08:00
|
|
|
|
if results == DataAccess._NULL_RESULT:
|
|
|
|
|
|
return
|
2025-12-23 15:15:35 +08:00
|
|
|
|
tmp_results: list[BanConsole] = []
|
|
|
|
|
|
for r in results:
|
|
|
|
|
|
tmp_results.append(CacheRoot._deserialize_value(r, BanConsole))
|
|
|
|
|
|
results = tmp_results
|
|
|
|
|
|
|
|
|
|
|
|
for result in results:
|
|
|
|
|
|
if not result.user_id and result.group_id:
|
|
|
|
|
|
logger.debug(
|
|
|
|
|
|
f"群组{result.group_id}被ban: {result}",
|
|
|
|
|
|
target=f"{result.group_id}:{entity.user_id}",
|
2025-07-14 22:35:29 +08:00
|
|
|
|
)
|
2025-12-23 15:15:35 +08:00
|
|
|
|
raise SkipPluginException(f"群组: {result.group_id} 处于黑名单中...")
|
|
|
|
|
|
if result.user_id:
|
|
|
|
|
|
logger.debug(
|
|
|
|
|
|
f"用户{result.user_id}被ban: {result}",
|
|
|
|
|
|
target=f"{result.group_id}:{entity.user_id}",
|
|
|
|
|
|
)
|
|
|
|
|
|
await user_handle(plugin, entity, session, result.duration)
|
|
|
|
|
|
|
2025-07-14 22:35:29 +08:00
|
|
|
|
finally:
|
|
|
|
|
|
# 记录总执行时间
|
|
|
|
|
|
elapsed = time.time() - start_time
|
|
|
|
|
|
if elapsed > WARNING_THRESHOLD: # 记录耗时超过500ms的检查
|
|
|
|
|
|
logger.warning(
|
|
|
|
|
|
f"auth_ban 总耗时: {elapsed:.3f}s, plugin={matcher.plugin_name}",
|
|
|
|
|
|
LOGGER_COMMAND,
|
|
|
|
|
|
session=session,
|
|
|
|
|
|
)
|