mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-09 22:00:01 +08:00
init🎉: 首次提交
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
from pathlib import Path
|
||||
|
||||
import nonebot
|
||||
|
||||
nonebot.load_plugins(str(Path(__file__).parent.resolve()))
|
||||
@@ -0,0 +1,86 @@
|
||||
import os
|
||||
import time
|
||||
|
||||
from nonebot.permission import SUPERUSER
|
||||
from nonebot.plugin import PluginMetadata
|
||||
from nonebot.rule import to_me
|
||||
from nonebot.utils import run_sync
|
||||
from nonebot_plugin_alconna import Alconna, on_alconna
|
||||
from nonebot_plugin_apscheduler import scheduler
|
||||
from nonebot_plugin_saa import Text
|
||||
from nonebot_plugin_session import EventSession
|
||||
|
||||
from zhenxun.configs.path_config import TEMP_PATH
|
||||
from zhenxun.configs.utils import PluginExtraData
|
||||
from zhenxun.services.log import logger
|
||||
from zhenxun.utils.enum import PluginType
|
||||
from zhenxun.utils.utils import ResourceDirManager
|
||||
|
||||
__plugin_meta__ = PluginMetadata(
|
||||
name="清理数据",
|
||||
description="清理已添加的临时文件夹中的数据",
|
||||
usage="""
|
||||
清理临时数据
|
||||
""".strip(),
|
||||
extra=PluginExtraData(
|
||||
author="HibiKier",
|
||||
version="0.1",
|
||||
plugin_type=PluginType.SUPERUSER,
|
||||
).dict(),
|
||||
)
|
||||
|
||||
|
||||
_matcher = on_alconna(
|
||||
Alconna("清理临时数据"),
|
||||
rule=to_me(),
|
||||
permission=SUPERUSER,
|
||||
priority=5,
|
||||
block=True,
|
||||
)
|
||||
|
||||
|
||||
ResourceDirManager.add_temp_dir(TEMP_PATH, True)
|
||||
|
||||
|
||||
@_matcher.handle()
|
||||
async def _(session: EventSession):
|
||||
await Text("开始清理临时数据...").send()
|
||||
size = await _clear_data()
|
||||
await Text("共清理了 {:.2f}MB 的数据...".format(size / 1024 / 1024)).send()
|
||||
logger.info(
|
||||
"清理临时数据完成,共清理了 {:.2f}MB 的数据...".format(size / 1024 / 1024), session=session
|
||||
)
|
||||
|
||||
|
||||
@run_sync
|
||||
def _clear_data() -> float:
|
||||
logger.debug("开始清理临时文件...")
|
||||
size = 0
|
||||
dir_list = [dir_ for dir_ in ResourceDirManager.temp_path if dir_.exists()]
|
||||
for dir_ in dir_list:
|
||||
logger.debug(f"尝试清理文件夹: {dir_.absolute()}", "清理临时数据")
|
||||
dir_size = 0
|
||||
for file in os.listdir(dir_):
|
||||
file = dir_ / file
|
||||
if file.is_file():
|
||||
try:
|
||||
if time.time() - os.path.getatime(file) > 10:
|
||||
file_size = os.path.getsize(file)
|
||||
file.unlink()
|
||||
size += file_size
|
||||
dir_size += file_size
|
||||
logger.debug(f"移除临时文件: {file.absolute()}", "清理临时数据")
|
||||
except Exception as e:
|
||||
logger.error(f"清理临时数据错误,临时文件夹: {dir_.absolute()}...", "清理临时数据", e=e)
|
||||
logger.debug("清理临时文件夹大小: {:.2f}MB".format(size / 1024 / 1024), "清理临时数据")
|
||||
return float(size)
|
||||
|
||||
|
||||
@scheduler.scheduled_job(
|
||||
"cron",
|
||||
hour=1,
|
||||
minute=1,
|
||||
)
|
||||
async def _():
|
||||
size = await _clear_data()
|
||||
logger.info("自动清理临时数据完成,共清理了 {:.2f}MB 的数据...".format(size / 1024 / 1024), "定时任务")
|
||||
@@ -0,0 +1,78 @@
|
||||
from nonebot.permission import SUPERUSER
|
||||
from nonebot.plugin import PluginMetadata
|
||||
from nonebot.rule import to_me
|
||||
from nonebot_plugin_alconna import (
|
||||
Alconna,
|
||||
AlconnaQuery,
|
||||
Args,
|
||||
Arparma,
|
||||
Match,
|
||||
Option,
|
||||
Query,
|
||||
on_alconna,
|
||||
store_true,
|
||||
)
|
||||
from nonebot_plugin_saa import Text
|
||||
from nonebot_plugin_session import EventSession
|
||||
from tortoise import Tortoise
|
||||
|
||||
from zhenxun.configs.utils import PluginExtraData
|
||||
from zhenxun.services.db_context import TestSQL
|
||||
from zhenxun.services.log import logger
|
||||
from zhenxun.utils.enum import PluginType
|
||||
|
||||
__plugin_meta__ = PluginMetadata(
|
||||
name="数据库操作",
|
||||
description="执行sql语句与查看表",
|
||||
usage="""
|
||||
查看所有表
|
||||
exec [sql语句]
|
||||
""".strip(),
|
||||
extra=PluginExtraData(
|
||||
author="HibiKier",
|
||||
version="0.1",
|
||||
plugin_type=PluginType.SUPERUSER,
|
||||
).dict(),
|
||||
)
|
||||
|
||||
|
||||
_matcher = on_alconna(
|
||||
Alconna(
|
||||
"exec",
|
||||
Args["sql?", str],
|
||||
Option("-l|--list", action=store_true, help_text="查看数据表"),
|
||||
),
|
||||
rule=to_me(),
|
||||
permission=SUPERUSER,
|
||||
priority=1,
|
||||
block=True,
|
||||
)
|
||||
|
||||
|
||||
@_matcher.handle()
|
||||
async def _(
|
||||
sql: Match[str],
|
||||
session: EventSession,
|
||||
arparma: Arparma,
|
||||
query_list: Query[bool] = AlconnaQuery("list.value", False),
|
||||
):
|
||||
db = Tortoise.get_connection("default")
|
||||
if query_list.result:
|
||||
query = await db.execute_query_dict(
|
||||
"select tablename from pg_tables where schemaname = 'public'"
|
||||
)
|
||||
msg = "数据库中的所有表名:\n"
|
||||
for tablename in query:
|
||||
msg += str(tablename["tablename"]) + "\n"
|
||||
logger.info("查看数据库所有表", arparma.header_result, session=session)
|
||||
await Text(msg[:-1]).finish()
|
||||
else:
|
||||
if not sql.available:
|
||||
await Text("必须带有需要执行的 SQL 语句...").finish()
|
||||
sql_text = sql.result
|
||||
if not sql_text.lower().startswith("select"):
|
||||
await TestSQL.raw(sql_text)
|
||||
await Text("执行 SQL 语句成功!").finish()
|
||||
else:
|
||||
res = await db.execute_query_dict(sql_text)
|
||||
# TODO: Alconna空格sql无法接收
|
||||
@@ -0,0 +1,114 @@
|
||||
|
||||
|
||||
from nonebot.adapters import Bot
|
||||
from nonebot.adapters.kaiheila.exception import ApiNotAvailable
|
||||
from nonebot.permission import SUPERUSER
|
||||
from nonebot.plugin import PluginMetadata
|
||||
from nonebot.rule import to_me
|
||||
from nonebot_plugin_alconna import (
|
||||
Alconna,
|
||||
AlconnaMatch,
|
||||
Arparma,
|
||||
Match,
|
||||
Query,
|
||||
Subcommand,
|
||||
UniMessage,
|
||||
on_alconna,
|
||||
store_true,
|
||||
)
|
||||
from nonebot_plugin_alconna.matcher import AlconnaMatcher
|
||||
from nonebot_plugin_saa import Text
|
||||
from nonebot_plugin_session import EventSession
|
||||
|
||||
from zhenxun.configs.config import Config
|
||||
from zhenxun.configs.path_config import DATA_PATH
|
||||
from zhenxun.configs.utils import ConfigModel, PluginExtraData
|
||||
from zhenxun.services.log import logger
|
||||
from zhenxun.utils.enum import PluginType
|
||||
from zhenxun.utils.rules import admin_check, ensure_group
|
||||
|
||||
__plugin_meta__ = PluginMetadata(
|
||||
name="好友群组列表",
|
||||
description="查看好友群组列表以",
|
||||
usage="""
|
||||
查看所有好友
|
||||
查看所有群组
|
||||
""".strip(),
|
||||
extra=PluginExtraData(
|
||||
author="HibiKier",
|
||||
version="0.1",
|
||||
plugin_type=PluginType.SUPERUSER,
|
||||
).dict(),
|
||||
)
|
||||
|
||||
_friend_matcher = on_alconna(
|
||||
Alconna("好友列表"),
|
||||
rule=to_me(),
|
||||
permission=SUPERUSER,
|
||||
priority=1,
|
||||
block=True,
|
||||
)
|
||||
|
||||
_group_matcher = on_alconna(
|
||||
Alconna("群组列表"),
|
||||
rule=to_me(),
|
||||
permission=SUPERUSER,
|
||||
priority=1,
|
||||
block=True,
|
||||
)
|
||||
|
||||
# _friend_handle_matcher = on_alconna(
|
||||
# Alconna(
|
||||
# "好友操作",
|
||||
# Subcommand("delete", Args["uid", str], help_text="删除好友"),
|
||||
# Subcommand("send", Args["uid", str]["message", str], help_text="发送消息"),
|
||||
# )
|
||||
# )
|
||||
|
||||
# _group_handle_matcher = on_alconna(
|
||||
# Alconna(
|
||||
# "群组操作",
|
||||
# Subcommand("delete", Args["gid", str], help_text="删除好友"),
|
||||
# Subcommand("send", Args["gid", str]["message", str], help_text="发送消息"),
|
||||
# )
|
||||
# )
|
||||
|
||||
|
||||
@_friend_matcher.handle()
|
||||
async def _(
|
||||
bot: Bot,
|
||||
session: EventSession,
|
||||
):
|
||||
try:
|
||||
# TODO: 其他adapter的好友api
|
||||
fl = await bot.get_friend_list()
|
||||
msg = ["{user_id} {nickname}".format_map(g) for g in fl]
|
||||
msg = "\n".join(msg)
|
||||
msg = f"| UID | 昵称 | 共{len(fl)}个好友\n" + msg
|
||||
await Text(msg).send()
|
||||
logger.info("查看好友列表", "好友列表", session=session)
|
||||
except (ApiNotAvailable, AttributeError) as e:
|
||||
await Text("Api未实现...").send()
|
||||
except Exception as e:
|
||||
logger.error("好友列表发生错误", "好友列表", session=session, e=e)
|
||||
await Text("其他未知错误...").send()
|
||||
|
||||
|
||||
@_group_matcher.handle()
|
||||
async def _(
|
||||
bot: Bot,
|
||||
session: EventSession,
|
||||
):
|
||||
try:
|
||||
# TODO: 其他adapter的群组api
|
||||
gl = await bot.get_group_list()
|
||||
msg = ["{group_id} {group_name}".format_map(g) for g in gl]
|
||||
msg = "\n".join(msg)
|
||||
msg = f"| GID | 名称 | 共{len(gl)}个群组\n" + msg
|
||||
await Text(msg).send()
|
||||
logger.info("查看群组列表", "群组列表", session=session)
|
||||
except (ApiNotAvailable, AttributeError) as e:
|
||||
await Text("Api未实现...").send()
|
||||
except Exception as e:
|
||||
logger.error("查看群组列表发生错误", "群组列表", session=session, e=e)
|
||||
await Text("其他未知错误...").send()
|
||||
@@ -0,0 +1,68 @@
|
||||
from nonebot.permission import SUPERUSER
|
||||
from nonebot.plugin import PluginMetadata
|
||||
from nonebot.rule import to_me
|
||||
from nonebot_plugin_alconna import Alconna, Arparma, on_alconna
|
||||
from nonebot_plugin_apscheduler import scheduler
|
||||
from nonebot_plugin_saa import Text
|
||||
from nonebot_plugin_session import EventSession
|
||||
|
||||
from zhenxun.configs.config import Config
|
||||
from zhenxun.configs.utils import PluginExtraData, RegisterConfig
|
||||
from zhenxun.services.log import logger
|
||||
from zhenxun.utils.enum import PluginType
|
||||
|
||||
__plugin_meta__ = PluginMetadata(
|
||||
name="重载配置",
|
||||
description="重新加载config.yaml",
|
||||
usage="""
|
||||
重载配置
|
||||
""".strip(),
|
||||
extra=PluginExtraData(
|
||||
author="HibiKier",
|
||||
version="0.1",
|
||||
plugin_type=PluginType.SUPERUSER,
|
||||
configs=[
|
||||
RegisterConfig(
|
||||
key="AUTO_RELOAD",
|
||||
value=False,
|
||||
help="自动重载配置文件",
|
||||
default_value=False,
|
||||
type=bool,
|
||||
),
|
||||
RegisterConfig(
|
||||
key="AUTO_RELOAD_TIME",
|
||||
value=180,
|
||||
help="自动重载配置文件时长",
|
||||
default_value=180,
|
||||
type=int,
|
||||
),
|
||||
],
|
||||
).dict(),
|
||||
)
|
||||
|
||||
_matcher = on_alconna(
|
||||
Alconna(
|
||||
"重载配置",
|
||||
),
|
||||
rule=to_me(),
|
||||
permission=SUPERUSER,
|
||||
priority=1,
|
||||
block=True,
|
||||
)
|
||||
|
||||
|
||||
@_matcher.handle()
|
||||
async def _(session: EventSession, arparma: Arparma):
|
||||
Config.reload()
|
||||
logger.debug("自动重载配置文件", arparma.header_result, session=session)
|
||||
await Text("重载完成!").send(reply=True)
|
||||
|
||||
|
||||
@scheduler.scheduled_job(
|
||||
"interval",
|
||||
seconds=Config.get_config("reload_setting", "AUTO_RELOAD_TIME", 180),
|
||||
)
|
||||
async def _():
|
||||
if Config.get_config("reload_setting", "AUTO_RELOAD"):
|
||||
Config.reload()
|
||||
logger.debug("已自动重载配置文件...")
|
||||
@@ -0,0 +1,266 @@
|
||||
from io import BytesIO
|
||||
|
||||
from arclet.alconna import Args, Option
|
||||
from arclet.alconna.typing import CommandMeta
|
||||
from nonebot.adapters import Bot
|
||||
from nonebot.permission import SUPERUSER
|
||||
from nonebot.plugin import PluginMetadata
|
||||
from nonebot.rule import to_me
|
||||
from nonebot_plugin_alconna import (
|
||||
Alconna,
|
||||
AlconnaQuery,
|
||||
Arparma,
|
||||
Query,
|
||||
on_alconna,
|
||||
store_true,
|
||||
)
|
||||
from nonebot_plugin_saa import Image, Text
|
||||
from nonebot_plugin_session import EventSession
|
||||
|
||||
from zhenxun.configs.path_config import IMAGE_PATH
|
||||
from zhenxun.configs.utils import PluginExtraData
|
||||
from zhenxun.models.fg_request import FgRequest
|
||||
from zhenxun.services.log import logger
|
||||
from zhenxun.utils.enum import PluginType, RequestHandleType, RequestType
|
||||
from zhenxun.utils.exception import NotFoundError
|
||||
from zhenxun.utils.image_utils import BuildImage
|
||||
from zhenxun.utils.utils import get_user_avatar
|
||||
|
||||
usage = """
|
||||
查看请求
|
||||
清空请求
|
||||
请求处理 -fa [id] / 同意好友请求 [id] # 同意好友请求
|
||||
请求处理 -fr [id] / 拒绝好友请求 [id] # 拒绝好友请求
|
||||
请求处理 -fi [id] / 忽略好友请求 [id] # 忽略好友请求
|
||||
请求处理 -ga [id] / 同意群组请求 [id] # 同意群聊请求
|
||||
请求处理 -gr [id] / 拒绝群组请求 [id] # 拒绝群聊请求
|
||||
请求处理 -gi [id] / 忽略群组请求 [id] # 忽略群聊请求
|
||||
""".strip()
|
||||
|
||||
|
||||
__plugin_meta__ = PluginMetadata(
|
||||
name="请求处理",
|
||||
description="好友与邀请群组请求处理",
|
||||
usage=usage,
|
||||
extra=PluginExtraData(
|
||||
author="HibiKier",
|
||||
version="0.1",
|
||||
plugin_type=PluginType.SUPERUSER,
|
||||
).dict(),
|
||||
)
|
||||
|
||||
|
||||
_req_matcher = on_alconna(
|
||||
Alconna(
|
||||
"请求处理",
|
||||
Args["handle", ["-fa", "-fr", "-fi", "-ga", "-gr", "-gi"]]["id", int],
|
||||
meta=CommandMeta(
|
||||
description="好友/群组请求处理",
|
||||
usage=usage,
|
||||
example="同意好友请求 20",
|
||||
compact=True,
|
||||
),
|
||||
),
|
||||
permission=SUPERUSER,
|
||||
priority=1,
|
||||
rule=to_me(),
|
||||
)
|
||||
|
||||
_read_matcher = on_alconna(
|
||||
Alconna(
|
||||
"查看请求",
|
||||
Option("-f|--friend", action=store_true, help_text="查看好友请求"),
|
||||
Option("-g|--group", action=store_true, help_text="查看群组请求"),
|
||||
meta=CommandMeta(
|
||||
description="查看所有请求或好友群组请求",
|
||||
usage="查看请求\n查看请求 -f\n查看请求-g",
|
||||
example="查看请求 -f",
|
||||
compact=True,
|
||||
),
|
||||
),
|
||||
permission=SUPERUSER,
|
||||
priority=1,
|
||||
rule=to_me(),
|
||||
)
|
||||
|
||||
_clear_matcher = on_alconna(
|
||||
Alconna(
|
||||
"清空请求",
|
||||
Option("-f|--friend", action=store_true, help_text="清空好友请求"),
|
||||
Option("-g|--group", action=store_true, help_text="清空群组请求"),
|
||||
meta=CommandMeta(
|
||||
description="清空请求",
|
||||
usage="清空请求\n清空请求 -f\n清空请求-g",
|
||||
example="清空请求 -f",
|
||||
compact=True,
|
||||
),
|
||||
),
|
||||
permission=SUPERUSER,
|
||||
priority=1,
|
||||
rule=to_me(),
|
||||
)
|
||||
|
||||
reg_arg_list = [
|
||||
(r"同意好友请求", ["-fa", "{%0}"]),
|
||||
(r"拒绝好友请求", ["-fr", "{%0}"]),
|
||||
(r"忽略好友请求", ["-fi", "{%0}"]),
|
||||
(r"同意群组请求", ["-ga", "{%0}"]),
|
||||
(r"拒绝群组请求", ["-gr", "{%0}"]),
|
||||
(r"忽略群组请求", ["-gi", "{%0}"]),
|
||||
]
|
||||
|
||||
for r in reg_arg_list:
|
||||
_req_matcher.shortcut(
|
||||
r[0],
|
||||
command="请求处理",
|
||||
arguments=r[1],
|
||||
prefix=True,
|
||||
)
|
||||
|
||||
|
||||
@_req_matcher.handle()
|
||||
async def _(
|
||||
bot: Bot,
|
||||
session: EventSession,
|
||||
handle: str,
|
||||
id: int,
|
||||
arparma: Arparma,
|
||||
):
|
||||
request_type = RequestType.FRIEND if handle.startswith("-f") else RequestType.GROUP
|
||||
type_dict = {
|
||||
"a": RequestHandleType.APPROVE,
|
||||
"r": RequestHandleType.REFUSED,
|
||||
"i": RequestHandleType.IGNORE,
|
||||
}
|
||||
handle_type = type_dict[handle[-1]]
|
||||
try:
|
||||
if handle_type == RequestHandleType.APPROVE:
|
||||
await FgRequest.approve(bot, id, request_type)
|
||||
if handle_type == RequestHandleType.REFUSED:
|
||||
await FgRequest.refused(bot, id, request_type)
|
||||
if handle_type == RequestHandleType.IGNORE:
|
||||
await FgRequest.ignore(bot, id, request_type)
|
||||
except NotFoundError:
|
||||
await Text("未发现此id的请求...").finish(reply=True)
|
||||
except Exception:
|
||||
await Text("其他错误, 可能flag已失效...").finish(reply=True)
|
||||
logger.info("处理请求", arparma.header_result, session=session)
|
||||
await Text("成功处理请求!").finish(reply=True)
|
||||
|
||||
|
||||
@_read_matcher.handle()
|
||||
async def _(
|
||||
session: EventSession,
|
||||
arparma: Arparma,
|
||||
is_friend: Query[bool] = AlconnaQuery("friend.value", False),
|
||||
is_group: Query[bool] = AlconnaQuery("group.value", False),
|
||||
):
|
||||
if all_request := await FgRequest.filter(handle_type__isnull=True).all():
|
||||
req_list = list(all_request)
|
||||
req_list.reverse()
|
||||
friend_req = []
|
||||
group_req = []
|
||||
for req in req_list:
|
||||
if req.request_type == RequestType.FRIEND:
|
||||
friend_req.append(req)
|
||||
else:
|
||||
group_req.append(req)
|
||||
if is_friend.result:
|
||||
group_req = []
|
||||
elif is_group.result:
|
||||
friend_req = []
|
||||
req_image_list: list[BuildImage] = []
|
||||
for i, req_list in enumerate([friend_req, group_req]):
|
||||
img_list = []
|
||||
for req in req_list:
|
||||
content = await get_user_avatar(req.user_id)
|
||||
ava_img = BuildImage(
|
||||
80, 80, background=BytesIO(content) if content else None
|
||||
)
|
||||
await ava_img.circle()
|
||||
handle_img = BuildImage(
|
||||
130, 32, font_size=15, color="#EEEFF4", font="HYWenHei-85W.ttf"
|
||||
)
|
||||
await handle_img.text((0, 0), "同意/拒绝/忽略", center_type="center")
|
||||
await handle_img.circle_corner(10)
|
||||
background = BuildImage(500, 100, font_size=22, color=(255, 255, 255))
|
||||
await background.paste(ava_img, (55, 0), center_type="height")
|
||||
if session.platform and session.platform != "unknown":
|
||||
platform_icon = BuildImage(
|
||||
30,
|
||||
30,
|
||||
background=IMAGE_PATH / "_icon" / f"{session.platform}.png",
|
||||
)
|
||||
await background.paste(platform_icon, (46, 10))
|
||||
await background.text((150, 12), req.nickname)
|
||||
comment_img = await BuildImage.build_text_image(
|
||||
f"对方留言:{req.comment}", size=15, font_color=(140, 140, 143)
|
||||
)
|
||||
await background.paste(comment_img, (150, 65))
|
||||
tag = await BuildImage.build_text_image(
|
||||
f"{req.platform}",
|
||||
size=13,
|
||||
color=(0, 167, 250),
|
||||
font="HYWenHei-85W.ttf",
|
||||
font_color=(255, 255, 255),
|
||||
padding=(1, 6, 1, 6),
|
||||
)
|
||||
await tag.circle_corner(5)
|
||||
await background.paste(tag, (150, 42))
|
||||
await background.paste(handle_img, (360, 35))
|
||||
_id_img = BuildImage(
|
||||
32, 32, font_size=15, color="#EEEFF4", font="HYWenHei-85W.ttf"
|
||||
)
|
||||
await _id_img.text((0, 0), f"{req.id}", center_type="center")
|
||||
await _id_img.circle_corner(10)
|
||||
await background.paste(_id_img, (10, 0), center_type="height")
|
||||
img_list.append(background)
|
||||
A = await BuildImage.auto_paste(img_list, 1)
|
||||
if A:
|
||||
result_image = BuildImage(
|
||||
A.width, A.height + 30, color=(255, 255, 255), font_size=20
|
||||
)
|
||||
await result_image.paste(A, (0, 30))
|
||||
_type_text = "好友请求" if i == 0 else "群组请求"
|
||||
await result_image.text((15, 13), _type_text, fill=(140, 140, 143))
|
||||
req_image_list.append(result_image)
|
||||
if not req_image_list:
|
||||
await Text("没有任何请求喔...").finish(reply=True)
|
||||
if len(req_image_list) == 1:
|
||||
await Image(req_image_list[0].pic2bs4()).finish()
|
||||
width = sum([img.width for img in req_image_list])
|
||||
height = max([img.height for img in req_image_list])
|
||||
background = BuildImage(width, height)
|
||||
await background.paste(req_image_list[0])
|
||||
await req_image_list[1].line((0, 10, 1, req_image_list[1].height - 10), width=1)
|
||||
await background.paste(req_image_list[1], (req_image_list[1].width, 0))
|
||||
logger.info("查看请求", arparma.header_result, session=session)
|
||||
await Image(background.pic2bs4()).finish()
|
||||
await Text("没有任何请求喔...").finish(reply=True)
|
||||
|
||||
|
||||
@_clear_matcher.handle()
|
||||
async def _(
|
||||
session: EventSession,
|
||||
arparma: Arparma,
|
||||
is_friend: Query[bool] = AlconnaQuery("friend.value", False),
|
||||
is_group: Query[bool] = AlconnaQuery("group.value", False),
|
||||
):
|
||||
_type = ""
|
||||
if is_friend.result:
|
||||
_type = "好友"
|
||||
await FgRequest.filter(
|
||||
handle_type__isnull=True, request_type=RequestType.FRIEND
|
||||
).update(handle_type=RequestHandleType.IGNORE)
|
||||
elif is_group.result:
|
||||
_type = "群组"
|
||||
await FgRequest.filter(
|
||||
handle_type__isnull=True, request_type=RequestType.GROUP
|
||||
).update(handle_type=RequestHandleType.IGNORE)
|
||||
else:
|
||||
_type = "所有"
|
||||
await FgRequest.filter(handle_type__isnull=True).update(
|
||||
handle_type=RequestHandleType.IGNORE
|
||||
)
|
||||
logger.info(f"清空{_type}请求", arparma.header_result, session=session)
|
||||
await Text(f"已清空{_type}请求!").finish()
|
||||
@@ -0,0 +1,105 @@
|
||||
from nonebot.permission import SUPERUSER
|
||||
from nonebot.plugin import PluginMetadata
|
||||
from nonebot_plugin_alconna import (
|
||||
Alconna,
|
||||
Args,
|
||||
Arparma,
|
||||
At,
|
||||
Match,
|
||||
Subcommand,
|
||||
on_alconna,
|
||||
)
|
||||
from nonebot_plugin_saa import Mention, MessageFactory, Text
|
||||
from nonebot_plugin_session import EventSession, SessionLevel
|
||||
|
||||
from zhenxun.configs.utils import PluginExtraData
|
||||
from zhenxun.models.level_user import LevelUser
|
||||
from zhenxun.services.log import logger
|
||||
from zhenxun.utils.enum import PluginType
|
||||
|
||||
__plugin_meta__ = PluginMetadata(
|
||||
name="用户权限管理",
|
||||
description="设置用户权限",
|
||||
usage="""
|
||||
权限设置 add [level: 权限等级] [at: at对象或用户id] [gid: 群组]
|
||||
权限设置 delete [at: at对象或用户id]
|
||||
|
||||
权限设置 add 5 @user
|
||||
权限设置 add 5 422 352352
|
||||
|
||||
权限设置 delete @user
|
||||
权限设置 delete 123456
|
||||
|
||||
""".strip(),
|
||||
extra=PluginExtraData(
|
||||
author="HibiKier",
|
||||
version="0.1",
|
||||
plugin_type=PluginType.SUPERUSER,
|
||||
).dict(),
|
||||
)
|
||||
|
||||
|
||||
_matcher = on_alconna(
|
||||
Alconna(
|
||||
"权限设置",
|
||||
Subcommand(
|
||||
"add", Args["level", int]["uid", [str, At]]["gid?", str], help_text="添加权限"
|
||||
),
|
||||
Subcommand("delete", Args["uid", [str, At]]["gid?", str], help_text="删除权限"),
|
||||
),
|
||||
permission=SUPERUSER,
|
||||
priority=5,
|
||||
block=True,
|
||||
)
|
||||
|
||||
|
||||
@_matcher.assign("add")
|
||||
async def _(
|
||||
session: EventSession,
|
||||
arparma: Arparma,
|
||||
level: int,
|
||||
gid: Match[str],
|
||||
uid: str | At,
|
||||
):
|
||||
group_id = gid.result if gid.available else session.id3 or session.id2
|
||||
if group_id:
|
||||
if isinstance(uid, At):
|
||||
uid = uid.target
|
||||
user = await LevelUser.get_or_none(user_id=uid, group_id=group_id)
|
||||
old_level = user.user_level if user else 0
|
||||
await LevelUser.set_level(uid, group_id, level, 1)
|
||||
logger.info(
|
||||
f"修改权限: {old_level} -> {level}", arparma.header_result, session=session
|
||||
)
|
||||
if session.level in [SessionLevel.LEVEL2, SessionLevel.LEVEL3]:
|
||||
await MessageFactory(
|
||||
[Text("成功为 "), Mention(uid), Text(f" 设置权限:{old_level} -> {level}")]
|
||||
).finish(reply=True)
|
||||
await Text(
|
||||
f"成功为 \n群组:{group_id}\n用户:{uid} \n设置权限!\n权限:{old_level} -> {level}"
|
||||
).finish()
|
||||
await Text(f"设置权限时群组不能为空...").finish()
|
||||
|
||||
|
||||
@_matcher.assign("delete")
|
||||
async def _(
|
||||
session: EventSession,
|
||||
arparma: Arparma,
|
||||
gid: Match[str],
|
||||
uid: str | At,
|
||||
):
|
||||
group_id = gid.result if gid.available else session.id3 or session.id2
|
||||
if group_id:
|
||||
if isinstance(uid, At):
|
||||
uid = uid.target
|
||||
if user := await LevelUser.get_or_none(user_id=uid, group_id=group_id):
|
||||
await user.delete()
|
||||
if session.level in [SessionLevel.LEVEL2, SessionLevel.LEVEL3]:
|
||||
await MessageFactory(
|
||||
[Text("成功删除 "), Mention(uid), Text(f" 的权限等级!")]
|
||||
).finish(reply=True)
|
||||
await Text(
|
||||
f"成功删除 \n群组:{group_id}\n用户:{uid} \n的权限等级!\n权限:{user.user_level} -> 0"
|
||||
).finish()
|
||||
await Text(f"对方目前暂无权限喔...").finish()
|
||||
await Text(f"设置权限时群组不能为空...").finish()
|
||||
@@ -0,0 +1,119 @@
|
||||
from nonebot.adapters import Bot
|
||||
from nonebot.adapters.kaiheila.exception import ApiNotAvailable
|
||||
from nonebot.permission import SUPERUSER
|
||||
from nonebot.plugin import PluginMetadata
|
||||
from nonebot.rule import to_me
|
||||
from nonebot_plugin_alconna import Alconna, Arparma, At, Match, on_alconna
|
||||
from nonebot_plugin_saa import Mention, MessageFactory, Text
|
||||
from nonebot_plugin_session import EventSession, SessionLevel
|
||||
|
||||
from zhenxun.configs.config import Config
|
||||
from zhenxun.configs.utils import PluginExtraData
|
||||
from zhenxun.models.friend_user import FriendUser
|
||||
from zhenxun.models.group_info import GroupInfo
|
||||
from zhenxun.models.level_user import LevelUser
|
||||
from zhenxun.services.log import logger
|
||||
from zhenxun.utils.enum import PluginType
|
||||
|
||||
__plugin_meta__ = PluginMetadata(
|
||||
name="更新群组/好友信息",
|
||||
description="更新群组/好友信息",
|
||||
usage="""
|
||||
更新群组信息
|
||||
更新好友信息
|
||||
""".strip(),
|
||||
extra=PluginExtraData(
|
||||
author="HibiKier",
|
||||
version="0.1",
|
||||
plugin_type=PluginType.SUPERUSER,
|
||||
).dict(),
|
||||
)
|
||||
|
||||
|
||||
_group_matcher = on_alconna(
|
||||
Alconna(
|
||||
"更新群组信息",
|
||||
),
|
||||
permission=SUPERUSER,
|
||||
rule=to_me(),
|
||||
priority=1,
|
||||
block=True,
|
||||
)
|
||||
|
||||
_friend_matcher = on_alconna(
|
||||
Alconna(
|
||||
"更新好友信息",
|
||||
),
|
||||
permission=SUPERUSER,
|
||||
rule=to_me(),
|
||||
priority=1,
|
||||
block=True,
|
||||
)
|
||||
|
||||
# TODO: 其他adapter的更新操作
|
||||
|
||||
@_group_matcher.handle()
|
||||
async def _(
|
||||
bot: Bot,
|
||||
session: EventSession,
|
||||
arparma: Arparma,
|
||||
):
|
||||
try:
|
||||
gl = await bot.get_group_list()
|
||||
gl = [g["group_id"] for g in gl]
|
||||
num = 0
|
||||
for g in gl:
|
||||
try:
|
||||
group_info = await bot.get_group_info(group_id=g)
|
||||
await GroupInfo.update_or_create(
|
||||
group_id=str(group_info["group_id"]),
|
||||
defaults={
|
||||
"group_name": group_info["group_name"],
|
||||
"max_member_count": group_info["max_member_count"],
|
||||
"member_count": group_info["member_count"],
|
||||
},
|
||||
)
|
||||
num += 1
|
||||
logger.debug(
|
||||
"群聊信息更新成功", "更新群信息", session=session, target=group_info["group_id"]
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
f"更新群聊信息失败",
|
||||
arparma.header_result,
|
||||
session=session,
|
||||
target=g,
|
||||
)
|
||||
await Text(f"成功更新了 {len(gl)} 个群的信息").send()
|
||||
logger.info(
|
||||
f"更新群聊信息完成,共更新了 {len(gl)} 个群的信息", arparma.header_result, session=session
|
||||
)
|
||||
except (ApiNotAvailable, AttributeError) as e:
|
||||
await Text("Api未实现...").send()
|
||||
except Exception as e:
|
||||
logger.error("更新好友信息发生错误", arparma.header_result, session=session, e=e)
|
||||
await Text("其他未知错误...").send()
|
||||
|
||||
|
||||
@_friend_matcher.assign("delete")
|
||||
async def _(
|
||||
bot: Bot,
|
||||
session: EventSession,
|
||||
arparma: Arparma,
|
||||
):
|
||||
num = 0
|
||||
error_list = []
|
||||
fl = await bot.get_friend_list()
|
||||
for f in fl:
|
||||
try:
|
||||
await FriendUser.update_or_create(
|
||||
user_id=str(f["user_id"]), defaults={"nickname": f["nickname"]}
|
||||
)
|
||||
logger.debug(f"更新好友信息成功", "更新好友信息", session=session, target=f["user_id"])
|
||||
num += 1
|
||||
except Exception as e:
|
||||
logger.error(f"更新好友信息失败", "更新好友信息", session=session, target=f["user_id"], e=e)
|
||||
await Text(f"成功更新了 {num} 个好友的信息!").send()
|
||||
if error_list:
|
||||
await Text(f"以下好友更新失败:\n" + "\n".join(error_list)).send()
|
||||
logger.info(f"更新好友信息完成,共更新了 {num} 个群的信息", arparma.header_result, session=session)
|
||||
Reference in New Issue
Block a user