Skip to content

Commit

Permalink
⚡️ 优化权限判断
Browse files Browse the repository at this point in the history
  • Loading branch information
HibiKier committed Aug 18, 2024
1 parent 369451c commit 862c977
Show file tree
Hide file tree
Showing 20 changed files with 93 additions and 37 deletions.
4 changes: 3 additions & 1 deletion zhenxun/builtin_plugins/admin/ban/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,9 @@ async def _(
await MessageUtils.build_message("权限不足捏...").finish(reply_to=True)
user_id = user.result
if gid := session.id3 or session.id2:
u_d = user_id
if group_id.available:
u_d = gid
gid = group_id.result
if await BanManage.unban(
user_id, gid, session, session.id1 in bot.config.superusers
Expand All @@ -234,7 +236,7 @@ async def _(
await MessageUtils.build_message(
[
"将 ",
At(flag="user", target=user_id) if isinstance(user.result, At) else user_id, # type: ignore
At(flag="user", target=user_id) if isinstance(user.result, At) else u_d, # type: ignore
f" 从黑屋中拉了出来并急救了一下!",
]
).finish(reply_to=True)
Expand Down
2 changes: 1 addition & 1 deletion zhenxun/builtin_plugins/help/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
extra=PluginExtraData(
author="HibiKier",
version="0.1",
plugin_type=PluginType.HIDDEN,
plugin_type=PluginType.DEPENDANT,
configs=[
RegisterConfig(
key="type",
Expand Down
2 changes: 1 addition & 1 deletion zhenxun/builtin_plugins/help/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async def sort_type(self):
self._data = await PluginInfo.filter(
menu_type__not="",
load_status=True,
plugin_type__in=[PluginType.NORMAL, PluginType.HIDDEN],
plugin_type__in=[PluginType.NORMAL, PluginType.DEPENDANT],
)
if not self._sort_data:
for plugin in self._data:
Expand Down
2 changes: 1 addition & 1 deletion zhenxun/builtin_plugins/help_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
extra=PluginExtraData(
author="HibiKier",
version="0.1",
plugin_type=PluginType.HIDDEN,
plugin_type=PluginType.DEPENDANT,
).dict(),
)

Expand Down
9 changes: 9 additions & 0 deletions zhenxun/builtin_plugins/hooks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,13 @@
type=int,
)

Config.add_plugin_config(
"hook",
"IS_SEND_TIP_MESSAGE",
True,
help="是否发送阻断时提示消息",
default_value=True,
type=bool,
)

nonebot.load_plugins(str(Path(__file__).parent.resolve()))
35 changes: 28 additions & 7 deletions zhenxun/builtin_plugins/hooks/_auth_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
from zhenxun.utils.message import MessageUtils
from zhenxun.utils.utils import CountLimiter, FreqLimiter, UserBlockLimiter

base_config = Config.get("hook")


class Limit(BaseModel):

Expand Down Expand Up @@ -181,6 +183,23 @@ def __init__(self):
self._flmt_s = FreqLimiter(check_notice_info_cd)
self._flmt_c = FreqLimiter(check_notice_info_cd)

def is_send_limit_message(self, plugin: PluginInfo, sid: str) -> bool:
"""是否发送提示消息
参数:
plugin: PluginInfo
返回:
bool: 是否发送提示消息
"""
if not base_config.get("IS_SEND_TIP_MESSAGE"):
return False
if plugin.plugin_type == PluginType.DEPENDANT:
return False
if not self._flmt_s.check(sid):
return False
return True

async def auth(
self,
matcher: Matcher,
Expand Down Expand Up @@ -217,8 +236,8 @@ async def auth(
)
return
if plugin := await PluginInfo.get_or_none(module_path=module_path):
if plugin.plugin_type == PluginType.HIDDEN and plugin.name != "帮助":
logger.debug("插件为HIDDEN且不是帮助功能,已跳过...")
if plugin.plugin_type == PluginType.HIDDEN:
logger.debug("插件为HIDDEN,已跳过...")
return
try:
cost_gold = await self.auth_cost(user, plugin, session)
Expand Down Expand Up @@ -301,11 +320,12 @@ async def auth_plugin(
channel_id = None
if user_id:
if group_id:
sid = group_id or user_id
if await GroupConsole.is_super_block_plugin(
group_id, plugin.module, channel_id
):
"""超级用户群组插件状态"""
if self._flmt_s.check(group_id or user_id) and not is_poke:
if self.is_send_limit_message(plugin, sid) and not is_poke:
self._flmt_s.start_cd(group_id or user_id)
await MessageUtils.build_message(
"超级管理员禁用了该群此功能..."
Expand All @@ -320,7 +340,7 @@ async def auth_plugin(
group_id, plugin.module, channel_id
):
"""群组插件状态"""
if self._flmt_s.check(group_id or user_id) and not is_poke:
if self.is_send_limit_message(plugin, sid) and not is_poke:
self._flmt_s.start_cd(group_id or user_id)
await MessageUtils.build_message("该群未开启此功能...").send(
reply_to=True
Expand All @@ -334,7 +354,7 @@ async def auth_plugin(
if not plugin.status and plugin.block_type == BlockType.GROUP:
"""全局群组禁用"""
try:
if self._flmt_c.check(group_id) and not is_poke:
if self.is_send_limit_message(plugin, sid) and not is_poke:
self._flmt_c.start_cd(group_id)
await MessageUtils.build_message(
"该功能在群组中已被禁用..."
Expand All @@ -350,10 +370,11 @@ async def auth_plugin(
)
raise IgnoredException("该插件在群组中已被禁用...")
else:
sid = user_id
if not plugin.status and plugin.block_type == BlockType.PRIVATE:
"""全局私聊禁用"""
try:
if self._flmt_c.check(user_id) and not is_poke:
if self.is_send_limit_message(plugin, sid) and not is_poke:
self._flmt_c.start_cd(user_id)
await MessageUtils.build_message(
"该功能在私聊中已被禁用..."
Expand All @@ -378,7 +399,7 @@ async def auth_plugin(
"HOOK",
session=session,
)
if self._flmt_s.check(group_id or user_id) and not is_poke:
if self.is_send_limit_message(plugin, sid) and not is_poke:
self._flmt_s.start_cd(group_id or user_id)
await MessageUtils.build_message("全局未开启此功能...").send()
raise IgnoredException("全局未开启此功能...")
Expand Down
2 changes: 1 addition & 1 deletion zhenxun/builtin_plugins/hooks/chkdsk_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ async def _(matcher: Matcher, bot: Bot, session: EventSession, state: T_State):
module = plugin.module_name
if metadata := plugin.metadata:
extra = metadata.extra
if extra.get("plugin_type") == PluginType.HIDDEN:
if extra.get("plugin_type") in [PluginType.HIDDEN, PluginType.DEPENDANT]:
return
else:
return
Expand Down
26 changes: 19 additions & 7 deletions zhenxun/builtin_plugins/init/init_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ async def _handle_setting(
if metadata.type == "library":
extra_data.plugin_type = PluginType.HIDDEN
if (
extra_data.plugin_type == PluginType.HIDDEN
and extra_data.plugin_type != "功能"
extra_data.plugin_type
== PluginType.HIDDEN
# and extra_data.plugin_type != "功能"
):
extra_data.menu_type = ""
plugin_list.append(
Expand Down Expand Up @@ -119,15 +120,26 @@ async def _():
create_list.append(plugin)
else:
plugin.id = module2id[plugin.module_path]
await plugin.save(
update_fields=[
"name",
"author",
"version",
"admin_level",
"plugin_type",
]
)
update_list.append(plugin)
if create_list:
await PluginInfo.bulk_create(create_list, 10)
if update_list:
await PluginInfo.bulk_update(
update_list,
["name", "author", "version", "admin_level"],
10,
)
# TODO: 批量更新无法更新plugin_type: tortoise.exceptions.OperationalError: column "superuser" does not exist
pass
# await PluginInfo.bulk_update(
# update_list,
# ["name", "author", "version", "admin_level", "plugin_type"],
# 10,
# )
if limit_list:
limit_create = []
plugins = []
Expand Down
7 changes: 1 addition & 6 deletions zhenxun/builtin_plugins/scripts.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
from asyncio.exceptions import TimeoutError

import nonebot
import ujson as json
from nonebot.drivers import Driver
from nonebot_plugin_apscheduler import scheduler

from zhenxun.configs.path_config import TEXT_PATH
from zhenxun.services.log import logger
from zhenxun.utils.http_utils import AsyncHttpx

try:
import ujson as json
except ModuleNotFoundError:
import json


driver: Driver = nonebot.get_driver()


Expand Down
2 changes: 1 addition & 1 deletion zhenxun/plugins/about.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from zhenxun.utils.message import MessageUtils

__plugin_meta__ = PluginMetadata(
name="识番",
name="关于",
description="想要更加了解真寻吗",
usage="""
指令:
Expand Down
3 changes: 2 additions & 1 deletion zhenxun/plugins/black_word/black_watch.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
extra=PluginExtraData(
author="HibiKier",
version="0.1",
plugin_type=PluginType.HIDDEN,
menu_type="其他",
plugin_type=PluginType.DEPENDANT,
).dict(),
)

Expand Down
2 changes: 1 addition & 1 deletion zhenxun/plugins/fudu.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
author="HibiKier",
version="0.1",
menu_type="其他",
plugin_type=PluginType.HIDDEN,
plugin_type=PluginType.DEPENDANT,
tasks=[Task(module="fudu", name="复读")],
configs=[
RegisterConfig(
Expand Down
5 changes: 4 additions & 1 deletion zhenxun/plugins/mute/mute_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from zhenxun.configs.config import NICKNAME
from zhenxun.configs.utils import PluginExtraData
from zhenxun.models.ban_console import BanConsole
from zhenxun.services.log import logger
from zhenxun.utils.enum import PluginType
from zhenxun.utils.image_utils import get_download_image_hash
Expand All @@ -23,7 +24,7 @@
author="HibiKier",
version="0.1",
menu_type="其他",
plugin_type=PluginType.HIDDEN,
plugin_type=PluginType.DEPENDANT,
).dict(),
)

Expand All @@ -35,6 +36,8 @@ async def _(bot: Bot, session: EventSession, message: UniMsg):
group_id = session.id2
if not session.id1 or not group_id:
return
if await BanConsole.is_ban(session.id1, group_id):
return
plain_text = message.extract_plain_text()
image_list = [m.url for m in message if isinstance(m, alcImage) and m.url]
img_hash = ""
Expand Down
3 changes: 2 additions & 1 deletion zhenxun/plugins/parse_bilibili/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
extra=PluginExtraData(
author="leekooyo",
version="0.1",
plugin_type=PluginType.HIDDEN,
plugin_type=PluginType.DEPENDANT,
menu_type="其他",
configs=[
RegisterConfig(
module="_task",
Expand Down
2 changes: 1 addition & 1 deletion zhenxun/plugins/poke/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
author="HibiKier",
version="0.1",
menu_type="其他",
plugin_type=PluginType.HIDDEN,
plugin_type=PluginType.DEPENDANT,
).dict(),
)

Expand Down
2 changes: 1 addition & 1 deletion zhenxun/plugins/web_ui/api/tabs/plugin_manage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ async def _() -> Result:
load_status=True,
).count()
plugin_count.other = await DbPluginInfo.filter(
plugin_type=PluginType.HIDDEN, load_status=True
plugin_type__in=[PluginType.HIDDEN, PluginType.DEPENDANT], load_status=True
).count()
return Result.ok(plugin_count)

Expand Down
5 changes: 3 additions & 2 deletions zhenxun/plugins/word_bank/_data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ async def show_word(
problem, _problem_list = await WordBank.get_problem_all_answer(
problem, # type: ignore
index,
group_id if group_id is None else None,
group_id if group_id is not None else None,
word_scope,
)
if not _problem_list:
Expand All @@ -248,7 +248,8 @@ async def show_word(
# __text = (m.data["image"], 30, 30)
__text = "[图片]"
_text.append(__text)
msg_list.append("".join(str(_text)))
_text = "".join(_text)
msg_list.append(_text)
column_name = ["序号", "回答内容"]
data_list = []
for index, msg in enumerate(msg_list):
Expand Down
4 changes: 2 additions & 2 deletions zhenxun/plugins/word_bank/message_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
description="",
usage="""""",
extra=PluginExtraData(
author="HibiKier", version="0.1", plugin_type=PluginType.HIDDEN
author="HibiKier", version="0.1", plugin_type=PluginType.DEPENDANT
).dict(),
)

Expand All @@ -28,4 +28,4 @@ async def _(session: EventSession, state: T_State):
gid = session.id3 or session.id2
if result := await WordBank.get_answer(gid, problem):
await result.send()
logger.info(f" 触发词条 {problem}", "词条检测", session=session)
logger.info(f"触发词条 {problem}", "词条检测", session=session)
6 changes: 5 additions & 1 deletion zhenxun/plugins/word_bank/word_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,4 +322,8 @@ async def _(
None, index.result if index.available else None, group_id, word_scope
)
await result.send()
logger.info(f"查看词条回答: {problem}", arparma.header_result, session=session)
logger.info(
f"查看词条回答: {problem.result if problem.available else index.result}",
arparma.header_result,
session=session,
)
7 changes: 7 additions & 0 deletions zhenxun/utils/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,17 @@ class PluginType(StrEnum):
"""

SUPERUSER = "SUPERUSER"
"""超级用户"""
ADMIN = "ADMIN"
"""管理员"""
SUPER_AND_ADMIN = "ADMIN_SUPER"
"""管理员以及超级用户"""
NORMAL = "NORMAL"
"""普通插件"""
DEPENDANT = "DEPENDANT"
"""依赖插件,一般为没有主动触发命令的插件,受权限控制"""
HIDDEN = "HIDDEN"
"""隐藏插件,一般为没有主动触发命令的插件,不受权限控制,如消息统计"""


class BlockType(StrEnum):
Expand Down

0 comments on commit 862c977

Please sign in to comment.