feat: 插件管理添加统一开关命令

This commit is contained in:
HibiKier 2024-07-18 23:16:29 +08:00
parent 15aba0bea9
commit 4b48fc2557
9 changed files with 359 additions and 34 deletions

103
poetry.lock generated
View File

@ -1,4 +1,4 @@
# This file is automatically @generated by Poetry 1.7.1 and should not be changed by hand.
# This file is automatically @generated by Poetry 1.8.3 and should not be changed by hand.
[[package]]
name = "aiofiles"
@ -309,6 +309,57 @@ type = "legacy"
url = "https://mirrors.aliyun.com/pypi/simple"
reference = "ali"
[[package]]
name = "black"
version = "24.4.2"
description = "The uncompromising code formatter."
optional = false
python-versions = ">=3.8"
files = [
{file = "black-24.4.2-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:dd1b5a14e417189db4c7b64a6540f31730713d173f0b63e55fabd52d61d8fdce"},
{file = "black-24.4.2-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:8e537d281831ad0e71007dcdcbe50a71470b978c453fa41ce77186bbe0ed6021"},
{file = "black-24.4.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:eaea3008c281f1038edb473c1aa8ed8143a5535ff18f978a318f10302b254063"},
{file = "black-24.4.2-cp310-cp310-win_amd64.whl", hash = "sha256:7768a0dbf16a39aa5e9a3ded568bb545c8c2727396d063bbaf847df05b08cd96"},
{file = "black-24.4.2-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:257d724c2c9b1660f353b36c802ccece186a30accc7742c176d29c146df6e474"},
{file = "black-24.4.2-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:bdde6f877a18f24844e381d45e9947a49e97933573ac9d4345399be37621e26c"},
{file = "black-24.4.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:e151054aa00bad1f4e1f04919542885f89f5f7d086b8a59e5000e6c616896ffb"},
{file = "black-24.4.2-cp311-cp311-win_amd64.whl", hash = "sha256:7e122b1c4fb252fd85df3ca93578732b4749d9be076593076ef4d07a0233c3e1"},
{file = "black-24.4.2-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:accf49e151c8ed2c0cdc528691838afd217c50412534e876a19270fea1e28e2d"},
{file = "black-24.4.2-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:88c57dc656038f1ab9f92b3eb5335ee9b021412feaa46330d5eba4e51fe49b04"},
{file = "black-24.4.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:be8bef99eb46d5021bf053114442914baeb3649a89dc5f3a555c88737e5e98fc"},
{file = "black-24.4.2-cp312-cp312-win_amd64.whl", hash = "sha256:415e686e87dbbe6f4cd5ef0fbf764af7b89f9057b97c908742b6008cc554b9c0"},
{file = "black-24.4.2-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:bf10f7310db693bb62692609b397e8d67257c55f949abde4c67f9cc574492cc7"},
{file = "black-24.4.2-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:98e123f1d5cfd42f886624d84464f7756f60ff6eab89ae845210631714f6db94"},
{file = "black-24.4.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:48a85f2cb5e6799a9ef05347b476cce6c182d6c71ee36925a6c194d074336ef8"},
{file = "black-24.4.2-cp38-cp38-win_amd64.whl", hash = "sha256:b1530ae42e9d6d5b670a34db49a94115a64596bc77710b1d05e9801e62ca0a7c"},
{file = "black-24.4.2-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:37aae07b029fa0174d39daf02748b379399b909652a806e5708199bd93899da1"},
{file = "black-24.4.2-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:da33a1a5e49c4122ccdfd56cd021ff1ebc4a1ec4e2d01594fef9b6f267a9e741"},
{file = "black-24.4.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:ef703f83fc32e131e9bcc0a5094cfe85599e7109f896fe8bc96cc402f3eb4b6e"},
{file = "black-24.4.2-cp39-cp39-win_amd64.whl", hash = "sha256:b9176b9832e84308818a99a561e90aa479e73c523b3f77afd07913380ae2eab7"},
{file = "black-24.4.2-py3-none-any.whl", hash = "sha256:d36ed1124bb81b32f8614555b34cc4259c3fbc7eec17870e8ff8ded335b58d8c"},
{file = "black-24.4.2.tar.gz", hash = "sha256:c872b53057f000085da66a19c55d68f6f8ddcac2642392ad3a355878406fbd4d"},
]
[package.dependencies]
click = ">=8.0.0"
mypy-extensions = ">=0.4.3"
packaging = ">=22.0"
pathspec = ">=0.9.0"
platformdirs = ">=2"
tomli = {version = ">=1.1.0", markers = "python_version < \"3.11\""}
typing-extensions = {version = ">=4.0.1", markers = "python_version < \"3.11\""}
[package.extras]
colorama = ["colorama (>=0.4.3)"]
d = ["aiohttp (>=3.7.4)", "aiohttp (>=3.7.4,!=3.9.0)"]
jupyter = ["ipython (>=7.8.0)", "tokenize-rt (>=3.2.0)"]
uvloop = ["uvloop (>=0.15.2)"]
[package.source]
type = "legacy"
url = "https://mirrors.aliyun.com/pypi/simple"
reference = "ali"
[[package]]
name = "cachetools"
version = "5.3.2"
@ -1388,6 +1439,22 @@ type = "legacy"
url = "https://mirrors.aliyun.com/pypi/simple"
reference = "ali"
[[package]]
name = "mypy-extensions"
version = "1.0.0"
description = "Type system extensions for programs checked with the mypy type checker."
optional = false
python-versions = ">=3.5"
files = [
{file = "mypy_extensions-1.0.0-py3-none-any.whl", hash = "sha256:4392f6c0eb8a5668a69e23d168ffa70f0be9ccfd32b5cc2d26a34ae5b844552d"},
{file = "mypy_extensions-1.0.0.tar.gz", hash = "sha256:75dbf8955dc00442a438fc4d0666508a9a97b6bd41aa2f0ffe9d2f2725af0782"},
]
[package.source]
type = "legacy"
url = "https://mirrors.aliyun.com/pypi/simple"
reference = "ali"
[[package]]
name = "nb-cli"
version = "1.3.0"
@ -1785,6 +1852,38 @@ type = "legacy"
url = "https://mirrors.aliyun.com/pypi/simple"
reference = "ali"
[[package]]
name = "packaging"
version = "24.1"
description = "Core utilities for Python packages"
optional = false
python-versions = ">=3.8"
files = [
{file = "packaging-24.1-py3-none-any.whl", hash = "sha256:5b8f2217dbdbd2f7f384c41c628544e6d52f2d0f53c6d0c3ea61aa5d1d7ff124"},
{file = "packaging-24.1.tar.gz", hash = "sha256:026ed72c8ed3fcce5bf8950572258698927fd1dbda10a5e981cdf0ac37f4f002"},
]
[package.source]
type = "legacy"
url = "https://mirrors.aliyun.com/pypi/simple"
reference = "ali"
[[package]]
name = "pathspec"
version = "0.12.1"
description = "Utility library for gitignore style pattern matching of file paths."
optional = false
python-versions = ">=3.8"
files = [
{file = "pathspec-0.12.1-py3-none-any.whl", hash = "sha256:a0d503e138a4c123b27490a4f7beda6a01c6f288df0e4a8b79c7eb0dc7b4cc08"},
{file = "pathspec-0.12.1.tar.gz", hash = "sha256:a482d51503a1ab33b1c67a6c3813a26953dbdc71c31dacaef9a838c4e29f5712"},
]
[package.source]
type = "legacy"
url = "https://mirrors.aliyun.com/pypi/simple"
reference = "ali"
[[package]]
name = "pillow"
version = "9.5.0"
@ -3403,4 +3502,4 @@ reference = "ali"
[metadata]
lock-version = "2.0"
python-versions = "^3.10"
content-hash = "c1da4a148819ff244d291be9ca67466a07d994f29d2c1821a2e640997bfe617c"
content-hash = "bb01964309a665f0348ca69fecf771a9c6f7d99147c47010c0e64d2d13fe25ad"

