From 8a1c9816661089d6e51732c6e2f9bffc7dec3a0e Mon Sep 17 00:00:00 2001 From: snowy Date: Wed, 20 Mar 2024 18:27:49 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AF=B9=E9=83=A8=E5=88=86=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E5=9B=9E=E5=A4=8D=E7=94=A8markdown=E8=BF=9B=E8=A1=8C?= =?UTF-8?q?=E4=BA=86=E9=87=8D=E6=96=B0=E6=8E=92=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/liteyuki_main/webdash.py | 182 +++++++++---------- src/plugins/liteyuki_markdowntest.py | 33 ++-- src/plugins/liteyuki_plugin_eventpush.py | 14 +- src/plugins/liteyuki_plugin_npm/manager.py | 17 +- src/plugins/liteyuki_plugin_user/__init__.py | 12 +- src/resources/lang/en.lang | 1 + src/resources/lang/zh-CN.lang | 2 +- src/utils/message.py | 9 +- 8 files changed, 152 insertions(+), 118 deletions(-) diff --git a/src/liteyuki_main/webdash.py b/src/liteyuki_main/webdash.py index f7fbae2..6833e56 100644 --- a/src/liteyuki_main/webdash.py +++ b/src/liteyuki_main/webdash.py @@ -1,91 +1,91 @@ -# import nonebot -# import psutil -# from dash import Dash, Input, Output, dcc, html -# from starlette.middleware.wsgi import WSGIMiddleware -# -# from src.utils.language import Language -# from src.utils.tools import convert_size -# -# app = nonebot.get_app() -# -# -# def get_system_info(): -# cpu_percent = psutil.cpu_percent() -# memory_info = psutil.virtual_memory() -# memory_percent = memory_info.percent -# return { -# "cpu_percent" : cpu_percent, -# "memory_percent": memory_percent -# } -# -# -# @app.get("/system_info") -# async def system_info(): -# return get_system_info() -# -# -# lang = Language() -# dash_app = Dash(__name__) -# dash_app.layout = dash_app.layout = html.Div(children=[ -# html.H1(children=lang.get("main.monitor.title"), style={ -# 'textAlign': 'center' -# }), -# -# dcc.Graph(id='live-update-graph'), -# dcc.Interval( -# id='interval-component', -# interval=1 * 1000, # in milliseconds -# n_intervals=0 -# ) -# ]) -# -# -# @dash_app.callback(Output('live-update-graph', 'figure'), -# [Input('interval-component', 'n_intervals')]) -# def update_graph_live(n): -# lang = Language() -# system_inf = get_system_info() -# dash_app.layout = html.Div(children=[ -# html.H1(children=lang.get("main.monitor.title"), style={ -# 'textAlign': 'center' -# }), -# -# dcc.Graph(id='live-update-graph'), -# dcc.Interval( -# id='interval-component', -# interval=2 * 1000, # in milliseconds -# n_intervals=0 -# ) -# ]) -# mem = psutil.virtual_memory() -# cpu_f = psutil.cpu_freq() -# figure = { -# 'data' : [ -# { -# 'x' : [f"{cpu_f.current / 1000:.2f}GHz {psutil.cpu_count(logical=False)}c{psutil.cpu_count()}t"], -# 'y' : [system_inf['cpu_percent']], -# 'type': 'bar', -# 'name': f"{lang.get('main.monitor.cpu')} {lang.get('main.monitor.usage')}" -# -# }, -# { -# 'x' : [f"{convert_size(mem.used, add_unit=False)}/{convert_size(mem.total)}({mem.used / mem.total * 100:.2f}%)"], -# 'y' : [system_inf['memory_percent']], -# 'type': 'bar', -# 'name': f"{lang.get('main.monitor.memory')} {lang.get('main.monitor.usage')}" -# }, -# ], -# 'layout': { -# 'title': lang.get('main.monitor.description'), -# # 'xaxis': { -# # 'range': [0, 10] -# # }, # 设置x轴的范围 -# 'yaxis': { -# 'range': [0, 100] -# }, # 设置y轴的范围 -# } -# } -# return figure -# -# -# app.mount("/", WSGIMiddleware(dash_app.server)) +import nonebot +import psutil +from dash import Dash, Input, Output, dcc, html +from starlette.middleware.wsgi import WSGIMiddleware + +from src.utils.language import Language +from src.utils.tools import convert_size + +app = nonebot.get_app() + + +def get_system_info(): + cpu_percent = psutil.cpu_percent() + memory_info = psutil.virtual_memory() + memory_percent = memory_info.percent + return { + "cpu_percent" : cpu_percent, + "memory_percent": memory_percent + } + + +@app.get("/system_info") +async def system_info(): + return get_system_info() + + +lang = Language() +dash_app = Dash(__name__) +dash_app.layout = dash_app.layout = html.Div(children=[ + html.H1(children=lang.get("main.monitor.title"), style={ + 'textAlign': 'center' + }), + + dcc.Graph(id='live-update-graph'), + dcc.Interval( + id='interval-component', + interval=1 * 1000, # in milliseconds + n_intervals=0 + ) +]) + + +@dash_app.callback(Output('live-update-graph', 'figure'), + [Input('interval-component', 'n_intervals')]) +def update_graph_live(n): + lang = Language() + system_inf = get_system_info() + dash_app.layout = html.Div(children=[ + html.H1(children=lang.get("main.monitor.title"), style={ + 'textAlign': 'center' + }), + + dcc.Graph(id='live-update-graph'), + dcc.Interval( + id='interval-component', + interval=2 * 1000, # in milliseconds + n_intervals=0 + ) + ]) + mem = psutil.virtual_memory() + cpu_f = psutil.cpu_freq() + figure = { + 'data' : [ + { + 'x' : [f"{cpu_f.current / 1000:.2f}GHz {psutil.cpu_count(logical=False)}c{psutil.cpu_count()}t"], + 'y' : [system_inf['cpu_percent']], + 'type': 'bar', + 'name': f"{lang.get('main.monitor.cpu')} {lang.get('main.monitor.usage')}" + + }, + { + 'x' : [f"{convert_size(mem.used, add_unit=False)}/{convert_size(mem.total)}({mem.used / mem.total * 100:.2f}%)"], + 'y' : [system_inf['memory_percent']], + 'type': 'bar', + 'name': f"{lang.get('main.monitor.memory')} {lang.get('main.monitor.usage')}" + }, + ], + 'layout': { + 'title': lang.get('main.monitor.description'), + # 'xaxis': { + # 'range': [0, 10] + # }, # 设置x轴的范围 + 'yaxis': { + 'range': [0, 100] + }, # 设置y轴的范围 + } + } + return figure + + +app.mount("/", WSGIMiddleware(dash_app.server)) diff --git a/src/plugins/liteyuki_markdowntest.py b/src/plugins/liteyuki_markdowntest.py index a17a6ac..b5837f6 100644 --- a/src/plugins/liteyuki_markdowntest.py +++ b/src/plugins/liteyuki_markdowntest.py @@ -4,22 +4,22 @@ from nonebot.adapters.onebot.v11 import MessageSegment from nonebot.exception import FinishedException from nonebot.params import CommandArg from nonebot.permission import SUPERUSER +from nonebot.plugin import PluginMetadata from src.utils.message import send_markdown from src.utils.typing import T_Message, T_Bot, v11, T_MessageEvent md_test = on_command("mdts", aliases={"会话md"}, permission=SUPERUSER) md_group = on_command("mdg", aliases={"群md"}, permission=SUPERUSER) -md_conv = on_command("md",block=False, permission=SUPERUSER) - +md_conv = on_command("md", block=False, permission=SUPERUSER) placeholder = { - "[": "[", - "]": "]", - "&": "&", - ",": ",", - "\n": r"\n", - "\"": r'\\\"' + "[": "[", + "]": "]", + "&": "&", + ",": ",", + "\n" : r"\n", + "\"" : r'\\\"' } @@ -30,17 +30,26 @@ async def _(bot: T_Bot, event: T_MessageEvent, arg: v11.Message = CommandArg()): bot, message_type=event.message_type, session_id=event.user_id if event.message_type == "private" else event.group_id -) + ) + + ignore_msg_ids = [] last_sent = None + + @md_conv.handle() async def _(bot: v11.Bot, event: v11.MessageEvent, arg: v11.Message = CommandArg()): if str(event.user_id) == str(bot.self_id) and str(bot.self_id) in ["2751454815"]: nonebot.logger.info("开始处理:%s" % str(event.message_id)) - data, fdata = await send_markdown(str(arg), bot, event.message_type, - event.group_id if event.message_type == "group" else event.user_id) + data = await send_markdown(str(arg), bot, event=event) await bot.delete_msg(message_id=event.message_id) - nonebot.logger.info("已处理:%s, %s" % (str(data["message_id"]), str(fdata["message_id"]))) \ No newline at end of file +__author__ = "snowykami" +__plugin_meta__ = PluginMetadata( + name="轻雪Markdown测试", + description="用于测试Markdown的插件", + usage="", + homepage="https://github.com/snowykami/LiteyukiBot", +) \ No newline at end of file diff --git a/src/plugins/liteyuki_plugin_eventpush.py b/src/plugins/liteyuki_plugin_eventpush.py index 874ad24..78b852a 100644 --- a/src/plugins/liteyuki_plugin_eventpush.py +++ b/src/plugins/liteyuki_plugin_eventpush.py @@ -3,6 +3,7 @@ from typing import Optional import nonebot from nonebot import on_message from arclet.alconna import Arparma, Alconna, Args, Option, Subcommand, Arg +from nonebot.plugin import PluginMetadata from nonebot_plugin_alconna import on_alconna from src.utils.data import LiteModel from src.utils.message import send_markdown @@ -34,7 +35,7 @@ alc = Alconna( "add", Args["source", str], Args["target", str], - Option("-b", Args["bidirectional", bool]) + Option("bidirectional", Args["bidirectional", bool]) ), Subcommand( "rm", @@ -105,5 +106,14 @@ async def _(event: T_MessageEvent, bot: T_Bot): push_message = ( f"> From {event.sender.nickname}@{push.source.session_type}.{push.source.session_id}\n> Bot {bot.self_id}\n\n" f"{msg_formatted}") - await send_markdown(push_message, bot2, push.target.session_type, push.target.session_id) + await send_markdown(push_message, bot2, event=event) return + + +__author__ = "snowykami" +__plugin_meta__ = PluginMetadata( + name="轻雪事件推送", + description="事件推送插件,支持单向和双向推送,支持跨Bot推送", + usage="", + homepage="https://github.com/snowykami/LiteyukiBot", +) \ No newline at end of file diff --git a/src/plugins/liteyuki_plugin_npm/manager.py b/src/plugins/liteyuki_plugin_npm/manager.py index ea4b8ac..2c07e98 100644 --- a/src/plugins/liteyuki_plugin_npm/manager.py +++ b/src/plugins/liteyuki_plugin_npm/manager.py @@ -2,21 +2,22 @@ import nonebot.plugin from nonebot import on_command from nonebot.permission import SUPERUSER -from src.utils.typing import T_MessageEvent +from src.utils.message import send_markdown +from src.utils.typing import T_Bot, T_MessageEvent from src.utils.language import get_user_lang -list_plugins = on_command("list-plugin", aliases={"列出插件"}, priority=0) -toggle_plugin = on_command("enable-plugin", aliases={"启用插件", "禁用插件", "disable-plugin"}, priority=0) +list_plugins = on_command("list-plugin", aliases={"列出插件"}, priority=0, permission=SUPERUSER) +toggle_plugin = on_command("enable-plugin", aliases={"启用插件", "禁用插件", "disable-plugin"}, priority=0, permission=SUPERUSER) @list_plugins.handle() -async def _(event: T_MessageEvent): +async def _(event: T_MessageEvent, bot: T_Bot): lang = get_user_lang(str(event.user_id)) - reply = lang.get("npm.loaded_plugins") + reply = f"# {lang.get('npm.loaded_plugins')} | {lang.get('npm.total', TOTAL=len(nonebot.get_loaded_plugins()))} \n***" for plugin in nonebot.get_loaded_plugins(): # 检查是否有 metadata 属性 if plugin.metadata: - reply += f"\n- {plugin.metadata.name}" + reply += f"\n- **{plugin.metadata.name}**".replace('_', r'\_') else: - reply += f"\n- {plugin.name}" - await list_plugins.finish(reply) + reply += f"\n- **{plugin.name}**".replace('_', r'\_') + await send_markdown(reply, bot, event=event) diff --git a/src/plugins/liteyuki_plugin_user/__init__.py b/src/plugins/liteyuki_plugin_user/__init__.py index f8f3222..25383dd 100644 --- a/src/plugins/liteyuki_plugin_user/__init__.py +++ b/src/plugins/liteyuki_plugin_user/__init__.py @@ -1 +1,11 @@ -from .profile_manager import * \ No newline at end of file +from nonebot.plugin import PluginMetadata + +from .profile_manager import * + +__author__ = "snowykami" +__plugin_meta__ = PluginMetadata( + name="轻雪用户管理", + description="用户管理插件", + usage="", + homepage="https://github.com/snowykami/LiteyukiBot", +) \ No newline at end of file diff --git a/src/resources/lang/en.lang b/src/resources/lang/en.lang index 1980947..a359fea 100644 --- a/src/resources/lang/en.lang +++ b/src/resources/lang/en.lang @@ -11,6 +11,7 @@ main.monitor.disk=Disk main.monitor.usage=Usage npm.loaded_plugins=Loaded plugins +npm.total=Total {TOTAL} user.profile_manager.query=Your {ATTR} is {VALUE} user.profile_manager.set=Your {ATTR} has been set to {VALUE} \ No newline at end of file diff --git a/src/resources/lang/zh-CN.lang b/src/resources/lang/zh-CN.lang index 1636f06..bc6ada4 100644 --- a/src/resources/lang/zh-CN.lang +++ b/src/resources/lang/zh-CN.lang @@ -11,7 +11,7 @@ main.monitor.disk=磁盘 main.monitor.usage=使用率 npm.loaded_plugins=已加载插件 -npm.loaded_plugins.description=已加载的插件列表 +npm.total=总计 {TOTAL} user.profile_manager.query=你的个人信息 {ATTR} 为 {VALUE} user.profile_manager.set=你的个人信息 {ATTR} 已设置为 {VALUE} \ No newline at end of file diff --git a/src/utils/message.py b/src/utils/message.py index fa88b77..24b9d1b 100644 --- a/src/utils/message.py +++ b/src/utils/message.py @@ -3,11 +3,14 @@ from nonebot.adapters.onebot import v11, v12 from typing_extensions import Any from .tools import de_escape -from .typing import T_Bot +from .typing import T_Bot, T_MessageEvent -async def send_markdown(markdown: str, bot: T_Bot, message_type: str, session_id: str | int) -> tuple[dict[str, Any], dict[str, Any]]: +async def send_markdown(markdown: str, bot: T_Bot, *, message_type: str=None, session_id: str | int=None, event: T_MessageEvent=None) -> dict[str, Any]: formatted_md = de_escape(markdown).replace("\n", r"\n").replace("\"", r'\\\"') + if event is not None and message_type is None: + message_type = event.message_type + session_id = event.user_id if event.message_type == "private" else event.group_id try: forward_data = await bot.call_api( api="send_private_forward_msg", @@ -67,4 +70,4 @@ async def send_markdown(markdown: str, bot: T_Bot, message_type: str, session_id else: nonebot.logger.error("send_markdown: bot type not supported") data = {} - return data, forward_data + return data