feat✨: 更新内置插件

This commit is contained in:
HibiKier
2024-02-25 03:18:34 +08:00
parent 131200a28e
commit eb0572ea77
83 changed files with 7588 additions and 450 deletions
+43
View File
@@ -0,0 +1,43 @@
from pathlib import Path
import nonebot
from zhenxun.configs.config import Config
Config.add_plugin_config(
"hook",
"CHECK_NOTICE_INFO_CD",
300,
help="群检测,个人权限检测等各种检测提示信息cd",
default_value=300,
type=int,
)
Config.add_plugin_config(
"hook",
"MALICIOUS_BAN_TIME",
30,
help="恶意命令触发检测触发后ban的时长(分钟)",
default_value=30,
type=int,
)
Config.add_plugin_config(
"hook",
"MALICIOUS_CHECK_TIME",
5,
help="恶意命令触发检测规定时间内(秒)",
default_value=5,
type=int,
)
Config.add_plugin_config(
"hook",
"MALICIOUS_BAN_COUNT",
6,
help="恶意命令触发检测最大触发次数",
default_value=6,
type=int,
)
nonebot.load_plugins(str(Path(__file__).parent.resolve()))
+61
View File
@@ -0,0 +1,61 @@
from nonebot.adapters import Bot, Event
from nonebot.exception import IgnoredException
from nonebot.matcher import Matcher
from nonebot.message import run_preprocessor
from nonebot.typing import T_State
from nonebot_plugin_saa import Mention, MessageFactory, Text
from nonebot_plugin_session import EventSession
from zhenxun.configs.config import Config
from zhenxun.models.ban_console import BanConsole
from zhenxun.services.log import logger
from zhenxun.utils.enum import PluginType
from zhenxun.utils.utils import FreqLimiter
Config.add_plugin_config(
"hook",
"BAN_RESULT",
"才不会给你发消息.",
help="对被ban用户发送的消息",
)
_flmt = FreqLimiter(300)
# 检查是否被ban
@run_preprocessor
async def _(
matcher: Matcher, bot: Bot, event: Event, state: T_State, session: EventSession
):
if plugin := matcher.plugin:
if metadata := plugin.metadata:
extra = metadata.extra
if extra.get("plugin_type") == PluginType.HIDDEN:
return
user_id = session.id1
group_id = session.id3 or session.id2
if user_id:
ban_result = Config.get_config("hook", "BAN_RESULT")
if user_id in bot.config.superusers:
return
if await BanConsole.is_ban(user_id) or await BanConsole.is_ban(
user_id, group_id
):
time = await BanConsole.check_ban_time(user_id)
if time == -1:
time_str = "∞"
else:
time = abs(int(time))
if time < 60:
time_str = str(time) + " 秒"
else:
time_str = str(int(time / 60)) + " 分钟"
if ban_result and _flmt.check(user_id):
_flmt.start_cd(user_id)
await MessageFactory(
[
Mention(user_id),
Text(f"{ban_result}\n在..在 {time_str} 后才会理你喔"),
]
).send()
raise IgnoredException("用户处于黑名单中")
@@ -0,0 +1,104 @@
import time
from collections import defaultdict
from click import command
from nonebot.adapters.onebot.v11 import ActionFailed, Bot, GroupMessageEvent
from nonebot.exception import IgnoredException
from nonebot.matcher import Matcher
from nonebot.message import run_preprocessor
from nonebot.typing import T_State
from nonebot_plugin_alconna import Arparma
from nonebot_plugin_saa import Mention, MessageFactory, Text
from nonebot_plugin_session import EventSession
from zhenxun.configs.config import Config
from zhenxun.models.ban_console import BanConsole
from zhenxun.services.log import logger
from zhenxun.utils.enum import PluginType
malicious_check_time = Config.get_config("hook", "MALICIOUS_CHECK_TIME")
malicious_ban_count = Config.get_config("hook", "MALICIOUS_BAN_COUNT")
if not malicious_check_time:
raise ValueError("模块: [hook], 配置项: [MALICIOUS_CHECK_TIME] 为空或小于0")
if not malicious_ban_count:
raise ValueError("模块: [hook], 配置项: [MALICIOUS_BAN_COUNT] 为空或小于0")
class BanCheckLimiter:
"""
恶意命令触发检测
"""
def __init__(self, default_check_time: float = 5, default_count: int = 4):
self.mint = defaultdict(int)
self.mtime = defaultdict(float)
self.default_check_time = default_check_time
self.default_count = default_count
def add(self, key: str | int | float):
if self.mint[key] == 1:
self.mtime[key] = time.time()
self.mint[key] += 1
def check(self, key: str | int | float) -> bool:
if time.time() - self.mtime[key] > self.default_check_time:
self.mtime[key] = time.time()
self.mint[key] = 0
return False
if (
self.mint[key] >= self.default_count
and time.time() - self.mtime[key] < self.default_check_time
):
self.mtime[key] = time.time()
self.mint[key] = 0
return True
return False
_blmt = BanCheckLimiter(
malicious_check_time,
malicious_ban_count,
)
# 恶意触发命令检测
@run_preprocessor
async def _(matcher: Matcher, bot: Bot, session: EventSession, state: T_State):
if plugin := matcher.plugin:
if metadata := plugin.metadata:
extra = metadata.extra
if extra.get("plugin_type") == PluginType.HIDDEN:
return
user_id = session.id1
group_id = session.id3 or session.id2
malicious_ban_time = Config.get_config("hook", "MALICIOUS_BAN_TIME")
if not malicious_ban_time:
raise ValueError("模块: [hook], 配置项: [MALICIOUS_BAN_TIME] 为空或小于0")
if user_id:
command = state["_prefix"]["raw_command"]
if state["_alc_result"]:
command = state["_alc_result"].source.command
if command:
if _blmt.check(f"{user_id}__{command}"):
await BanConsole.ban(
user_id, group_id, 9, malicious_ban_time * 60, bot.self_id
)
logger.info(
f"触发了恶意触发检测: {matcher.plugin_name}",
"HOOK",
session=session,
)
await MessageFactory(
[
Mention(user_id),
Text(f"检测到恶意触发命令,您将被封禁 30 分钟"),
]
).send()
logger.debug(
f"触发了恶意触发检测: {matcher.plugin_name}",
"HOOK",
session=session,
)
raise IgnoredException("检测到恶意触发命令")
_blmt.add(f"{user_id}__{command}")
@@ -0,0 +1,46 @@
import asyncio
from typing import Optional
from nonebot.adapters import Bot
from nonebot.adapters.discord import Bot as DiscordBot
from nonebot.adapters.dodo import Bot as DodoBot
from nonebot.adapters.kaiheila import Bot as KaiheilaBot
from nonebot.adapters.onebot.v11 import Bot as v11Bot
from nonebot.adapters.onebot.v12 import Bot as v12Bot
from nonebot.matcher import Matcher
from nonebot.message import run_postprocessor
from zhenxun.services.log import logger
from zhenxun.utils.utils import WithdrawManager
# TODO: 其他平台撤回消息
# 消息撤回
@run_postprocessor
async def _(
matcher: Matcher,
exception: Optional[Exception],
bot: Bot,
):
tasks = []
for message_id in WithdrawManager._data:
second = WithdrawManager._data[message_id]
tasks.append(asyncio.ensure_future(_withdraw_message(bot, message_id, second)))
WithdrawManager.remove(message_id)
await asyncio.gather(*tasks)
async def _withdraw_message(bot: Bot, message_id: str, time: int):
await asyncio.sleep(time)
logger.debug(f"撤回消息ID: {message_id}", "HOOK")
if isinstance(bot, v11Bot):
await bot.delete_msg(message_id=int(message_id))
elif isinstance(bot, v12Bot):
await bot.delete_message(message_id=message_id)
elif isinstance(bot, DodoBot):
pass
elif isinstance(bot, KaiheilaBot):
pass
elif isinstance(bot, DiscordBot):
pass