mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-09-29 08:39:59 +08:00
FIX BUG
This commit is contained in:
+195
-143
@@ -1,7 +1,5 @@
|
||||
import time
|
||||
from typing import Optional
|
||||
|
||||
import nonebot
|
||||
from nonebot.adapters.onebot.v11 import (
|
||||
Bot,
|
||||
Event,
|
||||
@@ -27,12 +25,14 @@ from utils.manager import (
|
||||
StaticData,
|
||||
admin_manager,
|
||||
group_manager,
|
||||
plugin_data_manager,
|
||||
plugins2block_manager,
|
||||
plugins2cd_manager,
|
||||
plugins2count_manager,
|
||||
plugins2settings_manager,
|
||||
plugins_manager,
|
||||
)
|
||||
from utils.manager.models import PluginType
|
||||
from utils.message_builder import at
|
||||
from utils.utils import FreqLimiter
|
||||
|
||||
@@ -71,17 +71,17 @@ def set_block_limit_false(event, module):
|
||||
:param module: 插件模块
|
||||
"""
|
||||
if plugins2block_manager.check_plugin_block_status(module):
|
||||
plugin_block_data = plugins2block_manager.get_plugin_block_data(module)
|
||||
check_type = plugin_block_data.check_type
|
||||
limit_type = plugin_block_data.limit_type
|
||||
if not (
|
||||
(isinstance(event, GroupMessageEvent) and check_type == "private")
|
||||
or (isinstance(event, PrivateMessageEvent) and check_type == "group")
|
||||
):
|
||||
block_type_ = event.user_id
|
||||
if limit_type == "group" and isinstance(event, GroupMessageEvent):
|
||||
block_type_ = event.group_id
|
||||
plugins2block_manager.set_false(block_type_, module)
|
||||
if plugin_block_data := plugins2block_manager.get_plugin_block_data(module):
|
||||
check_type = plugin_block_data.check_type
|
||||
limit_type = plugin_block_data.limit_type
|
||||
if not (
|
||||
(isinstance(event, GroupMessageEvent) and check_type == "private")
|
||||
or (isinstance(event, PrivateMessageEvent) and check_type == "group")
|
||||
):
|
||||
block_type_ = event.user_id
|
||||
if limit_type == "group" and isinstance(event, GroupMessageEvent):
|
||||
block_type_ = event.group_id
|
||||
plugins2block_manager.set_false(block_type_, module)
|
||||
|
||||
|
||||
async def send_msg(msg: str, bot: Bot, event: MessageEvent):
|
||||
@@ -94,7 +94,7 @@ async def send_msg(msg: str, bot: Bot, event: MessageEvent):
|
||||
:param event: pass
|
||||
"""
|
||||
if "[uname]" in msg:
|
||||
uname = event.sender.card or event.sender.nickname
|
||||
uname = event.sender.card or event.sender.nickname or ""
|
||||
msg = msg.replace("[uname]", uname)
|
||||
if "[nickname]" in msg:
|
||||
if isinstance(event, GroupMessageEvent):
|
||||
@@ -128,10 +128,13 @@ class AuthChecker:
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self._flmt = FreqLimiter(Config.get_config("hook", "CHECK_NOTICE_INFO_CD"))
|
||||
self._flmt_g = FreqLimiter(Config.get_config("hook", "CHECK_NOTICE_INFO_CD"))
|
||||
self._flmt_s = FreqLimiter(Config.get_config("hook", "CHECK_NOTICE_INFO_CD"))
|
||||
self._flmt_c = FreqLimiter(Config.get_config("hook", "CHECK_NOTICE_INFO_CD"))
|
||||
check_notice_info_cd = Config.get_config("hook", "CHECK_NOTICE_INFO_CD")
|
||||
if check_notice_info_cd is None or check_notice_info_cd < 0:
|
||||
raise ValueError("模块: [hook], 配置项: [CHECK_NOTICE_INFO_CD] 为空或小于0")
|
||||
self._flmt = FreqLimiter(check_notice_info_cd)
|
||||
self._flmt_g = FreqLimiter(check_notice_info_cd)
|
||||
self._flmt_s = FreqLimiter(check_notice_info_cd)
|
||||
self._flmt_c = FreqLimiter(check_notice_info_cd)
|
||||
|
||||
async def auth(self, matcher: Matcher, bot: Bot, event: Event):
|
||||
"""
|
||||
@@ -142,22 +145,24 @@ class AuthChecker:
|
||||
:param bot: bot
|
||||
:param event: event
|
||||
"""
|
||||
user_id = getattr(event, "user_id", None)
|
||||
group_id = getattr(event, "group_id", None)
|
||||
try:
|
||||
if plugin_name := matcher.plugin_name:
|
||||
cost_gold = await self.auth_cost(plugin_name, bot, event)
|
||||
user_id = getattr(event, "user_id", None)
|
||||
group_id = getattr(event, "group_id", None)
|
||||
if user_id and str(user_id) not in bot.config.superusers:
|
||||
await self.auth_basic(plugin_name, bot, event)
|
||||
self.auth_group(plugin_name, bot, event)
|
||||
await self.auth_admin(plugin_name, matcher, bot, event)
|
||||
await self.auth_plugin(plugin_name, matcher, bot, event)
|
||||
await self.auth_limit(plugin_name, bot, event)
|
||||
if cost_gold:
|
||||
await BagUser.spend_gold(
|
||||
event.user_id, event.group_id, cost_gold
|
||||
)
|
||||
if cost_gold and user_id and group_id:
|
||||
await BagUser.spend_gold(user_id, group_id, cost_gold)
|
||||
logger.debug(f"调用功能花费金币: {cost_gold}", "HOOK", user_id, group_id)
|
||||
except IsSuperuserException:
|
||||
return
|
||||
logger.debug(f"超级用户或被ban跳过权限检测...", "HOOK", user_id, group_id)
|
||||
|
||||
async def auth_limit(self, plugin_name: str, bot: Bot, event: Event):
|
||||
"""
|
||||
@@ -168,62 +173,79 @@ class AuthChecker:
|
||||
:param bot: bot
|
||||
:param event: event
|
||||
"""
|
||||
user_id = getattr(event, "user_id", None)
|
||||
if not user_id:
|
||||
return
|
||||
group_id = getattr(event, "group_id", None)
|
||||
if plugins2cd_manager.check_plugin_cd_status(plugin_name):
|
||||
plugin_cd_data = plugins2cd_manager.get_plugin_cd_data(plugin_name)
|
||||
check_type = plugin_cd_data.check_type
|
||||
limit_type = plugin_cd_data.limit_type
|
||||
msg = plugin_cd_data.rst
|
||||
if (
|
||||
(isinstance(event, PrivateMessageEvent) and check_type == "private")
|
||||
or (isinstance(event, GroupMessageEvent) and check_type == "group")
|
||||
or plugins2cd_manager.get_plugin_data(plugin_name).check_type == "all"
|
||||
):
|
||||
cd_type_ = event.user_id
|
||||
if limit_type == "group" and isinstance(event, GroupMessageEvent):
|
||||
cd_type_ = event.group_id
|
||||
if not plugins2cd_manager.check(plugin_name, cd_type_):
|
||||
if msg:
|
||||
await send_msg(msg, bot, event)
|
||||
raise IgnoredException(f"{plugin_name} 正在cd中...")
|
||||
else:
|
||||
plugins2cd_manager.start_cd(plugin_name, cd_type_)
|
||||
plugin_cd_data := plugins2cd_manager.get_plugin_cd_data(plugin_name)
|
||||
) and (plugin_data := plugins2cd_manager.get_plugin_data(plugin_name)):
|
||||
check_type = plugin_cd_data.check_type
|
||||
limit_type = plugin_cd_data.limit_type
|
||||
msg = plugin_cd_data.rst
|
||||
if (
|
||||
(isinstance(event, PrivateMessageEvent) and check_type == "private")
|
||||
or (isinstance(event, GroupMessageEvent) and check_type == "group")
|
||||
or plugin_data.check_type == "all"
|
||||
):
|
||||
cd_type_ = user_id
|
||||
if limit_type == "group" and isinstance(event, GroupMessageEvent):
|
||||
cd_type_ = event.group_id
|
||||
if not plugins2cd_manager.check(plugin_name, cd_type_):
|
||||
if msg:
|
||||
await send_msg(msg, bot, event) # type: ignore
|
||||
logger.debug(
|
||||
f"{plugin_name} 正在cd中...", "HOOK", user_id, group_id
|
||||
)
|
||||
raise IgnoredException(f"{plugin_name} 正在cd中...")
|
||||
else:
|
||||
plugins2cd_manager.start_cd(plugin_name, cd_type_)
|
||||
# Block
|
||||
if plugins2block_manager.check_plugin_block_status(plugin_name):
|
||||
plugin_block_data = plugins2block_manager.get_plugin_block_data(plugin_name)
|
||||
check_type = plugin_block_data.check_type
|
||||
limit_type = plugin_block_data.limit_type
|
||||
msg = plugin_block_data.rst
|
||||
if (
|
||||
(isinstance(event, PrivateMessageEvent) and check_type == "private")
|
||||
or (isinstance(event, GroupMessageEvent) and check_type == "group")
|
||||
or check_type == "all"
|
||||
if plugin_block_data := plugins2block_manager.get_plugin_block_data(
|
||||
plugin_name
|
||||
):
|
||||
block_type_ = event.user_id
|
||||
if limit_type == "group" and isinstance(event, GroupMessageEvent):
|
||||
block_type_ = event.group_id
|
||||
if plugins2block_manager.check(block_type_, plugin_name):
|
||||
if msg:
|
||||
await send_msg(msg, bot, event)
|
||||
raise IgnoredException(f"{event.user_id}正在调用{plugin_name}....")
|
||||
else:
|
||||
plugins2block_manager.set_true(block_type_, plugin_name)
|
||||
check_type = plugin_block_data.check_type
|
||||
limit_type = plugin_block_data.limit_type
|
||||
msg = plugin_block_data.rst
|
||||
if (
|
||||
(isinstance(event, PrivateMessageEvent) and check_type == "private")
|
||||
or (isinstance(event, GroupMessageEvent) and check_type == "group")
|
||||
or check_type == "all"
|
||||
):
|
||||
block_type_ = user_id
|
||||
if limit_type == "group" and isinstance(event, GroupMessageEvent):
|
||||
block_type_ = event.group_id
|
||||
if plugins2block_manager.check(block_type_, plugin_name):
|
||||
if msg:
|
||||
await send_msg(msg, bot, event) # type: ignore
|
||||
logger.debug(f"正在调用{plugin_name}...", "HOOK", user_id, group_id)
|
||||
raise IgnoredException(f"{user_id}正在调用{plugin_name}....")
|
||||
else:
|
||||
plugins2block_manager.set_true(block_type_, plugin_name)
|
||||
# Count
|
||||
if (
|
||||
plugins2count_manager.check_plugin_count_status(plugin_name)
|
||||
and event.user_id not in bot.config.superusers
|
||||
and user_id not in bot.config.superusers
|
||||
):
|
||||
plugin_count_data = plugins2count_manager.get_plugin_count_data(plugin_name)
|
||||
limit_type = plugin_count_data.limit_type
|
||||
msg = plugin_count_data.rst
|
||||
count_type_ = event.user_id
|
||||
if limit_type == "group" and isinstance(event, GroupMessageEvent):
|
||||
count_type_ = event.group_id
|
||||
if not plugins2count_manager.check(plugin_name, count_type_):
|
||||
if msg:
|
||||
await send_msg(msg, bot, event)
|
||||
raise IgnoredException(f"{plugin_name} count次数限制...")
|
||||
else:
|
||||
plugins2count_manager.increase(plugin_name, count_type_)
|
||||
if plugin_count_data := plugins2count_manager.get_plugin_count_data(
|
||||
plugin_name
|
||||
):
|
||||
limit_type = plugin_count_data.limit_type
|
||||
msg = plugin_count_data.rst
|
||||
count_type_ = user_id
|
||||
if limit_type == "group" and isinstance(event, GroupMessageEvent):
|
||||
count_type_ = event.group_id
|
||||
if not plugins2count_manager.check(plugin_name, count_type_):
|
||||
if msg:
|
||||
await send_msg(msg, bot, event) # type: ignore
|
||||
logger.debug(
|
||||
f"{plugin_name} count次数限制...", "HOOK", user_id, group_id
|
||||
)
|
||||
raise IgnoredException(f"{plugin_name} count次数限制...")
|
||||
else:
|
||||
plugins2count_manager.increase(plugin_name, count_type_)
|
||||
|
||||
async def auth_plugin(
|
||||
self, plugin_name: str, matcher: Matcher, bot: Bot, event: Event
|
||||
@@ -241,66 +263,75 @@ class AuthChecker:
|
||||
1,
|
||||
999,
|
||||
]:
|
||||
user_id = getattr(event, "user_id", None)
|
||||
if not user_id:
|
||||
return
|
||||
group_id = getattr(event, "group_id", None)
|
||||
# 戳一戳单独判断
|
||||
if (
|
||||
isinstance(event, GroupMessageEvent)
|
||||
or isinstance(event, PokeNotifyEvent)
|
||||
or matcher.plugin_name in other_limit_plugins
|
||||
):
|
||||
if status_message_manager.get(event.group_id) is None:
|
||||
status_message_manager.delete(event.group_id)
|
||||
) and group_id:
|
||||
if status_message_manager.get(group_id) is None:
|
||||
status_message_manager.delete(group_id)
|
||||
if plugins2settings_manager[
|
||||
plugin_name
|
||||
].level > group_manager.get_group_level(event.group_id):
|
||||
].level > group_manager.get_group_level(group_id):
|
||||
try:
|
||||
if (
|
||||
self._flmt_g.check(event.user_id)
|
||||
self._flmt_g.check(user_id)
|
||||
and plugin_name not in ignore_rst_module
|
||||
):
|
||||
self._flmt_g.start_cd(event.user_id)
|
||||
self._flmt_g.start_cd(user_id)
|
||||
await bot.send_group_msg(
|
||||
group_id=event.group_id, message="群权限不足..."
|
||||
group_id=group_id, message="群权限不足..."
|
||||
)
|
||||
except ActionFailed:
|
||||
pass
|
||||
if event.is_tome():
|
||||
status_message_manager.add(event.group_id)
|
||||
status_message_manager.add(group_id)
|
||||
set_block_limit_false(event, plugin_name)
|
||||
logger.debug(f"{plugin_name} 群权限不足...", "HOOK", user_id, group_id)
|
||||
raise IgnoredException("群权限不足")
|
||||
# 插件状态
|
||||
if not group_manager.get_plugin_status(plugin_name, event.group_id):
|
||||
if not group_manager.get_plugin_status(plugin_name, group_id):
|
||||
try:
|
||||
if plugin_name not in ignore_rst_module and self._flmt_s.check(
|
||||
event.group_id
|
||||
group_id
|
||||
):
|
||||
self._flmt_s.start_cd(event.group_id)
|
||||
self._flmt_s.start_cd(group_id)
|
||||
await bot.send_group_msg(
|
||||
group_id=event.group_id, message="该群未开启此功能.."
|
||||
group_id=group_id, message="该群未开启此功能.."
|
||||
)
|
||||
except ActionFailed:
|
||||
pass
|
||||
if event.is_tome():
|
||||
status_message_manager.add(event.group_id)
|
||||
status_message_manager.add(group_id)
|
||||
set_block_limit_false(event, plugin_name)
|
||||
logger.debug(f"{plugin_name} 未开启此功能...", "HOOK", user_id, group_id)
|
||||
raise IgnoredException("未开启此功能...")
|
||||
# 管理员禁用
|
||||
if not group_manager.get_plugin_status(
|
||||
f"{plugin_name}:super", event.group_id
|
||||
f"{plugin_name}:super", group_id
|
||||
):
|
||||
try:
|
||||
if (
|
||||
self._flmt_s.check(event.group_id)
|
||||
self._flmt_s.check(group_id)
|
||||
and plugin_name not in ignore_rst_module
|
||||
):
|
||||
self._flmt_s.start_cd(event.group_id)
|
||||
self._flmt_s.start_cd(group_id)
|
||||
await bot.send_group_msg(
|
||||
group_id=event.group_id, message="管理员禁用了此群该功能..."
|
||||
group_id=group_id, message="管理员禁用了此群该功能..."
|
||||
)
|
||||
except ActionFailed:
|
||||
pass
|
||||
if event.is_tome():
|
||||
status_message_manager.add(event.group_id)
|
||||
status_message_manager.add(group_id)
|
||||
set_block_limit_false(event, plugin_name)
|
||||
logger.debug(
|
||||
f"{plugin_name} 管理员禁用了此群该功能...", "HOOK", user_id, group_id
|
||||
)
|
||||
raise IgnoredException("管理员禁用了此群该功能...")
|
||||
# 群聊禁用
|
||||
if not plugins_manager.get_plugin_status(
|
||||
@@ -308,18 +339,21 @@ class AuthChecker:
|
||||
):
|
||||
try:
|
||||
if (
|
||||
self._flmt_c.check(event.group_id)
|
||||
self._flmt_c.check(group_id)
|
||||
and plugin_name not in ignore_rst_module
|
||||
):
|
||||
self._flmt_c.start_cd(event.group_id)
|
||||
self._flmt_c.start_cd(group_id)
|
||||
await bot.send_group_msg(
|
||||
group_id=event.group_id, message="该功能在群聊中已被禁用..."
|
||||
group_id=group_id, message="该功能在群聊中已被禁用..."
|
||||
)
|
||||
except ActionFailed:
|
||||
pass
|
||||
if event.is_tome():
|
||||
status_message_manager.add(event.group_id)
|
||||
status_message_manager.add(group_id)
|
||||
set_block_limit_false(event, plugin_name)
|
||||
logger.debug(
|
||||
f"{plugin_name} 该插件在群聊中已被禁用...", "HOOK", user_id, group_id
|
||||
)
|
||||
raise IgnoredException("该插件在群聊中已被禁用...")
|
||||
else:
|
||||
# 私聊禁用
|
||||
@@ -327,16 +361,19 @@ class AuthChecker:
|
||||
plugin_name, block_type="private"
|
||||
):
|
||||
try:
|
||||
if self._flmt_c.check(event.user_id):
|
||||
self._flmt_c.start_cd(event.user_id)
|
||||
if self._flmt_c.check(user_id):
|
||||
self._flmt_c.start_cd(user_id)
|
||||
await bot.send_private_msg(
|
||||
user_id=event.user_id, message="该功能在私聊中已被禁用..."
|
||||
user_id=user_id, message="该功能在私聊中已被禁用..."
|
||||
)
|
||||
except ActionFailed:
|
||||
pass
|
||||
if event.is_tome():
|
||||
status_message_manager.add(event.user_id)
|
||||
status_message_manager.add(user_id)
|
||||
set_block_limit_false(event, plugin_name)
|
||||
logger.debug(
|
||||
f"{plugin_name} 该插件在私聊中已被禁用...", "HOOK", user_id, group_id
|
||||
)
|
||||
raise IgnoredException("该插件在私聊中已被禁用...")
|
||||
# 维护
|
||||
if not plugins_manager.get_plugin_status(plugin_name, block_type="all"):
|
||||
@@ -357,18 +394,15 @@ class AuthChecker:
|
||||
)
|
||||
else:
|
||||
await bot.send_private_msg(
|
||||
user_id=event.user_id, message="此功能正在维护..."
|
||||
user_id=user_id, message="此功能正在维护..."
|
||||
)
|
||||
except ActionFailed:
|
||||
pass
|
||||
if event.is_tome():
|
||||
id_ = (
|
||||
event.group_id
|
||||
if isinstance(event, GroupMessageEvent)
|
||||
else event.user_id
|
||||
)
|
||||
id_ = group_id or user_id
|
||||
status_message_manager.add(id_)
|
||||
set_block_limit_false(event, plugin_name)
|
||||
logger.debug(f"{plugin_name} 此功能正在维护...", "HOOK", user_id, group_id)
|
||||
raise IgnoredException("此功能正在维护...")
|
||||
|
||||
async def auth_admin(
|
||||
@@ -383,6 +417,10 @@ class AuthChecker:
|
||||
:param bot: bot
|
||||
:param event: event
|
||||
"""
|
||||
user_id = getattr(event, "user_id", None)
|
||||
if not user_id:
|
||||
return
|
||||
group_id = getattr(event, "group_id", None)
|
||||
if plugin_name in admin_manager.keys() and matcher.priority not in [1, 999]:
|
||||
if isinstance(event, GroupMessageEvent):
|
||||
# 个人权限
|
||||
@@ -407,21 +445,23 @@ class AuthChecker:
|
||||
set_block_limit_false(event, plugin_name)
|
||||
if event.is_tome():
|
||||
status_message_manager.add(event.group_id)
|
||||
raise IgnoredException("权限不足")
|
||||
logger.debug(f"{plugin_name} 管理员权限不足...", "HOOK", user_id, group_id)
|
||||
raise IgnoredException("管理员权限不足")
|
||||
else:
|
||||
if not await LevelUser.check_level(
|
||||
event.user_id, 0, admin_manager.get_plugin_level(plugin_name)
|
||||
user_id, 0, admin_manager.get_plugin_level(plugin_name)
|
||||
):
|
||||
try:
|
||||
await bot.send_private_msg(
|
||||
user_id=event.user_id,
|
||||
user_id=user_id,
|
||||
message=f"你的权限不足喔,该功能需要的权限等级:{admin_manager.get_plugin_level(plugin_name)}",
|
||||
)
|
||||
except ActionFailed:
|
||||
pass
|
||||
set_block_limit_false(event, plugin_name)
|
||||
if event.is_tome():
|
||||
status_message_manager.add(event.user_id)
|
||||
status_message_manager.add(user_id)
|
||||
logger.debug(f"{plugin_name} 管理员权限不足...", "HOOK", user_id, group_id)
|
||||
raise IgnoredException("权限不足")
|
||||
|
||||
def auth_group(self, plugin_name: str, bot: Bot, event: Event):
|
||||
@@ -433,21 +473,26 @@ class AuthChecker:
|
||||
:param bot: bot
|
||||
:param event: event
|
||||
"""
|
||||
if isinstance(event, GroupMessageEvent) or plugin_name in other_limit_plugins:
|
||||
user_id = getattr(event, "user_id", None)
|
||||
group_id = getattr(event, "group_id", None)
|
||||
if not group_id:
|
||||
return
|
||||
if (
|
||||
group_manager.get_group_level(group_id) < 0
|
||||
and str(user_id) not in bot.config.superusers
|
||||
):
|
||||
logger.debug(f"{plugin_name} 群黑名单, 群权限-1...", "HOOK", user_id, group_id)
|
||||
raise IgnoredException("群黑名单")
|
||||
if not group_manager.check_group_bot_status(group_id):
|
||||
try:
|
||||
if (
|
||||
group_manager.get_group_level(event.group_id) < 0
|
||||
and str(event.user_id) not in bot.config.superusers
|
||||
):
|
||||
raise IgnoredException("群黑名单")
|
||||
if not group_manager.check_group_bot_status(event.group_id):
|
||||
try:
|
||||
if str(event.get_message()) != "醒来":
|
||||
raise IgnoredException("功能总开关关闭状态")
|
||||
except ValueError:
|
||||
raise IgnoredException("功能总开关关闭状态")
|
||||
except AttributeError:
|
||||
pass
|
||||
if str(event.get_message()) != "醒来":
|
||||
logger.debug(
|
||||
f"{plugin_name} 功能总开关关闭状态...", "HOOK", user_id, group_id
|
||||
)
|
||||
raise IgnoredException("功能总开关关闭状态")
|
||||
except ValueError:
|
||||
logger.debug(f"{plugin_name} 功能总开关关闭状态...", "HOOK", user_id, group_id)
|
||||
raise IgnoredException("功能总开关关闭状态")
|
||||
|
||||
async def auth_basic(self, plugin_name: str, bot: Bot, event: Event):
|
||||
"""
|
||||
@@ -458,32 +503,29 @@ class AuthChecker:
|
||||
:param bot: bot
|
||||
:param event: event
|
||||
"""
|
||||
try:
|
||||
user_id = getattr(event, "user_id", None)
|
||||
if not user_id:
|
||||
return
|
||||
plugin_setting = plugins2settings_manager.get_plugin_data(plugin_name)
|
||||
if (
|
||||
(
|
||||
not isinstance(event, MessageEvent)
|
||||
and plugin_name not in other_limit_plugins
|
||||
)
|
||||
or await BanUser.is_ban(user_id)
|
||||
and str(user_id) not in bot.config.superusers
|
||||
) or (
|
||||
str(user_id) in bot.config.superusers
|
||||
and plugin_setting
|
||||
and not plugin_setting.limit_superuser
|
||||
):
|
||||
raise IsSuperuserException()
|
||||
if plugin_data := plugin_data_manager.get(plugin_name):
|
||||
if (
|
||||
(
|
||||
not isinstance(event, MessageEvent)
|
||||
and plugin_name not in other_limit_plugins
|
||||
)
|
||||
or await BanUser.is_ban(event.user_id)
|
||||
and str(event.user_id) not in bot.config.superusers
|
||||
) or (
|
||||
str(event.user_id) in bot.config.superusers
|
||||
and plugins2settings_manager.get_plugin_data(plugin_name)
|
||||
and not plugins2settings_manager.get_plugin_data(
|
||||
plugin_name
|
||||
).limit_superuser
|
||||
plugin_data.plugin_type == PluginType.SUPERUSER
|
||||
and str(user_id) in bot.config.superusers
|
||||
):
|
||||
raise IsSuperuserException()
|
||||
_plugin = nonebot.plugin.get_plugin(plugin_name)
|
||||
_module = _plugin.module
|
||||
_plugin_name = _module.__getattribute__("__zx_plugin_name__")
|
||||
if (
|
||||
"[superuser]" in _plugin_name.lower()
|
||||
and str(event.user_id) in bot.config.superusers
|
||||
):
|
||||
raise IsSuperuserException()
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
async def auth_cost(self, plugin_name: str, bot: Bot, event: Event) -> int:
|
||||
"""
|
||||
@@ -494,6 +536,10 @@ class AuthChecker:
|
||||
:param bot: bot
|
||||
:param event: event
|
||||
"""
|
||||
user_id = getattr(event, "user_id", None)
|
||||
if not user_id:
|
||||
return 0
|
||||
group_id = getattr(event, "group_id", None)
|
||||
cost_gold = 0
|
||||
if isinstance(event, GroupMessageEvent) and (
|
||||
psm := plugins2settings_manager.get_plugin_data(plugin_name)
|
||||
@@ -504,6 +550,12 @@ class AuthChecker:
|
||||
< psm.cost_gold
|
||||
):
|
||||
await send_msg(f"金币不足..该功能需要{psm.cost_gold}金币..", bot, event)
|
||||
logger.debug(
|
||||
f"{plugin_name} 金币限制..该功能需要{psm.cost_gold}金币..",
|
||||
"HOOK",
|
||||
user_id,
|
||||
group_id,
|
||||
)
|
||||
raise IgnoredException(f"{plugin_name} 金币限制...")
|
||||
# 当插件不阻塞超级用户时,超级用户提前扣除金币
|
||||
if (
|
||||
|
||||
@@ -1,17 +1,11 @@
|
||||
from nonebot.adapters.onebot.v11 import (
|
||||
ActionFailed,
|
||||
Bot,
|
||||
Event,
|
||||
GroupMessageEvent,
|
||||
MessageEvent,
|
||||
PokeNotifyEvent,
|
||||
)
|
||||
from nonebot.adapters.onebot.v11 import ActionFailed, Bot, Event, GroupMessageEvent
|
||||
from nonebot.matcher import Matcher
|
||||
from nonebot.message import IgnoredException, run_preprocessor
|
||||
from nonebot.typing import T_State
|
||||
|
||||
from configs.config import Config
|
||||
from models.ban_user import BanUser
|
||||
from services.log import logger
|
||||
from utils.message_builder import at
|
||||
from utils.utils import FreqLimiter, is_number, static_flmt
|
||||
|
||||
@@ -30,26 +24,19 @@ _flmt = FreqLimiter(300)
|
||||
# 检查是否被ban
|
||||
@run_preprocessor
|
||||
async def _(matcher: Matcher, bot: Bot, event: Event, state: T_State):
|
||||
if hasattr(event, "user_id") and (
|
||||
(
|
||||
(isinstance(event, MessageEvent) or isinstance(event, PokeNotifyEvent))
|
||||
and matcher.priority not in [1, 999]
|
||||
)
|
||||
or matcher.plugin_name in other_limit_plugins
|
||||
user_id = getattr(event, "user_id", None)
|
||||
group_id = getattr(event, "group_id", None)
|
||||
if user_id and (
|
||||
matcher.priority not in [1, 999] or matcher.plugin_name in other_limit_plugins
|
||||
):
|
||||
try:
|
||||
if (
|
||||
await BanUser.is_super_ban(event.user_id)
|
||||
and str(event.user_id) not in bot.config.superusers
|
||||
):
|
||||
raise IgnoredException("用户处于超级黑名单中")
|
||||
except AttributeError:
|
||||
pass
|
||||
if (
|
||||
await BanUser.is_ban(event.user_id)
|
||||
and str(event.user_id) not in bot.config.superusers
|
||||
await BanUser.is_super_ban(user_id)
|
||||
and str(user_id) not in bot.config.superusers
|
||||
):
|
||||
time = await BanUser.check_ban_time(event.user_id)
|
||||
logger.debug(f"用户处于超级黑名单中...", "HOOK", user_id, group_id)
|
||||
raise IgnoredException("用户处于超级黑名单中")
|
||||
if await BanUser.is_ban(user_id) and str(user_id) not in bot.config.superusers:
|
||||
time = await BanUser.check_ban_time(user_id)
|
||||
if isinstance(time, int):
|
||||
time = abs(int(time))
|
||||
if time < 60:
|
||||
@@ -59,40 +46,38 @@ async def _(matcher: Matcher, bot: Bot, event: Event, state: T_State):
|
||||
else:
|
||||
time = str(time) + " 分钟"
|
||||
if isinstance(event, GroupMessageEvent):
|
||||
if not static_flmt.check(event.user_id):
|
||||
if not static_flmt.check(user_id):
|
||||
logger.debug(f"用户处于黑名单中...", "HOOK", user_id, group_id)
|
||||
raise IgnoredException("用户处于黑名单中")
|
||||
static_flmt.start_cd(event.user_id)
|
||||
static_flmt.start_cd(user_id)
|
||||
if matcher.priority != 999:
|
||||
try:
|
||||
ban_result = Config.get_config("hook", "BAN_RESULT")
|
||||
if (
|
||||
ban_result
|
||||
and _flmt.check(event.user_id)
|
||||
and _flmt.check(user_id)
|
||||
and matcher.plugin_name not in ignore_rst_module
|
||||
):
|
||||
_flmt.start_cd(event.user_id)
|
||||
_flmt.start_cd(user_id)
|
||||
await bot.send_group_msg(
|
||||
group_id=event.group_id,
|
||||
message=at(event.user_id)
|
||||
message=at(user_id)
|
||||
+ ban_result
|
||||
+ f" 在..在 {time} 后才会理你喔",
|
||||
)
|
||||
except ActionFailed:
|
||||
pass
|
||||
else:
|
||||
if not static_flmt.check(event.user_id):
|
||||
if not static_flmt.check(user_id):
|
||||
logger.debug(f"用户处于黑名单中...", "HOOK", user_id, group_id)
|
||||
raise IgnoredException("用户处于黑名单中")
|
||||
static_flmt.start_cd(event.user_id)
|
||||
static_flmt.start_cd(user_id)
|
||||
if matcher.priority != 999:
|
||||
try:
|
||||
ban_result = Config.get_config("hook", "BAN_RESULT")
|
||||
if ban_result and matcher.plugin_name not in ignore_rst_module:
|
||||
await bot.send_private_msg(
|
||||
user_id=event.user_id,
|
||||
message=at(event.user_id)
|
||||
+ ban_result
|
||||
+ f" 在..在 {time}后才会理你喔",
|
||||
)
|
||||
except ActionFailed:
|
||||
pass
|
||||
ban_result = Config.get_config("hook", "BAN_RESULT")
|
||||
if ban_result and matcher.plugin_name not in ignore_rst_module:
|
||||
await bot.send_private_msg(
|
||||
user_id=user_id,
|
||||
message=at(user_id) + ban_result + f" 在..在 {time}后才会理你喔",
|
||||
)
|
||||
logger.debug(f"用户处于黑名单中...", "HOOK", user_id, group_id)
|
||||
raise IgnoredException("用户处于黑名单中")
|
||||
|
||||
@@ -1,39 +1,54 @@
|
||||
from nonebot.matcher import Matcher
|
||||
from nonebot.message import run_preprocessor, IgnoredException
|
||||
from nonebot.typing import T_State
|
||||
from nonebot.adapters.onebot.v11 import (
|
||||
Bot,
|
||||
ActionFailed,
|
||||
MessageEvent,
|
||||
Bot,
|
||||
GroupMessageEvent,
|
||||
MessageEvent,
|
||||
)
|
||||
from nonebot.matcher import Matcher
|
||||
from nonebot.message import IgnoredException, run_preprocessor
|
||||
from nonebot.typing import T_State
|
||||
|
||||
from configs.config import Config
|
||||
from models.ban_user import BanUser
|
||||
from utils.utils import BanCheckLimiter
|
||||
from utils.message_builder import at
|
||||
from services.log import logger
|
||||
from utils.message_builder import at
|
||||
from utils.utils import BanCheckLimiter
|
||||
|
||||
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")
|
||||
|
||||
_blmt = BanCheckLimiter(
|
||||
Config.get_config("hook", "MALICIOUS_CHECK_TIME"),
|
||||
Config.get_config("hook", "MALICIOUS_BAN_COUNT"),
|
||||
malicious_check_time,
|
||||
malicious_ban_count,
|
||||
)
|
||||
|
||||
|
||||
# 恶意触发命令检测
|
||||
@run_preprocessor
|
||||
async def _(matcher: Matcher, bot: Bot, event: GroupMessageEvent, state: T_State):
|
||||
user_id = getattr(event, "user_id", None)
|
||||
group_id = getattr(event, "group_id", None)
|
||||
if not isinstance(event, MessageEvent):
|
||||
return
|
||||
malicious_ban_time = Config.get_config("hook", "MALICIOUS_BAN_TIME")
|
||||
if not malicious_ban_time:
|
||||
raise ValueError("模块: [hook], 配置项: [MALICIOUS_BAN_TIME] 为空或小于0")
|
||||
if matcher.type == "message" and matcher.priority not in [1, 999]:
|
||||
if state["_prefix"]["raw_command"]:
|
||||
if _blmt.check(f'{event.user_id}{state["_prefix"]["raw_command"]}'):
|
||||
await BanUser.ban(
|
||||
event.user_id,
|
||||
9,
|
||||
Config.get_config("hook", "MALICIOUS_BAN_TIME") * 60,
|
||||
malicious_ban_time * 60,
|
||||
)
|
||||
logger.info(
|
||||
f"触发了恶意触发检测: {matcher.plugin_name}", "HOOK", user_id, group_id
|
||||
)
|
||||
logger.info(f"USER {event.user_id} 触发了恶意触发检测")
|
||||
if isinstance(event, GroupMessageEvent):
|
||||
try:
|
||||
await bot.send_group_msg(
|
||||
@@ -50,5 +65,8 @@ async def _(matcher: Matcher, bot: Bot, event: GroupMessageEvent, state: T_State
|
||||
)
|
||||
except ActionFailed:
|
||||
pass
|
||||
logger.debug(
|
||||
f"触发了恶意触发检测: {matcher.plugin_name}", "HOOK", user_id, group_id
|
||||
)
|
||||
raise IgnoredException("检测到恶意触发命令")
|
||||
_blmt.add(f'{event.user_id}{state["_prefix"]["raw_command"]}')
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
from nonebot.exception import MockApiException
|
||||
from nonebot.adapters.onebot.v11 import Bot, Message
|
||||
from utils.manager import group_manager
|
||||
from services.log import logger
|
||||
from typing import Dict, Any
|
||||
import re
|
||||
from typing import Any, Dict
|
||||
|
||||
from nonebot.adapters.onebot.v11 import Bot, Message
|
||||
from nonebot.exception import MockApiException
|
||||
|
||||
from services.log import logger
|
||||
from utils.manager import group_manager
|
||||
|
||||
|
||||
@Bot.on_calling_api
|
||||
@@ -40,13 +42,13 @@ async def _(bot: Bot, api: str, data: Dict[str, Any]):
|
||||
task = r.group(1)
|
||||
group_id = data["group_id"]
|
||||
except Exception as e:
|
||||
logger.error(f"TaskHook ERROR {type(e)}:{e}")
|
||||
logger.error(f"TaskHook ERROR", "HOOK", e=e)
|
||||
else:
|
||||
if task and group_id:
|
||||
if (
|
||||
group_manager.get_group_level(group_id) < 0
|
||||
or not group_manager.check_task_status(task, group_id)
|
||||
):
|
||||
if group_manager.get_group_level(
|
||||
group_id
|
||||
) < 0 or not group_manager.check_task_status(task, group_id):
|
||||
logger.debug(f"被动技能 {task} 处于关闭状态")
|
||||
raise MockApiException(f"被动技能 {task} 处于关闭状态...")
|
||||
else:
|
||||
msg = str(data["message"]).strip()
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
import asyncio
|
||||
from typing import Optional
|
||||
|
||||
from nonebot.adapters.onebot.v11 import Bot, Event
|
||||
from nonebot.matcher import Matcher
|
||||
from nonebot.message import run_postprocessor
|
||||
from typing import Optional
|
||||
from nonebot.typing import T_State
|
||||
from nonebot.adapters.onebot.v11 import Bot, Event
|
||||
|
||||
from services.log import logger
|
||||
from utils.manager import withdraw_message_manager
|
||||
import asyncio
|
||||
|
||||
|
||||
# 消息撤回
|
||||
@@ -25,4 +28,5 @@ async def _(
|
||||
|
||||
async def _withdraw_message(bot: Bot, id_: int, time: int):
|
||||
await asyncio.sleep(time)
|
||||
logger.debug(f"撤回消息ID: {id_}", "HOOK")
|
||||
await bot.delete_msg(message_id=id_)
|
||||
|
||||
Reference in New Issue
Block a user