View File

@ -40,6 +40,7 @@ psutil = "^5.9.8"
feedparser = "^6.0.11"
opencv-python = "^4.9.0.80"
imagehash = "^4.3.1"
black = "^24.4.2"
[tool.poetry.dev-dependencies]

View File

@ -8,7 +8,6 @@ from nonebot_plugin_alconna import (
At,
Match,
Option,
Subcommand,
on_alconna,
store_true,
)

View File

@ -43,6 +43,12 @@ __plugin_meta__ = PluginMetadata(
插件列表
开启/关闭[功能名称] ?[-t ["private", "p", "group", "g"](关闭类型)] ?[-g 群组Id]
开启/关闭插件df[功能名称]: 开启/关闭指定插件进群默认状态
开启/关闭所有插件df: 开启/关闭所有插件进群默认状态
开启/关闭所有插件:
私聊中: 开启/关闭所有插件全局状态
群组中: 开启/关闭当前群组所有插件状态
私聊下:
示例:
开启签到 : 全局开启签到
@ -90,19 +96,69 @@ async def _(
bot: Bot,
session: EventSession,
arparma: Arparma,
name: str,
plugin_name: Match[str],
group: Match[str],
task: Query[bool] = AlconnaQuery("task.value", False),
default_status: Query[bool] = AlconnaQuery("default.value", False),
all: Query[bool] = AlconnaQuery("all.value", False),
):
if gid := session.id3 or session.id2:
if not all.result and not plugin_name.available:
await Text("请输入功能名称").finish(reply=True)
name = plugin_name.result
gid = session.id3 or session.id2
if gid:
"""群组中使用命令"""
if task.result:
result = await PluginManage.unblock_group_task(name, gid)
logger.info(f"开启群组被动 {name}", arparma.header_result, session=session)
else:
result = await PluginManage.block_group_plugin(name, gid)
if session.id1 in bot.config.superusers and (
all.result or default_status.result
):
if all.result:
"""所有插件"""
result = await PluginManage.set_all_plugin_status(
True, default_status.result, gid
)
logger.info(
f"超级用户开启群组中全部功能",
arparma.header_result,
session=session,
)
else:
"""单个插件的进群默认修改"""
result = await PluginManage.set_default_status(name, True)
logger.info(
f"超级用户开启 {name} 功能进群默认开关",
arparma.header_result,
session=session,
)
else:
result = await PluginManage.block_group_plugin(name, gid)
logger.info(f"开启功能 {name}", arparma.header_result, session=session)
await Text(result).send(reply=True)
logger.info(f"开启功能 {name}", arparma.header_result, session=session)
elif session.id1 in bot.config.superusers:
"""私聊"""
group_id = group.result if group.available else None
if all.result:
result = await PluginManage.set_all_plugin_status(
True, default_status.result, group_id
)
logger.info(
f"超级用户开启全部功能全局开关 {f'指定群组: {group_id}' if group_id else ''}",
arparma.header_result,
session=session,
)
await Text(result).finish(reply=True)
if default_status.result:
result = await PluginManage.set_default_status(name, True)
logger.info(
f"超级用户开启 {name} 功能进群默认开关",
arparma.header_result,
session=session,
target=group_id,
)
await Text(result).finish(reply=True)
if task.result:
result = await PluginManage.superuser_task_handle(name, group_id, True)
await Text(result).send(reply=True)
@ -128,20 +184,67 @@ async def _(
bot: Bot,
session: EventSession,
arparma: Arparma,
name: str,
plugin_name: Match[str],
block_type: Match[str],
group: Match[str],
task: Query[bool] = AlconnaQuery("task.value", False),
default_status: Query[bool] = AlconnaQuery("default.value", False),
all: Query[bool] = AlconnaQuery("all.value", False),
):
if gid := session.id3 or session.id2:
if not all.result and not plugin_name.available:
await Text("请输入功能名称").finish(reply=True)
name = plugin_name.result
gid = session.id3 or session.id2
if gid:
if task.result:
result = await PluginManage.block_group_task(name, gid)
else:
result = await PluginManage.unblock_group_plugin(name, gid)
if session.id1 in bot.config.superusers and (
all.result or default_status.result
):
if all.result:
"""所有插件"""
result = await PluginManage.set_all_plugin_status(
False, default_status.result, gid
)
logger.info(
f"超级用户开启群组中全部功能",
arparma.header_result,
session=session,
)
else:
"""单个插件的进群默认修改"""
result = await PluginManage.set_default_status(name, False)
logger.info(
f"超级用户开启 {name} 功能进群默认开关",
arparma.header_result,
session=session,
)
else:
result = await PluginManage.unblock_group_plugin(name, gid)
logger.info(f"关闭功能 {name}", arparma.header_result, session=session)
await Text(result).send(reply=True)
logger.info(f"关闭功能 {name}", arparma.header_result, session=session)
elif session.id1 in bot.config.superusers:
group_id = group.result if group.available else None
if all.result:
result = await PluginManage.set_all_plugin_status(
False, default_status.result, group_id
)
logger.info(
f"超级用户关闭全部功能全局开关 {f'指定群组: {group_id}' if group_id else ''}",
arparma.header_result,
session=session,
)
await Text(result).finish(reply=True)
if default_status.result:
result = await PluginManage.set_default_status(name, False)
logger.info(
f"超级用户关闭 {name} 功能进群默认开关",
arparma.header_result,
session=session,
target=group_id,
)
await Text(result).finish(reply=True)
if task.result:
result = await PluginManage.superuser_task_handle(name, group_id, False)
await Text(result).send(reply=True)

View File

@ -145,8 +145,75 @@ async def build_task(group_id: str | None) -> BuildImage:
class PluginManage:
@classmethod
async def set_default_status(cls, plugin_name: str, status: bool) -> str:
"""设置插件进群默认状态
参数:
plugin_name: 插件名称
status: 状态
返回:
str: 返回信息
"""
if plugin_name.isdigit():
plugin = await PluginInfo.get_or_none(id=int(plugin_name))
else:
plugin = await PluginInfo.get_or_none(name=plugin_name)
if plugin:
plugin.default_status = status
await plugin.save(update_fields=["default_status"])
return f'成功将 {plugin.name} 进群默认状态修改为: {"开启" if status else "关闭"}'
return f"没有找到这个功能喔..."
@classmethod
async def set_all_plugin_status(
cls, status: bool, is_default: bool = False, group_id: str | None = None
) -> str:
"""修改所有插件状态
参数:
status: 状态
is_default: 是否进群默认.
group_id: 指定群组id.
返回:
str: 返回信息
"""
if is_default:
await PluginInfo.filter(plugin_type=PluginType.NORMAL).update(
default_status=status
)
return f'成功将所有功能进群默认状态修改为: {"开启" if status else "关闭"}'
if group_id:
if group := await GroupConsole.get_or_none(
group_id=group_id, channel_id__isnull=True
):
if status:
group.block_plugin = ""
else:
module_list = await PluginInfo.filter(
plugin_type=PluginType.NORMAL
).values_list("module", flat=True)
group.block_plugin = ",".join(module_list) + "," # type: ignore
await group.save(update_fields=["block_plugin"])
return f'成功将此群组所有功能状态修改为: {"开启" if status else "关闭"}'
return "获取群组失败..."
await PluginInfo.filter(plugin_type=PluginType.NORMAL).update(
status=status, block_type=BlockType.ALL if not status else None
)
return f'成功将所有功能全局状态修改为: {"开启" if status else "关闭"}'
@classmethod
async def is_wake(cls, group_id: str) -> bool:
"""是否醒来
参数:
group_id: 群组id
返回:
bool: 是否醒来
"""
if c := await GroupConsole.get_or_none(
group_id=group_id, channel_id__isnull=True
):
@ -155,22 +222,42 @@ class PluginManage:
@classmethod
async def sleep(cls, group_id: str):
"""休眠
参数:
group_id: 群组id
"""
await GroupConsole.filter(group_id=group_id, channel_id__isnull=True).update(
status=False
)
@classmethod
async def wake(cls, group_id: str):
"""醒来
参数:
group_id: 群组id
"""
await GroupConsole.filter(group_id=group_id, channel_id__isnull=True).update(
status=True
)
@classmethod
async def block(cls, module: str):
"""禁用
参数:
module: 模块名
"""
await PluginInfo.filter(module=module).update(status=False)
@classmethod
async def unblock(cls, module: str):
"""启用
参数:
module: 模块名
"""
await PluginInfo.filter(module=module).update(status=True)
@classmethod

View File

@ -14,9 +14,11 @@ _status_matcher = on_alconna(
Alconna(
"switch",
Option("-t|--task", action=store_true, help_text="被动技能"),
Option("-df|--default", action=store_true, help_text="进群默认开关"),
Option("--all", action=store_true, help_text="全部插件"),
Subcommand(
"open",
Args["name", [str, int]],
Args["plugin_name?", [str, int]],
Option(
"-g|--group",
Args["group", str],
@ -24,7 +26,7 @@ _status_matcher = on_alconna(
),
Subcommand(
"close",
Args["name", [str, int]],
Args["plugin_name?", [str, int]],
Option(
"-t|--type",
Args["block_type", ["all", "a", "private", "p", "group", "g"]],
@ -72,6 +74,27 @@ _status_matcher.shortcut(
)
_status_matcher.shortcut(
r"开启所有插件",
command="switch",
arguments=["open", "s", "--all"],
prefix=True,
)
_status_matcher.shortcut(
r"开启所有插件df",
command="switch",
arguments=["open", "s", "-df", "--all"],
prefix=True,
)
_status_matcher.shortcut(
r"开启插件df(?P<name>.+)",
command="switch",
arguments=["open", "{name}", "-df"],
prefix=True,
)
_status_matcher.shortcut(
r"开启(?P<name>.+)",
command="switch",
@ -79,6 +102,7 @@ _status_matcher.shortcut(
prefix=True,
)
_status_matcher.shortcut(
r"关闭群被动(?P<name>.+)",
command="switch",
@ -86,6 +110,28 @@ _status_matcher.shortcut(
prefix=True,
)
_status_matcher.shortcut(
r"关闭所有插件",
command="switch",
arguments=["close", "s", "--all"],
prefix=True,
)
_status_matcher.shortcut(
r"关闭所有插件df",
command="switch",
arguments=["close", "s", "-df", "--all"],
prefix=True,
)
_status_matcher.shortcut(
r"关闭插件df(?P<name>.+)",
command="switch",
arguments=["close", "{name}", "-df"],
prefix=True,
)
_status_matcher.shortcut(
r"关闭(?P<name>.+)",
command="switch",
@ -93,7 +139,6 @@ _status_matcher.shortcut(
prefix=True,
)
_group_status_matcher.shortcut(
r"醒来",
command="group-status",

View File

@ -18,6 +18,7 @@ from zhenxun.models.ban_console import BanConsole
from zhenxun.models.friend_user import FriendUser
from zhenxun.models.group_member_info import GroupInfoUser
from zhenxun.services.log import logger
from zhenxun.utils.depends import UserName
from zhenxun.utils.enum import PluginType
__plugin_meta__ = PluginMetadata(
@ -177,7 +178,7 @@ async def _(
@_global_nickname_matcher.handle(parameterless=[CheckNickname()])
async def _(
session: EventSession,
user_info: UserInfo = EventUserInfo(),
nickname: str = UserName(),
reg_group: tuple[Any, ...] = RegexGroup(),
):
if session.id1:
@ -185,7 +186,7 @@ async def _(
await FriendUser.set_user_nickname(
session.id1,
name,
user_info.user_displayname or user_info.user_remark or user_info.user_name,
nickname,
session.platform,
)
await GroupInfoUser.filter(user_id=session.id1).update(nickname=name)

View File

@ -14,6 +14,7 @@ from nonebot_plugin_userinfo import EventUserInfo, UserInfo
from zhenxun.configs.utils import PluginCdBlock, PluginExtraData, RegisterConfig
from zhenxun.services.log import logger
from zhenxun.utils.depends import UserName
from ._data_source import SignManage
from .goods_register import driver
@ -106,29 +107,23 @@ _sign_matcher.shortcut(
@_sign_matcher.assign("$main")
async def _(
session: EventSession, arparma: Arparma, user_info: UserInfo = EventUserInfo()
session: EventSession, arparma: Arparma, nickname: str = UserName()
):
nickname = (
user_info.user_displayname or user_info.user_remark or user_info.user_name
)
if session.id1:
if path := await SignManage.sign(session, nickname):
logger.info("签到成功", arparma.header_result, session=session)
await Image(path).finish(reply=True)
await Image(path).finish()
return Text("用户id为空...").send()
@_sign_matcher.assign("my")
async def _(
session: EventSession, arparma: Arparma, user_info: UserInfo = EventUserInfo()
session: EventSession, arparma: Arparma, nickname: str = UserName()
):
nickname = (
user_info.user_displayname or user_info.user_remark or user_info.user_name
)
if session.id1:
if image := await SignManage.sign(session, nickname, True):
logger.info("查看我的签到", arparma.header_result, session=session)
await Image(image).finish(reply=True)
await Image(image).finish()
return Text("用户id为空...").send()
@ -137,11 +132,8 @@ async def _(
session: EventSession,
arparma: Arparma,
num: int,
user_info: UserInfo = EventUserInfo(),
nickname: str = UserName()
):
nickname = (
user_info.user_displayname or user_info.user_remark or user_info.user_name
)
if session.id1:
if image := await SignManage.rank(session.id1, num):
logger.info("查看签到排行", arparma.header_result, session=session)

View File

@ -11,6 +11,7 @@ from nonebot_plugin_userinfo import EventUserInfo, UserInfo
from zhenxun.configs.config import NICKNAME
from zhenxun.configs.utils import PluginExtraData
from zhenxun.services.log import logger
from zhenxun.utils.depends import UserName
__plugin_meta__ = PluginMetadata(
name="roll",
@ -34,14 +35,11 @@ _matcher = on_command("roll", priority=5, block=True)
async def _(
session: EventSession,
message: UniMsg,
user_info: UserInfo = EventUserInfo(),
user_name: str = UserName(),
):
text = message.extract_plain_text().strip().replace("roll", "", 1).split()
if not text:
await Text(f"roll: {random.randint(0, 100)}").finish(reply=True)
user_name = (
user_info.user_displayname or user_info.user_remark or user_info.user_name
)
await Text(
random.choice(
[