zhenxun_bot/zhenxun/builtin_plugins/superuser/group_manage.py
HibiKier 8649aaaa54
引入缓存机制 (#1889)
* 添加全局cache

*  构建缓存,hook使用缓存

*  新增数据库Model方法监控

*  数据库添加semaphore锁

* 🩹 优化webapi返回数据

*  添加增量缓存与缓存过期

* 🎨 优化检测代码结构

*  优化hook权限检测性能

* 🐛 添加新异常判断跳过权限检测

*  添加插件limit缓存

* 🎨 代码格式优化

* 🐛  修复代码导入

* 🐛 修复刷新时检查

* 👽 Rename exception for missing database URL in initialization

*  Update default database URL to SQLite in configuration

* 🔧 Update tortoise-orm and aiocache dependencies restrictions; add optional redis and asyncpg support

* 🐛 修复ban检测

* 🐛 修复所有插件关闭时缓存更新

* 🐛 尝试迁移至aiocache

* 🐛 完善aiocache缓存

*  代码性能优化

* 🐛 移除获取封禁缓存时的日志记录

* 🐛 修复缓存类型声明,优化封禁用户处理逻辑

* 🐛 优化LevelUser权限更新逻辑及数据库迁移

*  cache支持redis连接

* 🚨 auto fix by pre-commit hooks

*  :增强获取群组的安全性和准确性。同时,优化了缓存管理中的相关逻辑,确保缓存操作的一致性。

*  feat(auth_limit): 将插件初始化逻辑的启动装饰器更改为优先级管理器

* 🔧 修复日志记录级别

* 🔧 更新数据库连接字符串

* 🔧 更新数据库连接字符串为内存数据库,并优化权限检查逻辑

*  feat(cache): 增加缓存功能配置项,并新增数据访问层以支持缓存逻辑

* ♻️ 重构cache

*  feat(cache): 增强缓存管理,新增缓存字典和缓存列表功能,支持过期时间管理

* 🔧 修复Notebook类中的viewport高度设置,将其从1000调整为10

*  更新插件管理逻辑,替换缓存服务为CacheRoot并优化缓存失效处理

*  更新RegisterConfig类中的type字段

*  修复清理重复记录逻辑,确保检查记录的id属性有效性

*  超级无敌大优化,解决延迟与卡死问题

*  更新封禁功能,增加封禁时长参数和描述,优化插件信息返回结构

*  更新zhenxun_help.py中的viewport高度,将其从453调整为10,以优化页面显示效果

*  优化插件分类逻辑,增加插件ID排序,并更新插件信息返回结构

---------

Co-authored-by: BalconyJH <balconyjh@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2025-07-14 22:35:29 +08:00

214 lines
7.1 KiB
Python

from nonebot.adapters import Bot
from nonebot.adapters.onebot.v11 import Bot as v11Bot
from nonebot.params import Depends
from nonebot.permission import SUPERUSER
from nonebot.plugin import PluginMetadata
from nonebot.typing import T_State
from nonebot_plugin_alconna import (
Alconna,
Args,
Arparma,
Match,
Option,
Subcommand,
on_alconna,
store_true,
)
from nonebot_plugin_session import EventSession
from zhenxun.configs.config import BotConfig
from zhenxun.configs.utils import PluginExtraData
from zhenxun.models.group_console import GroupConsole
from zhenxun.services.log import logger
from zhenxun.utils.enum import PluginType
from zhenxun.utils.message import MessageUtils
__plugin_meta__ = PluginMetadata(
name="管理群操作",
description="管理群操作",
usage="""
群权限 | 群白名单 | 退出群 操作
退群,添加/删除群白名单,添加/删除群认证,当在群组中这五个命令且没有指定群号时,默认指定当前群组
指令:
格式:
group-manage modify-level [权限等级] ?[群组Id] : 修改群权限
group-manage super-handle [群组Id] [--del 删除操作] : 添加/删除群白名单
group-manage auth-handle [群组Id] [--del 删除操作] : 添加/删除群认证
group-manage del-group [群组Id] : 退出指定群
快捷:
group-manage modify-level : 修改群权限
group-manage super-handle : 添加/删除群白名单
group-manage auth-handle : 添加/删除群认证
group-manage del-group : 退群
示例:
修改群权限 7 : 在群组中修改当前群组权限为7
修改群权限 7 1234556 : 修改 123456 群组的权限等级为7
添加/删除群白名单 1234567 : 添加/删除 1234567 为群白名单
添加/删除群认证 1234567 : 添加/删除 1234567 为群认证
退群 12344566 : 退出指定群组
""".strip(),
extra=PluginExtraData(
author="HibiKier",
version="0.1",
plugin_type=PluginType.SUPERUSER,
).to_dict(),
)
_matcher = on_alconna(
Alconna(
"group-manage",
Option("--delete", action=store_true, help_text="删除"),
Subcommand(
"modify-level", Args["level", int]["group_id?", int], help_text="修改群权限"
),
Subcommand(
"super-handle",
Args["group_id", int],
help_text="添加/删除群白名单",
),
Subcommand(
"auth-handle",
Args["group_id", int],
help_text="添加/删除群认证",
),
Subcommand("del-group", Args["group_id", int], help_text="退出群组"),
),
permission=SUPERUSER,
priority=1,
block=True,
)
_matcher.shortcut(
r"修改群权限\s?(?P<level>-?\d+)\s?(?P<group_id>\d+)?",
command="group-manage",
arguments=["modify-level", "{level}", "{group_id}"],
prefix=True,
)
_matcher.shortcut(
"添加群白名单",
command="group-manage",
arguments=["super-handle", "{%0}"],
prefix=True,
)
_matcher.shortcut(
"删除群白名单",
command="group-manage",
arguments=["super-handle", "{%0}", "--delete"],
prefix=True,
)
_matcher.shortcut(
"添加群认证",
command="group-manage",
arguments=["auth-handle", "{%0}"],
prefix=True,
)
_matcher.shortcut(
"删除群认证",
command="group-manage",
arguments=["auth-handle", "{%0}", "--delete"],
prefix=True,
)
_matcher.shortcut(
"退群",
command="group-manage",
arguments=["del-group", "{%0}"],
prefix=True,
)
def CheckGroupId():
"""
检测群组id
"""
async def dependency(
session: EventSession,
group_id: Match[int],
state: T_State,
):
gid = session.id3 or session.id2
if group_id.available:
gid = group_id.result
if not gid:
await MessageUtils.build_message("群组id不能为空...").finish()
state["group_id"] = gid
return Depends(dependency)
@_matcher.assign("modify-level", parameterless=[CheckGroupId()])
async def _(session: EventSession, arparma: Arparma, state: T_State, level: int):
gid = state["group_id"]
group, _ = await GroupConsole.get_or_create(group_id=gid)
old_level = group.level
group.level = level
await group.save(update_fields=["level"])
await MessageUtils.build_message("群权限修改成功!").send(reply_to=True)
logger.info(
f"修改群权限: {old_level} -> {level}",
arparma.header_result,
session=session,
target=gid,
)
@_matcher.assign("super-handle", parameterless=[CheckGroupId()])
async def _(session: EventSession, arparma: Arparma, state: T_State):
gid = state["group_id"]
group = await GroupConsole.get_group(group_id=gid)
if not group:
await MessageUtils.build_message("群组信息不存在, 请更新群组信息...").finish()
s = "删除" if arparma.find("delete") else "添加"
group.is_super = not arparma.find("delete")
await group.save(update_fields=["is_super"])
await MessageUtils.build_message(f"{s}群白名单成功!").send(reply_to=True)
logger.info(f"{s}群白名单", arparma.header_result, session=session, target=gid)
@_matcher.assign("auth-handle", parameterless=[CheckGroupId()])
async def _(session: EventSession, arparma: Arparma, state: T_State):
gid = state["group_id"]
await GroupConsole.update_or_create(
group_id=gid,
channel_id__isnull=True,
defaults={"group_flag": 0 if arparma.find("delete") else 1},
)
s = "删除" if arparma.find("delete") else "添加"
await MessageUtils.build_message(f"{s}群认证成功!").send(reply_to=True)
logger.info(f"{s}群白名单", arparma.header_result, session=session, target=gid)
@_matcher.assign("del-group")
async def _(bot: Bot, session: EventSession, arparma: Arparma, group_id: int):
if isinstance(bot, v11Bot):
group_list = [g["group_id"] for g in await bot.get_group_list()]
if group_id not in group_list:
logger.debug("群组不存在", "退群", session=session, target=group_id)
await MessageUtils.build_message(
f"{BotConfig.self_nickname}未在该群组中..."
).finish()
try:
await bot.set_group_leave(group_id=group_id)
logger.info(
f"{BotConfig.self_nickname}退出群组成功",
"退群",
session=session,
target=group_id,
)
await MessageUtils.build_message(f"退出群组 {group_id} 成功!").send()
await GroupConsole.filter(group_id=group_id).delete()
except Exception as e:
logger.error("退出群组失败", "退群", session=session, target=group_id, e=e)
await MessageUtils.build_message(f"退出群组 {group_id} 失败...").send()
else:
# TODO: 其他平台的退群操作
await MessageUtils.build_message("暂未支持退群操作...").send()