✨ 分离magicoca
和croterline
This commit is contained in:
@ -1,53 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Copyright (C) 2020-2024 LiteyukiStudio. All Rights Reserved
|
||||
|
||||
@Time : 2024/8/11 下午5:24
|
||||
@Author : snowykami
|
||||
@Email : snowykami@outlook.com
|
||||
@File : __init__.py.py
|
||||
@Software: PyCharm
|
||||
"""
|
||||
|
||||
import nonebot
|
||||
from liteyuki.utils import IS_MAIN_PROCESS
|
||||
from liteyuki.plugin import PluginMetadata, PluginType
|
||||
from .nb_utils import adapter_manager, driver_manager # type: ignore
|
||||
from liteyuki.log import logger
|
||||
|
||||
__plugin_meta__ = PluginMetadata(
|
||||
name="NoneBot2启动器",
|
||||
type=PluginType.APPLICATION,
|
||||
)
|
||||
|
||||
|
||||
def nb_run(*args, **kwargs):
|
||||
"""
|
||||
初始化NoneBot并运行在子进程
|
||||
Args:
|
||||
**kwargs:
|
||||
|
||||
Returns:
|
||||
"""
|
||||
# 给子进程传递通道对象
|
||||
kwargs.update(kwargs.get("nonebot", {})) # nonebot配置优先
|
||||
nonebot.init(**kwargs)
|
||||
|
||||
driver_manager.init(config=kwargs)
|
||||
adapter_manager.init(kwargs)
|
||||
adapter_manager.register()
|
||||
|
||||
try:
|
||||
# nonebot.load_plugin("nonebot-plugin-lnpm") # 尝试加载轻雪NoneBot插件加载器(Nonebot插件)
|
||||
nonebot.load_plugin("src.liteyuki_main") # 尝试加载轻雪主插件(Nonebot插件)
|
||||
except Exception as e:
|
||||
pass
|
||||
nonebot.run()
|
||||
|
||||
|
||||
if IS_MAIN_PROCESS:
|
||||
from liteyuki import get_bot
|
||||
from .dev_reloader import *
|
||||
|
||||
liteyuki = get_bot()
|
||||
liteyuki.process_manager.add_target(name="nonebot", target=nb_run, args=(), kwargs=liteyuki.config)
|
@ -1,24 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
NoneBot 开发环境重载监视器
|
||||
"""
|
||||
import os.path
|
||||
|
||||
from liteyuki.dev import observer
|
||||
from liteyuki import get_bot, logger
|
||||
from liteyuki.utils import IS_MAIN_PROCESS
|
||||
from watchdog.events import FileSystemEvent
|
||||
|
||||
|
||||
liteyuki = get_bot()
|
||||
|
||||
exclude_extensions = (".pyc", ".pyo")
|
||||
|
||||
|
||||
@observer.on_file_system_event(
|
||||
directories=("src/nonebot_plugins",),
|
||||
event_filter=lambda event: not event.src_path.endswith(exclude_extensions) and ("__pycache__" not in event.src_path ) and os.path.isfile(event.src_path)
|
||||
)
|
||||
def restart_nonebot_process(event: FileSystemEvent):
|
||||
logger.debug(f"File {event.src_path} changed, reloading nonebot...")
|
||||
liteyuki.restart_process("nonebot")
|
33
src/liteyuki_plugins/nonebot/__init__.py
Normal file
33
src/liteyuki_plugins/nonebot/__init__.py
Normal file
@ -0,0 +1,33 @@
|
||||
import os.path
|
||||
from pathlib import Path
|
||||
|
||||
import nonebot
|
||||
from croterline.utils import IsMainProcess
|
||||
|
||||
from liteyuki import get_bot
|
||||
from liteyuki.core import sub_process_manager
|
||||
from liteyuki.plugin import PluginMetadata, PluginType
|
||||
|
||||
__plugin_meta__ = PluginMetadata(
|
||||
name="NoneBot2启动器",
|
||||
type=PluginType.APPLICATION,
|
||||
)
|
||||
|
||||
|
||||
def nb_run(*args, **kwargs):
|
||||
nonebot.init(**kwargs)
|
||||
|
||||
from .nb_utils import driver_manager, adapter_manager
|
||||
driver_manager.init(config=kwargs)
|
||||
adapter_manager.init(kwargs)
|
||||
adapter_manager.register()
|
||||
nonebot.load_plugin(Path(os.path.dirname(__file__)) / "np_main")
|
||||
nonebot.run()
|
||||
|
||||
|
||||
if IsMainProcess:
|
||||
from .dev_reloader import *
|
||||
bot = get_bot()
|
||||
sub_process_manager.add(
|
||||
name="nonebot", func=nb_run, **bot.config.get("nonebot", {})
|
||||
)
|
@ -0,0 +1,15 @@
|
||||
from nonebot.plugin import PluginMetadata
|
||||
from nonebot import get_driver
|
||||
|
||||
__plugin_meta__ = PluginMetadata(
|
||||
name="Minecraft工具箱",
|
||||
description="一些Minecraft相关工具箱",
|
||||
usage="我觉得你应该会用",
|
||||
type="application",
|
||||
homepage="https://github.com/snowykami/LiteyukiBot",
|
||||
extra={
|
||||
"liteyuki" : True,
|
||||
"toggleable" : True,
|
||||
"default_enable": True,
|
||||
}
|
||||
)
|
@ -0,0 +1,22 @@
|
||||
from nonebot.plugin import PluginMetadata
|
||||
from .npm import *
|
||||
from .rpm import *
|
||||
|
||||
__author__ = "snowykami"
|
||||
__plugin_meta__ = PluginMetadata(
|
||||
name="轻雪包管理器",
|
||||
description="本地插件管理和插件商店支持,资源包管理,支持启用/停用,安装/卸载插件",
|
||||
usage=(
|
||||
"npm list\n"
|
||||
"npm enable/disable <plugin_name>\n"
|
||||
"npm search <keywords...>\n"
|
||||
"npm install/uninstall <plugin_name>\n"
|
||||
),
|
||||
type="application",
|
||||
homepage="https://github.com/snowykami/LiteyukiBot",
|
||||
extra={
|
||||
"liteyuki": True,
|
||||
"toggleable" : False,
|
||||
"default_enable" : True,
|
||||
}
|
||||
)
|
@ -0,0 +1,255 @@
|
||||
import json
|
||||
from typing import Optional
|
||||
|
||||
import aiofiles
|
||||
import nonebot.plugin
|
||||
from nonebot.adapters import satori
|
||||
|
||||
from src.utils import event as event_utils
|
||||
from src.utils.base.data import LiteModel
|
||||
from src.utils.base.data_manager import GlobalPlugin, Group, User, group_db, plugin_db, user_db
|
||||
from src.utils.base.ly_typing import T_MessageEvent
|
||||
|
||||
__group_data = {} # 群数据缓存, {group_id: Group}
|
||||
__user_data = {} # 用户数据缓存, {user_id: User}
|
||||
__default_enable = {} # 插件默认启用状态缓存, {plugin_name: bool} static
|
||||
__global_enable = {} # 插件全局启用状态缓存, {plugin_name: bool} dynamic
|
||||
|
||||
|
||||
class PluginTag(LiteModel):
|
||||
label: str
|
||||
color: str = '#000000'
|
||||
|
||||
|
||||
class StorePlugin(LiteModel):
|
||||
name: str
|
||||
desc: str
|
||||
module_name: str # 插件商店中的模块名不等于本地的模块名,前者是文件夹名,后者是点分割模块名
|
||||
project_link: str = ""
|
||||
homepage: str = ""
|
||||
author: str = ""
|
||||
type: str | None = None
|
||||
version: str | None = ""
|
||||
time: str = ""
|
||||
tags: list[PluginTag] = []
|
||||
is_official: bool = False
|
||||
|
||||
|
||||
def get_plugin_exist(plugin_name: str) -> bool:
|
||||
"""
|
||||
获取插件是否存在于加载列表
|
||||
Args:
|
||||
plugin_name:
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
for plugin in nonebot.plugin.get_loaded_plugins():
|
||||
if plugin.name == plugin_name:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
async def get_store_plugin(plugin_name: str) -> Optional[StorePlugin]:
|
||||
"""
|
||||
获取插件信息
|
||||
|
||||
Args:
|
||||
plugin_name (str): 插件模块名
|
||||
|
||||
Returns:
|
||||
Optional[StorePlugin]: 插件信息
|
||||
"""
|
||||
async with aiofiles.open("data/liteyuki/plugins.json", "r", encoding="utf-8") as f:
|
||||
plugins: list[StorePlugin] = [StorePlugin(**pobj) for pobj in json.loads(await f.read())]
|
||||
for plugin in plugins:
|
||||
if plugin.module_name == plugin_name:
|
||||
return plugin
|
||||
return None
|
||||
|
||||
|
||||
def get_plugin_default_enable(plugin_name: str) -> bool:
|
||||
"""
|
||||
获取插件默认启用状态,由插件定义,不存在则默认为启用,优先从缓存中获取
|
||||
|
||||
Args:
|
||||
plugin_name (str): 插件模块名
|
||||
|
||||
Returns:
|
||||
bool: 插件默认状态
|
||||
"""
|
||||
if plugin_name not in __default_enable:
|
||||
plug = nonebot.plugin.get_plugin(plugin_name)
|
||||
default_enable = (plug.metadata.extra.get("default_enable", True) if plug.metadata else True) if plug else True
|
||||
__default_enable[plugin_name] = default_enable
|
||||
|
||||
return __default_enable[plugin_name]
|
||||
|
||||
|
||||
def get_plugin_session_enable(event: T_MessageEvent, plugin_name: str) -> bool:
|
||||
"""
|
||||
获取插件当前会话启用状态
|
||||
|
||||
Args:
|
||||
event: 会话事件
|
||||
plugin_name (str): 插件模块名
|
||||
|
||||
Returns:
|
||||
bool: 插件当前状态
|
||||
"""
|
||||
if isinstance(event, satori.event.Event):
|
||||
if event.guild is not None:
|
||||
message_type = "group"
|
||||
else:
|
||||
message_type = "private"
|
||||
else:
|
||||
message_type = event.message_type
|
||||
if message_type == "group":
|
||||
group_id = str(event.guild.id if isinstance(event, satori.event.Event) else event.group_id)
|
||||
if group_id not in __group_data:
|
||||
group: Group = group_db.where_one(Group(), "group_id = ?", group_id, default=Group(group_id=group_id))
|
||||
__group_data[str(group_id)] = group
|
||||
|
||||
session = __group_data[group_id]
|
||||
else:
|
||||
# session: User = user_db.first(User(), "user_id = ?", event.user_id, default=User(user_id=str(event.user_id)))
|
||||
user_id = str(event.user.id if isinstance(event, satori.event.Event) else event.user_id)
|
||||
if user_id not in __user_data:
|
||||
user: User = user_db.where_one(User(), "user_id = ?", user_id, default=User(user_id=user_id))
|
||||
__user_data[user_id] = user
|
||||
session = __user_data[user_id]
|
||||
# 默认停用插件在启用列表内表示启用
|
||||
# 默认停用插件不在启用列表内表示停用
|
||||
# 默认启用插件在停用列表内表示停用
|
||||
# 默认启用插件不在停用列表内表示启用
|
||||
default_enable = get_plugin_default_enable(plugin_name)
|
||||
if default_enable:
|
||||
return plugin_name not in session.disabled_plugins
|
||||
else:
|
||||
return plugin_name in session.enabled_plugins
|
||||
|
||||
|
||||
def set_plugin_session_enable(event: T_MessageEvent, plugin_name: str, enable: bool):
|
||||
"""
|
||||
设置插件会话启用状态,同时更新数据库和缓存
|
||||
Args:
|
||||
event:
|
||||
plugin_name:
|
||||
enable:
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
if event_utils.get_message_type(event) == "group":
|
||||
session: Group = group_db.where_one(Group(), "group_id = ?", str(event_utils.get_group_id(event)),
|
||||
default=Group(group_id=str(event_utils.get_group_id(event))))
|
||||
else:
|
||||
session: User = user_db.where_one(User(), "user_id = ?", str(event_utils.get_user_id(event)),
|
||||
default=User(user_id=str(event_utils.get_user_id(event))))
|
||||
default_enable = get_plugin_default_enable(plugin_name)
|
||||
if default_enable:
|
||||
if enable:
|
||||
session.disabled_plugins.remove(plugin_name)
|
||||
else:
|
||||
session.disabled_plugins.append(plugin_name)
|
||||
else:
|
||||
if enable:
|
||||
session.enabled_plugins.append(plugin_name)
|
||||
else:
|
||||
session.enabled_plugins.remove(plugin_name)
|
||||
|
||||
if event_utils.get_message_type(event) == "group":
|
||||
__group_data[str(event_utils.get_group_id(event))] = session
|
||||
group_db.save(session)
|
||||
else:
|
||||
__user_data[str(event_utils.get_user_id(event))] = session
|
||||
user_db.save(session)
|
||||
|
||||
|
||||
def get_plugin_global_enable(plugin_name: str) -> bool:
|
||||
"""
|
||||
获取插件全局启用状态, 优先从缓存中获取
|
||||
Args:
|
||||
plugin_name:
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
if plugin_name not in __global_enable:
|
||||
plugin = plugin_db.where_one(
|
||||
GlobalPlugin(),
|
||||
"module_name = ?",
|
||||
plugin_name,
|
||||
default=GlobalPlugin(module_name=plugin_name, enabled=True))
|
||||
__global_enable[plugin_name] = plugin.enabled
|
||||
|
||||
return __global_enable[plugin_name]
|
||||
|
||||
|
||||
def set_plugin_global_enable(plugin_name: str, enable: bool):
|
||||
"""
|
||||
设置插件全局启用状态,同时更新数据库和缓存
|
||||
Args:
|
||||
plugin_name:
|
||||
enable:
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
plugin = plugin_db.where_one(
|
||||
GlobalPlugin(),
|
||||
"module_name = ?",
|
||||
plugin_name,
|
||||
default=GlobalPlugin(module_name=plugin_name, enabled=True))
|
||||
plugin.enabled = enable
|
||||
|
||||
plugin_db.save(plugin)
|
||||
__global_enable[plugin_name] = enable
|
||||
|
||||
|
||||
def get_plugin_can_be_toggle(plugin_name: str) -> bool:
|
||||
"""
|
||||
获取插件是否可以被启用/停用
|
||||
|
||||
Args:
|
||||
plugin_name (str): 插件模块名
|
||||
|
||||
Returns:
|
||||
bool: 插件是否可以被启用/停用
|
||||
"""
|
||||
plug = nonebot.plugin.get_plugin(plugin_name)
|
||||
return plug.metadata.extra.get("toggleable", True) if plug and plug.metadata else True
|
||||
|
||||
|
||||
def get_group_enable(group_id: str) -> bool:
|
||||
"""
|
||||
获取群组是否启用插机器人
|
||||
|
||||
Args:
|
||||
group_id (str): 群组ID
|
||||
|
||||
Returns:
|
||||
bool: 群组是否启用插件
|
||||
"""
|
||||
group_id = str(group_id)
|
||||
if group_id not in __group_data:
|
||||
group: Group = group_db.where_one(Group(), "group_id = ?", group_id, default=Group(group_id=group_id))
|
||||
__group_data[group_id] = group
|
||||
|
||||
return __group_data[group_id].enable
|
||||
|
||||
|
||||
def set_group_enable(group_id: str, enable: bool):
|
||||
"""
|
||||
设置群组是否启用插机器人
|
||||
|
||||
Args:
|
||||
group_id (str): 群组ID
|
||||
enable (bool): 是否启用
|
||||
"""
|
||||
group_id = str(group_id)
|
||||
group: Group = group_db.where_one(Group(), "group_id = ?", group_id, default=Group(group_id=group_id))
|
||||
group.enable = enable
|
||||
|
||||
__group_data[group_id] = group
|
||||
group_db.save(group)
|
@ -0,0 +1,853 @@
|
||||
import os
|
||||
import sys
|
||||
import aiohttp
|
||||
import nonebot.plugin
|
||||
import pip
|
||||
from io import StringIO
|
||||
from arclet.alconna import MultiVar
|
||||
from nonebot import Bot, require
|
||||
from nonebot.exception import FinishedException, IgnoredException, MockApiException
|
||||
from nonebot.internal.adapter import Event
|
||||
from nonebot.internal.matcher import Matcher
|
||||
from nonebot.message import run_preprocessor
|
||||
from nonebot.permission import SUPERUSER
|
||||
from nonebot.plugin import Plugin, PluginMetadata
|
||||
from nonebot.utils import run_sync
|
||||
|
||||
|
||||
from src.utils.base.data_manager import InstalledPlugin
|
||||
from src.utils.base.language import get_user_lang
|
||||
from src.utils.base.ly_typing import T_Bot
|
||||
from src.utils.base.permission import GROUP_ADMIN, GROUP_OWNER
|
||||
from src.utils.message.tools import clamp
|
||||
from src.utils.message.message import MarkdownMessage as md
|
||||
from src.utils.message.markdown import MarkdownComponent as mdc, compile_md, escape_md
|
||||
from src.utils.message.html_tool import md_to_pic
|
||||
from .common import *
|
||||
|
||||
|
||||
require("nonebot_plugin_alconna")
|
||||
from nonebot_plugin_alconna import (
|
||||
UniMessage, on_alconna,
|
||||
Alconna,
|
||||
Args,
|
||||
Arparma,
|
||||
Subcommand,
|
||||
Option,
|
||||
OptionResult,
|
||||
SubcommandResult,
|
||||
)
|
||||
|
||||
# const
|
||||
enable_global = "enable-global"
|
||||
disable_global = "disable-global"
|
||||
enable = "enable"
|
||||
disable = "disable"
|
||||
|
||||
|
||||
@on_alconna(
|
||||
aliases={"插件"},
|
||||
command=Alconna(
|
||||
"npm",
|
||||
Subcommand(
|
||||
"enable",
|
||||
Args["plugin_name", str],
|
||||
Option(
|
||||
"-g|--group",
|
||||
Args["group_id", str, None],
|
||||
help_text="群号",
|
||||
),
|
||||
alias=["e", "启用"],
|
||||
),
|
||||
Subcommand(
|
||||
"disable",
|
||||
Args["plugin_name", str],
|
||||
Option(
|
||||
"-g|--group",
|
||||
Args["group_id", str, None],
|
||||
help_text="群号",
|
||||
),
|
||||
alias=["d", "停用"],
|
||||
),
|
||||
Subcommand(
|
||||
enable_global,
|
||||
Args["plugin_name", str],
|
||||
alias=["eg", "全局启用"],
|
||||
),
|
||||
Subcommand(
|
||||
disable_global,
|
||||
Args["plugin_name", str],
|
||||
alias=["dg", "全局停用"],
|
||||
),
|
||||
# 安装部分
|
||||
Subcommand(
|
||||
"update",
|
||||
alias=["u", "更新"],
|
||||
),
|
||||
Subcommand(
|
||||
"search",
|
||||
Args["keywords", MultiVar(str)],
|
||||
alias=["s", "搜索"],
|
||||
),
|
||||
Subcommand(
|
||||
"install",
|
||||
Args["plugin_name", str],
|
||||
alias=["i", "安装"],
|
||||
),
|
||||
Subcommand(
|
||||
"uninstall",
|
||||
Args["plugin_name", str],
|
||||
alias=["r", "rm", "卸载"],
|
||||
),
|
||||
Subcommand(
|
||||
"list",
|
||||
Args["page", int, 1]["num", int, 10],
|
||||
alias=["ls", "列表"],
|
||||
),
|
||||
),
|
||||
).handle()
|
||||
async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot, npm: Matcher):
|
||||
if not os.path.exists("data/liteyuki/plugins.json"):
|
||||
await npm_update()
|
||||
# 判断会话类型
|
||||
ulang = get_user_lang(str(event.user_id))
|
||||
plugin_name = result.args.get("plugin_name")
|
||||
sc = result.subcommands # 获取子命令
|
||||
perm_s = await SUPERUSER(bot, event) # 判断是否为超级用户
|
||||
# 支持对自定义command_start的判断
|
||||
if sc.get("enable") or sc.get("disable"):
|
||||
|
||||
toggle = result.subcommands.get("enable") is not None
|
||||
|
||||
plugin_exist = get_plugin_exist(plugin_name)
|
||||
|
||||
# 判定会话类型
|
||||
# 输入群号
|
||||
if (
|
||||
group_id := (
|
||||
sc.get("enable", SubcommandResult())
|
||||
.options.get("group", OptionResult())
|
||||
.args.get("group_id")
|
||||
or sc.get("disable", SubcommandResult())
|
||||
.options.get("group", OptionResult())
|
||||
.args.get("group_id")
|
||||
)
|
||||
) and await SUPERUSER(bot, event):
|
||||
session_id = group_id
|
||||
new_event = event.copy()
|
||||
new_event.group_id = group_id
|
||||
new_event.message_type = "group"
|
||||
|
||||
elif event.message_type == "private":
|
||||
session_id = event.user_id
|
||||
new_event = event
|
||||
else:
|
||||
if (
|
||||
await GROUP_ADMIN(bot, event)
|
||||
or await GROUP_OWNER(bot, event)
|
||||
or await SUPERUSER(bot, event)
|
||||
):
|
||||
session_id = event.group_id
|
||||
new_event = event
|
||||
else:
|
||||
raise FinishedException(ulang.get("Permission Denied"))
|
||||
|
||||
session_enable = get_plugin_session_enable(
|
||||
new_event, plugin_name
|
||||
) # 获取插件当前状态
|
||||
|
||||
can_be_toggled = get_plugin_can_be_toggle(
|
||||
plugin_name
|
||||
) # 获取插件是否可以被启用/停用
|
||||
|
||||
if not plugin_exist:
|
||||
await npm.finish(ulang.get("npm.plugin_not_found", NAME=plugin_name))
|
||||
|
||||
if not can_be_toggled:
|
||||
await npm.finish(
|
||||
ulang.get("npm.plugin_cannot_be_toggled", NAME=plugin_name)
|
||||
)
|
||||
|
||||
if session_enable == toggle:
|
||||
await npm.finish(
|
||||
ulang.get(
|
||||
"npm.plugin_already",
|
||||
NAME=plugin_name,
|
||||
STATUS=(
|
||||
ulang.get("npm.enable") if toggle else ulang.get("npm.disable")
|
||||
),
|
||||
)
|
||||
)
|
||||
|
||||
# 键入自定义群号的情况
|
||||
|
||||
try:
|
||||
set_plugin_session_enable(new_event, plugin_name, toggle)
|
||||
except Exception as e:
|
||||
nonebot.logger.error(e)
|
||||
await npm.finish(
|
||||
ulang.get(
|
||||
"npm.toggle_failed",
|
||||
NAME=plugin_name,
|
||||
STATUS=(
|
||||
ulang.get("npm.enable") if toggle else ulang.get("npm.disable")
|
||||
),
|
||||
ERROR=str(e),
|
||||
)
|
||||
)
|
||||
|
||||
await npm.finish(
|
||||
ulang.get(
|
||||
"npm.toggle_success",
|
||||
NAME=plugin_name,
|
||||
STATUS=(
|
||||
ulang.get("npm.enable") if toggle else ulang.get("npm.disable")
|
||||
),
|
||||
) # + str(session_id) 这里应该不需增加一个id,在任何语言文件里,这句话都不是这样翻的,你是不是调试的时候忘删了?
|
||||
)
|
||||
|
||||
elif (
|
||||
sc.get(enable_global)
|
||||
or result.subcommands.get(disable_global)
|
||||
and await SUPERUSER(bot, event)
|
||||
):
|
||||
plugin_exist = get_plugin_exist(plugin_name)
|
||||
|
||||
toggle = result.subcommands.get(enable_global) is not None
|
||||
|
||||
can_be_toggled = get_plugin_can_be_toggle(plugin_name)
|
||||
|
||||
if not plugin_exist:
|
||||
await npm.finish(ulang.get("npm.plugin_not_found", NAME=plugin_name))
|
||||
|
||||
if not can_be_toggled:
|
||||
await npm.finish(
|
||||
ulang.get("npm.plugin_cannot_be_toggled", NAME=plugin_name)
|
||||
)
|
||||
|
||||
global_enable = get_plugin_global_enable(plugin_name)
|
||||
if global_enable == toggle:
|
||||
await npm.finish(
|
||||
ulang.get(
|
||||
"npm.plugin_already",
|
||||
NAME=plugin_name,
|
||||
STATUS=(
|
||||
ulang.get("npm.enable") if toggle else ulang.get("npm.disable")
|
||||
),
|
||||
)
|
||||
)
|
||||
|
||||
try:
|
||||
set_plugin_global_enable(plugin_name, toggle)
|
||||
except Exception as e:
|
||||
await npm.finish(
|
||||
ulang.get(
|
||||
"npm.toggle_failed",
|
||||
NAME=plugin_name,
|
||||
STATUS=(
|
||||
ulang.get("npm.enable") if toggle else ulang.get("npm.disable")
|
||||
),
|
||||
ERROR=str(e),
|
||||
)
|
||||
)
|
||||
|
||||
await npm.finish(
|
||||
ulang.get(
|
||||
"npm.toggle_success",
|
||||
NAME=plugin_name,
|
||||
STATUS=ulang.get("npm.enable") if toggle else ulang.get("npm.disable"),
|
||||
)
|
||||
)
|
||||
|
||||
elif sc.get("update") and perm_s:
|
||||
r = await npm_update()
|
||||
if r:
|
||||
await npm.finish(ulang.get("npm.store_update_success"))
|
||||
else:
|
||||
await npm.finish(ulang.get("npm.store_update_failed"))
|
||||
|
||||
elif sc.get("search"):
|
||||
keywords: list[str] = result.subcommands["search"].args.get("keywords")
|
||||
rs = await npm_search(keywords)
|
||||
max_show = 10
|
||||
if len(rs):
|
||||
reply = f"{ulang.get('npm.search_result')} | {ulang.get('npm.total', TOTAL=len(rs))}\n***"
|
||||
for storePlugin in rs[: min(max_show, len(rs))]:
|
||||
btn_install_or_update = md.btn_cmd(
|
||||
(
|
||||
ulang.get("npm.update")
|
||||
if get_plugin_exist(storePlugin.module_name)
|
||||
else ulang.get("npm.install")
|
||||
),
|
||||
"npm install %s" % storePlugin.module_name,
|
||||
)
|
||||
link_page = md.btn_link(ulang.get("npm.homepage"), storePlugin.homepage)
|
||||
link_pypi = md.btn_link(ulang.get("npm.pypi"), storePlugin.homepage)
|
||||
|
||||
reply += (
|
||||
f"\n# **{storePlugin.name}**\n"
|
||||
f"\n> **{storePlugin.desc}**\n"
|
||||
f"\n> {ulang.get('npm.author')}: {storePlugin.author}"
|
||||
f"\n> *{md.escape(storePlugin.module_name)}*"
|
||||
f"\n> {btn_install_or_update} {link_page} {link_pypi}\n\n***\n"
|
||||
)
|
||||
if len(rs) > max_show:
|
||||
reply += f"\n{ulang.get('npm.too_many_results', HIDE_NUM=len(rs) - max_show)}"
|
||||
else:
|
||||
reply = ulang.get("npm.search_no_result")
|
||||
img_bytes = await md_to_pic(reply)
|
||||
await UniMessage.send(UniMessage.image(raw=img_bytes))
|
||||
|
||||
elif sc.get("install") and perm_s:
|
||||
plugin_name: str = result.subcommands["install"].args.get("plugin_name")
|
||||
store_plugin = await get_store_plugin(plugin_name)
|
||||
await npm.send(ulang.get("npm.installing", NAME=plugin_name))
|
||||
|
||||
r, log = await npm_install(plugin_name)
|
||||
log = log.replace("\\", "/")
|
||||
|
||||
if not store_plugin:
|
||||
await npm.finish(ulang.get("npm.plugin_not_found", NAME=plugin_name))
|
||||
|
||||
homepage_btn = md.btn_cmd(ulang.get("npm.homepage"), store_plugin.homepage)
|
||||
if r:
|
||||
r_load = nonebot.load_plugin(plugin_name) # 加载插件
|
||||
installed_plugin = InstalledPlugin(
|
||||
module_name=plugin_name
|
||||
) # 构造插件信息模型
|
||||
found_in_db_plugin = plugin_db.where_one(
|
||||
InstalledPlugin(), "module_name = ?", plugin_name
|
||||
) # 查询数据库中是否已经安装
|
||||
if r_load:
|
||||
if found_in_db_plugin is None:
|
||||
plugin_db.save(installed_plugin)
|
||||
info = md.escape(
|
||||
ulang.get("npm.install_success", NAME=store_plugin.name)
|
||||
) # markdown转义
|
||||
await npm.send(f"{info}\n\n" + f"\n{log}\n")
|
||||
else:
|
||||
await npm.finish(
|
||||
ulang.get(
|
||||
"npm.plugin_already_installed", NAME=store_plugin.name
|
||||
)
|
||||
)
|
||||
else:
|
||||
info = ulang.get(
|
||||
"npm.load_failed", NAME=plugin_name, HOMEPAGE=homepage_btn
|
||||
).replace("_", r"\\_")
|
||||
await npm.finish(f"{info}\n\n" f"```\n{log}\n```\n")
|
||||
else:
|
||||
info = ulang.get(
|
||||
"npm.install_failed", NAME=plugin_name, HOMEPAGE=homepage_btn
|
||||
).replace("_", r"\\_")
|
||||
await npm.send(f"{info}\n\n" f"```\n{log}\n```")
|
||||
|
||||
elif sc.get("uninstall") and perm_s:
|
||||
plugin_name: str = result.subcommands["uninstall"].args.get("plugin_name") # type: ignore
|
||||
found_installed_plugin: InstalledPlugin = plugin_db.where_one(
|
||||
InstalledPlugin(), "module_name = ?", plugin_name
|
||||
)
|
||||
if found_installed_plugin:
|
||||
plugin_db.delete(InstalledPlugin(), "module_name = ?", plugin_name)
|
||||
reply = f"{ulang.get('npm.uninstall_success', NAME=found_installed_plugin.module_name)}"
|
||||
await npm.finish(reply)
|
||||
else:
|
||||
await npm.finish(ulang.get("npm.plugin_not_installed", NAME=plugin_name))
|
||||
|
||||
elif sc.get("list"):
|
||||
loaded_plugin_list = sorted(nonebot.get_loaded_plugins(), key=lambda x: x.name)
|
||||
num_per_page = result.subcommands.get("list").args.get("num")
|
||||
total = len(loaded_plugin_list) // num_per_page + (
|
||||
1 if len(loaded_plugin_list) % num_per_page else 0
|
||||
)
|
||||
|
||||
page = clamp(result.subcommands.get("list").args.get("page"), 1, total)
|
||||
|
||||
# 已加载插件 | 总计10 | 第1/3页
|
||||
reply = (
|
||||
f"# {ulang.get('npm.loaded_plugins')} | "
|
||||
f"{ulang.get('npm.total', TOTAL=len(nonebot.get_loaded_plugins()))} | "
|
||||
f"{ulang.get('npm.page', PAGE=page, TOTAL=total)} \n***\n"
|
||||
)
|
||||
|
||||
permission_oas = (
|
||||
await GROUP_ADMIN(bot, event)
|
||||
or await GROUP_OWNER(bot, event)
|
||||
or await SUPERUSER(bot, event)
|
||||
)
|
||||
permission_s = await SUPERUSER(bot, event)
|
||||
|
||||
for storePlugin in loaded_plugin_list[
|
||||
(page - 1)
|
||||
* num_per_page : min(page * num_per_page, len(loaded_plugin_list))
|
||||
]:
|
||||
# 检查是否有 metadata 属性
|
||||
# 添加帮助按钮
|
||||
|
||||
btn_usage = md.btn_cmd(
|
||||
ulang.get("npm.usage"), f"help {storePlugin.name}", False
|
||||
)
|
||||
store_plugin = await get_store_plugin(storePlugin.name)
|
||||
session_enable = get_plugin_session_enable(event, storePlugin.name)
|
||||
if store_plugin:
|
||||
# btn_homepage = md.btn_link(ulang.get("npm.homepage"), store_plugin.homepage)
|
||||
show_name = store_plugin.name
|
||||
elif storePlugin.metadata:
|
||||
# if storePlugin.metadata.extra.get("liteyuki"):
|
||||
# btn_homepage = md.btn_link(ulang.get("npm.homepage"), "https://github.com/snowykami/LiteyukiBot")
|
||||
# else:
|
||||
# btn_homepage = ulang.get("npm.homepage")
|
||||
show_name = storePlugin.metadata.name
|
||||
else:
|
||||
# btn_homepage = ulang.get("npm.homepage")
|
||||
show_name = storePlugin.name
|
||||
ulang.get("npm.no_description")
|
||||
|
||||
if storePlugin.metadata:
|
||||
reply += f"\n**{md.escape(show_name)}**\n"
|
||||
else:
|
||||
reply += f"**{md.escape(show_name)}**\n"
|
||||
|
||||
reply += f"\n > {btn_usage}"
|
||||
|
||||
if permission_oas:
|
||||
# 添加启用/停用插件按钮
|
||||
cmd_toggle = f"npm {'disable' if session_enable else 'enable'} {storePlugin.name}"
|
||||
text_toggle = ulang.get(
|
||||
"npm.disable" if session_enable else "npm.enable"
|
||||
)
|
||||
can_be_toggle = get_plugin_can_be_toggle(storePlugin.name)
|
||||
btn_toggle = (
|
||||
text_toggle
|
||||
if not can_be_toggle
|
||||
else md.btn_cmd(text_toggle, cmd_toggle)
|
||||
)
|
||||
reply += f" {btn_toggle}"
|
||||
|
||||
if permission_s:
|
||||
plugin_in_database = plugin_db.where_one(
|
||||
InstalledPlugin(), "module_name = ?", storePlugin.name
|
||||
)
|
||||
# 添加移除插件和全局切换按钮
|
||||
global_enable = get_plugin_global_enable(storePlugin.name)
|
||||
btn_uninstall = (
|
||||
(
|
||||
md.btn_cmd(
|
||||
ulang.get("npm.uninstall"),
|
||||
f"npm uninstall {storePlugin.name}",
|
||||
)
|
||||
)
|
||||
if plugin_in_database
|
||||
else ulang.get("npm.uninstall")
|
||||
)
|
||||
btn_toggle_global_text = ulang.get(
|
||||
"npm.disable_global" if global_enable else "npm.enable_global"
|
||||
)
|
||||
cmd_toggle_global = f"npm {'disable' if global_enable else 'enable'}-global {storePlugin.name}"
|
||||
btn_toggle_global = (
|
||||
btn_toggle_global_text
|
||||
if not can_be_toggle
|
||||
else md.btn_cmd(btn_toggle_global_text, cmd_toggle_global)
|
||||
)
|
||||
|
||||
reply += f" {btn_uninstall} {btn_toggle_global}"
|
||||
reply += "\n\n***\n"
|
||||
# 根据页数添加翻页按钮。第一页显示上一页文本而不是按钮,最后一页显示下一页文本而不是按钮
|
||||
btn_prev = (
|
||||
md.btn_cmd(
|
||||
ulang.get("npm.prev_page"), f"npm list {page - 1} {num_per_page}"
|
||||
)
|
||||
if page > 1
|
||||
else ulang.get("npm.prev_page")
|
||||
)
|
||||
btn_next = (
|
||||
md.btn_cmd(
|
||||
ulang.get("npm.next_page"), f"npm list {page + 1} {num_per_page}"
|
||||
)
|
||||
if page < total
|
||||
else ulang.get("npm.next_page")
|
||||
)
|
||||
reply += f"\n{btn_prev} {page}/{total} {btn_next}"
|
||||
img_bytes = await md_to_pic(reply)
|
||||
await UniMessage.send(UniMessage.image(raw=img_bytes))
|
||||
|
||||
else:
|
||||
if await SUPERUSER(bot, event):
|
||||
btn_enable_global = md.btn_cmd(
|
||||
ulang.get("npm.enable_global"), "npm enable-global", False, False
|
||||
)
|
||||
btn_disable_global = md.btn_cmd(
|
||||
ulang.get("npm.disable_global"), "npm disable-global", False, False
|
||||
)
|
||||
btn_search = md.btn_cmd(
|
||||
ulang.get("npm.search"), "npm search ", False, False
|
||||
)
|
||||
btn_uninstall_ = md.btn_cmd(
|
||||
ulang.get("npm.uninstall"), "npm uninstall ", False, False
|
||||
)
|
||||
btn_install_ = md.btn_cmd(
|
||||
ulang.get("npm.install"), "npm install ", False, False
|
||||
)
|
||||
btn_update = md.btn_cmd(
|
||||
ulang.get("npm.update_index"), "npm update", False, True
|
||||
)
|
||||
btn_list = md.btn_cmd(
|
||||
ulang.get("npm.list_plugins"), "npm list ", False, False
|
||||
)
|
||||
btn_disable = md.btn_cmd(
|
||||
ulang.get("npm.disable_session"), "npm disable ", False, False
|
||||
)
|
||||
btn_enable = md.btn_cmd(
|
||||
ulang.get("npm.enable_session"), "npm enable ", False, False
|
||||
)
|
||||
reply = (
|
||||
f"\n# **{ulang.get('npm.help')}**"
|
||||
f"\n{btn_update}"
|
||||
f"\n\n>*{md.escape('npm update')}*\n"
|
||||
f"\n{btn_install_}"
|
||||
f"\n\n>*{md.escape('npm install <plugin_name')}*>\n"
|
||||
f"\n{btn_uninstall_}"
|
||||
f"\n\n>*{md.escape('npm uninstall <plugin_name')}*>\n"
|
||||
f"\n{btn_search}"
|
||||
f"\n\n>*{md.escape('npm search <keywords...')}*>\n"
|
||||
f"\n{btn_disable_global}"
|
||||
f"\n\n>*{md.escape('npm disable-global <plugin_name')}*>\n"
|
||||
f"\n{btn_enable_global}"
|
||||
f"\n\n>*{md.escape('npm enable-global <plugin_name')}*>\n"
|
||||
f"\n{btn_disable}"
|
||||
f"\n\n>*{md.escape('npm disable <plugin_name')}*>\n"
|
||||
f"\n{btn_enable}"
|
||||
f"\n\n>*{md.escape('npm enable <plugin_name')}*>\n"
|
||||
f"\n{btn_list}"
|
||||
f"\n\n>page为页数,num为每页显示数量"
|
||||
f"\n\n>*{md.escape('npm list [page] [num]')}*"
|
||||
)
|
||||
img_bytes = await md_to_pic(reply)
|
||||
await UniMessage.send(UniMessage.image(raw=img_bytes))
|
||||
else:
|
||||
|
||||
btn_list = md.btn_cmd(
|
||||
ulang.get("npm.list_plugins"), "npm list ", False, False
|
||||
)
|
||||
btn_disable = md.btn_cmd(
|
||||
ulang.get("npm.disable_session"), "npm disable ", False, False
|
||||
)
|
||||
btn_enable = md.btn_cmd(
|
||||
ulang.get("npm.enable_session"), "npm enable ", False, False
|
||||
)
|
||||
reply = (
|
||||
f"\n# **{ulang.get('npm.help')}**"
|
||||
f"\n{btn_disable}"
|
||||
f"\n\n>*{md.escape('npm disable <plugin_name')}*>\n"
|
||||
f"\n{btn_enable}"
|
||||
f"\n\n>*{md.escape('npm enable <plugin_name')}*>\n"
|
||||
f"\n{btn_list}"
|
||||
f"\n\n>page为页数,num为每页显示数量"
|
||||
f"\n\n>*{md.escape('npm list [page] [num]')}*"
|
||||
)
|
||||
img_bytes = await md_to_pic(reply)
|
||||
await UniMessage.send(UniMessage.image(raw=img_bytes))
|
||||
|
||||
|
||||
@on_alconna(
|
||||
aliases={"群聊"},
|
||||
command=Alconna(
|
||||
"gm",
|
||||
Subcommand(
|
||||
enable,
|
||||
Args["group_id", str, None],
|
||||
alias=["e", "启用"],
|
||||
),
|
||||
Subcommand(
|
||||
disable,
|
||||
Args["group_id", str, None],
|
||||
alias=["d", "停用"],
|
||||
),
|
||||
),
|
||||
permission=SUPERUSER | GROUP_OWNER | GROUP_ADMIN,
|
||||
).handle()
|
||||
async def _(bot: T_Bot, event: T_MessageEvent, gm: Matcher, result: Arparma):
|
||||
ulang = get_user_lang(str(event.user_id))
|
||||
to_enable = result.subcommands.get(enable) is not None
|
||||
|
||||
group_id = None
|
||||
if await SUPERUSER(bot, event):
|
||||
# 仅超级用户可以自定义群号
|
||||
group_id = result.subcommands.get(
|
||||
enable, result.subcommands.get(disable)
|
||||
).args.get("group_id")
|
||||
if group_id is None and event.message_type == "group":
|
||||
group_id = str(event.group_id)
|
||||
|
||||
if group_id is None:
|
||||
await gm.finish(ulang.get("liteyuki.invalid_command"), liteyuki_pass=True)
|
||||
|
||||
enabled = get_group_enable(group_id)
|
||||
if enabled == to_enable:
|
||||
await gm.finish(
|
||||
ulang.get(
|
||||
"liteyuki.group_already",
|
||||
STATUS=(
|
||||
ulang.get("npm.enable") if to_enable else ulang.get("npm.disable")
|
||||
),
|
||||
GROUP=group_id,
|
||||
),
|
||||
liteyuki_pass=True,
|
||||
)
|
||||
else:
|
||||
set_group_enable(group_id, to_enable)
|
||||
await gm.finish(
|
||||
ulang.get(
|
||||
"liteyuki.group_success",
|
||||
STATUS=(
|
||||
ulang.get("npm.enable") if to_enable else ulang.get("npm.disable")
|
||||
),
|
||||
GROUP=group_id,
|
||||
),
|
||||
liteyuki_pass=True,
|
||||
)
|
||||
|
||||
|
||||
@on_alconna(
|
||||
aliases={"帮助"},
|
||||
command=Alconna(
|
||||
"help",
|
||||
Args["plugin_name", str, None],
|
||||
),
|
||||
).handle()
|
||||
async def _(result: Arparma, matcher: Matcher, event: T_MessageEvent, bot: T_Bot):
|
||||
ulang = get_user_lang(str(event.user_id))
|
||||
plugin_name = result.main_args.get("plugin_name")
|
||||
if plugin_name:
|
||||
searched_plugins = search_loaded_plugin(plugin_name)
|
||||
if searched_plugins:
|
||||
loaded_plugin = searched_plugins[0]
|
||||
else:
|
||||
await matcher.finish(ulang.get("npm.plugin_not_found", NAME=plugin_name))
|
||||
|
||||
if loaded_plugin:
|
||||
if loaded_plugin.metadata is None:
|
||||
loaded_plugin.metadata = PluginMetadata(
|
||||
name=plugin_name, description="", usage=""
|
||||
)
|
||||
# 从商店获取详细信息
|
||||
store_plugin = await get_store_plugin(plugin_name)
|
||||
if loaded_plugin.metadata.extra.get("liteyuki"):
|
||||
store_plugin = StorePlugin(
|
||||
name=loaded_plugin.metadata.name,
|
||||
desc=loaded_plugin.metadata.description,
|
||||
author="SnowyKami",
|
||||
module_name=plugin_name,
|
||||
homepage="https://github.com/snowykami/LiteyukiBot",
|
||||
)
|
||||
elif store_plugin is None:
|
||||
store_plugin = StorePlugin(
|
||||
name=loaded_plugin.metadata.name,
|
||||
desc=loaded_plugin.metadata.description,
|
||||
author="",
|
||||
module_name=plugin_name,
|
||||
homepage="",
|
||||
)
|
||||
|
||||
if store_plugin:
|
||||
link = store_plugin.homepage
|
||||
elif loaded_plugin.metadata.extra.get("liteyuki"):
|
||||
link = "https://github.com/snowykami/LiteyukiBot"
|
||||
else:
|
||||
link = None
|
||||
|
||||
reply = [
|
||||
mdc.heading(escape_md(store_plugin.name)),
|
||||
mdc.quote(store_plugin.module_name),
|
||||
mdc.quote(
|
||||
mdc.bold(ulang.get("npm.author"))
|
||||
+ " "
|
||||
+ (
|
||||
mdc.link(
|
||||
store_plugin.author,
|
||||
f"https://github.com/{store_plugin.author}",
|
||||
)
|
||||
if store_plugin.author
|
||||
else "Unknown"
|
||||
)
|
||||
),
|
||||
mdc.quote(
|
||||
mdc.bold(ulang.get("npm.description"))
|
||||
+ " "
|
||||
+ mdc.paragraph(
|
||||
max(loaded_plugin.metadata.description, store_plugin.desc)
|
||||
)
|
||||
),
|
||||
mdc.heading(ulang.get("npm.usage"), 2),
|
||||
mdc.paragraph(loaded_plugin.metadata.usage.replace("\n", "\n\n")),
|
||||
(
|
||||
mdc.link(ulang.get("npm.homepage"), link)
|
||||
if link
|
||||
else mdc.paragraph(ulang.get("npm.homepage"))
|
||||
),
|
||||
]
|
||||
await matcher.finish(compile_md(reply))
|
||||
else:
|
||||
await matcher.finish(ulang.get("npm.plugin_not_found", NAME=plugin_name))
|
||||
else:
|
||||
pass
|
||||
|
||||
|
||||
# 传入事件阻断hook
|
||||
@run_preprocessor
|
||||
async def pre_handle(event: Event, matcher: Matcher):
|
||||
plugin: Plugin = matcher.plugin
|
||||
plugin_global_enable = get_plugin_global_enable(plugin.name)
|
||||
if not plugin_global_enable:
|
||||
raise IgnoredException("Plugin disabled globally")
|
||||
if event.get_type() == "message":
|
||||
plugin_session_enable = get_plugin_session_enable(event, plugin.name)
|
||||
if not plugin_session_enable:
|
||||
raise IgnoredException("Plugin disabled in session")
|
||||
|
||||
|
||||
# 群聊开关阻断hook
|
||||
@Bot.on_calling_api
|
||||
async def block_disable_session(bot: Bot, api: str, args: dict):
|
||||
if "group_id" in args and not args.get("liteyuki_pass", False):
|
||||
group_id = args["group_id"]
|
||||
if not get_group_enable(group_id):
|
||||
nonebot.logger.debug(f"Group {group_id} disabled")
|
||||
raise MockApiException(f"Group {group_id} disabled")
|
||||
|
||||
|
||||
async def npm_update() -> bool:
|
||||
"""
|
||||
更新本地插件json缓存
|
||||
|
||||
Returns:
|
||||
bool: 是否成功更新
|
||||
"""
|
||||
url_list = [
|
||||
"https://registry.nonebot.dev/plugins.json",
|
||||
]
|
||||
for url in url_list:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get(url) as resp:
|
||||
if resp.status == 200:
|
||||
async with aiofiles.open("data/liteyuki/plugins.json", "wb") as f:
|
||||
data = await resp.read()
|
||||
await f.write(data)
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
async def npm_search(keywords: list[str]) -> list[StorePlugin]:
|
||||
"""
|
||||
在本地缓存商店数据中搜索插件
|
||||
|
||||
Args:
|
||||
keywords (list[str]): 关键词列表
|
||||
|
||||
Returns:
|
||||
list[StorePlugin]: 插件列表
|
||||
"""
|
||||
plugin_blacklist = [
|
||||
"nonebot_plugin_xiuxian_2",
|
||||
"nonebot_plugin_htmlrender",
|
||||
"nonebot_plugin_alconna",
|
||||
]
|
||||
|
||||
results = []
|
||||
async with aiofiles.open("data/liteyuki/plugins.json", "r", encoding="utf-8") as f:
|
||||
plugins: list[StorePlugin] = [
|
||||
StorePlugin(**pobj) for pobj in json.loads(await f.read())
|
||||
]
|
||||
for plugin in plugins:
|
||||
if plugin.module_name in plugin_blacklist:
|
||||
continue
|
||||
plugin_text = " ".join(
|
||||
[
|
||||
plugin.name,
|
||||
plugin.desc,
|
||||
plugin.author,
|
||||
plugin.module_name,
|
||||
" ".join([tag.label for tag in plugin.tags]),
|
||||
]
|
||||
)
|
||||
if all([keyword in plugin_text for keyword in keywords]):
|
||||
results.append(plugin)
|
||||
return results
|
||||
|
||||
|
||||
@run_sync
|
||||
def npm_install(plugin_package_name) -> tuple[bool, str]:
|
||||
"""
|
||||
异步安装插件,使用pip安装
|
||||
Args:
|
||||
plugin_package_name:
|
||||
|
||||
Returns:
|
||||
tuple[bool, str]: 是否成功,输出信息
|
||||
|
||||
"""
|
||||
# 重定向标准输出
|
||||
buffer = StringIO()
|
||||
sys.stdout = buffer
|
||||
sys.stderr = buffer
|
||||
|
||||
update = False
|
||||
if get_plugin_exist(plugin_package_name):
|
||||
update = True
|
||||
|
||||
mirrors = [
|
||||
"https://pypi.tuna.tsinghua.edu.cn/simple", # 清华大学
|
||||
"https://pypi.org/simple", # 官方源
|
||||
]
|
||||
|
||||
# 使用pip安装包,对每个镜像尝试一次,成功后返回值
|
||||
success = False
|
||||
for mirror in mirrors:
|
||||
try:
|
||||
nonebot.logger.info(f"pip install try mirror: {mirror}")
|
||||
if update:
|
||||
result = pip.main(
|
||||
["install", "--upgrade", plugin_package_name, "-i", mirror]
|
||||
)
|
||||
else:
|
||||
result = pip.main(["install", plugin_package_name, "-i", mirror])
|
||||
success = result == 0
|
||||
if success:
|
||||
break
|
||||
else:
|
||||
nonebot.logger.warning(f"pip install failed, try next mirror.")
|
||||
except Exception as e:
|
||||
success = False
|
||||
continue
|
||||
|
||||
sys.stdout = sys.__stdout__
|
||||
sys.stderr = sys.__stderr__
|
||||
|
||||
return success, buffer.getvalue()
|
||||
|
||||
|
||||
def search_loaded_plugin(keyword: str) -> list[Plugin]:
|
||||
"""
|
||||
搜索已加载插件
|
||||
|
||||
Args:
|
||||
keyword (str): 关键词
|
||||
|
||||
Returns:
|
||||
list[Plugin]: 插件列表
|
||||
"""
|
||||
if nonebot.get_plugin(keyword) is not None:
|
||||
return [nonebot.get_plugin(keyword)]
|
||||
else:
|
||||
results = []
|
||||
for plugin in nonebot.get_loaded_plugins():
|
||||
if plugin.metadata is None:
|
||||
plugin.metadata = PluginMetadata(
|
||||
name=plugin.name, description="", usage=""
|
||||
)
|
||||
if (
|
||||
keyword
|
||||
in plugin.name + plugin.metadata.name + plugin.metadata.description
|
||||
):
|
||||
results.append(plugin)
|
||||
return results
|
@ -0,0 +1,186 @@
|
||||
# 轻雪资源包管理器
|
||||
import os
|
||||
import zipfile
|
||||
import yaml
|
||||
from nonebot import require
|
||||
from nonebot.internal.matcher import Matcher
|
||||
from nonebot.permission import SUPERUSER
|
||||
|
||||
from src.utils.base.language import get_user_lang
|
||||
from src.utils.base.ly_typing import T_Bot, T_MessageEvent
|
||||
from src.utils.message.message import MarkdownMessage as md
|
||||
from src.utils.base.resource import (ResourceMetadata, add_resource_pack, change_priority, check_exist, check_status, get_loaded_resource_packs, get_resource_metadata, load_resources, remove_resource_pack)
|
||||
|
||||
require("nonebot_plugin_alconna")
|
||||
from nonebot_plugin_alconna import Alconna, Args, on_alconna, Arparma, Subcommand
|
||||
|
||||
|
||||
@on_alconna(
|
||||
aliases={"资源包"},
|
||||
command=Alconna(
|
||||
"rpm",
|
||||
Subcommand(
|
||||
"list",
|
||||
Args["page", int, 1]["num", int, 10],
|
||||
alias=["ls", "列表", "列出"],
|
||||
),
|
||||
Subcommand(
|
||||
"load",
|
||||
Args["name", str],
|
||||
alias=["安装"],
|
||||
),
|
||||
Subcommand(
|
||||
"unload",
|
||||
Args["name", str],
|
||||
alias=["卸载"],
|
||||
),
|
||||
Subcommand(
|
||||
"up",
|
||||
Args["name", str],
|
||||
alias=["上移"],
|
||||
),
|
||||
Subcommand(
|
||||
"down",
|
||||
Args["name", str],
|
||||
alias=["下移"],
|
||||
),
|
||||
Subcommand(
|
||||
"top",
|
||||
Args["name", str],
|
||||
alias=["置顶"],
|
||||
),
|
||||
Subcommand(
|
||||
"reload",
|
||||
alias=["重载"],
|
||||
),
|
||||
),
|
||||
permission=SUPERUSER
|
||||
).handle()
|
||||
async def _(bot: T_Bot, event: T_MessageEvent, result: Arparma, matcher: Matcher):
|
||||
ulang = get_user_lang(str(event.user_id))
|
||||
reply = ""
|
||||
send_as_md = False
|
||||
if result.subcommands.get("list"):
|
||||
send_as_md = True
|
||||
loaded_rps = get_loaded_resource_packs()
|
||||
reply += f"{ulang.get('liteyuki.loaded_resources', NUM=len(loaded_rps))}\n"
|
||||
for rp in loaded_rps:
|
||||
btn_unload = md.btn_cmd(
|
||||
ulang.get("npm.uninstall"),
|
||||
f"rpm unload {rp.folder}"
|
||||
)
|
||||
btn_move_up = md.btn_cmd(
|
||||
ulang.get("rpm.move_up"),
|
||||
f"rpm up {rp.folder}"
|
||||
)
|
||||
btn_move_down = md.btn_cmd(
|
||||
ulang.get("rpm.move_down"),
|
||||
f"rpm down {rp.folder}"
|
||||
)
|
||||
btn_move_top = md.btn_cmd(
|
||||
ulang.get("rpm.move_top"),
|
||||
f"rpm top {rp.folder}"
|
||||
)
|
||||
# 添加新行
|
||||
reply += (f"\n**{md.escape(rp.name)}**({md.escape(rp.folder)})\n\n"
|
||||
f"> {btn_move_up} {btn_move_down} {btn_move_top} {btn_unload}\n\n***")
|
||||
reply += f"\n\n{ulang.get('liteyuki.unloaded_resources')}\n"
|
||||
loaded_folders = [rp.folder for rp in get_loaded_resource_packs()]
|
||||
# 遍历resources文件夹,获取未加载的资源包
|
||||
for folder in os.listdir("resources"):
|
||||
if folder not in loaded_folders:
|
||||
if os.path.exists(os.path.join("resources", folder, "metadata.yml")):
|
||||
metadata = ResourceMetadata(
|
||||
**yaml.load(
|
||||
open(
|
||||
os.path.join("resources", folder, "metadata.yml"),
|
||||
encoding="utf-8"
|
||||
),
|
||||
Loader=yaml.FullLoader
|
||||
)
|
||||
)
|
||||
metadata.folder = folder
|
||||
metadata.path = os.path.join("resources", folder)
|
||||
btn_load = md.btn_cmd(
|
||||
ulang.get("npm.install"),
|
||||
f"rpm load {metadata.folder}"
|
||||
)
|
||||
# 添加新行
|
||||
reply += (f"\n**{md.escape(metadata.name)}**({md.escape(metadata.folder)})\n\n"
|
||||
f"> {btn_load}\n\n***")
|
||||
elif os.path.isfile(os.path.join("resources", folder)) and folder.endswith(".zip"):
|
||||
# zip文件
|
||||
# 临时解压并读取metadata.yml
|
||||
with zipfile.ZipFile(os.path.join("resources", folder), "r") as zip_ref:
|
||||
with zip_ref.open("metadata.yml") as f:
|
||||
metadata = ResourceMetadata(
|
||||
**yaml.load(f, Loader=yaml.FullLoader)
|
||||
)
|
||||
btn_load = md.btn_cmd(
|
||||
ulang.get("npm.install"),
|
||||
f"rpm load {folder}"
|
||||
)
|
||||
# 添加新行
|
||||
reply += (f"\n**{md.escape(metadata.name)}**({md.escape(folder)})\n\n"
|
||||
f"> {btn_load}\n\n***")
|
||||
elif result.subcommands.get("load") or result.subcommands.get("unload"):
|
||||
load = result.subcommands.get("load") is not None
|
||||
rp_name = result.args.get("name")
|
||||
r = False # 操作结果
|
||||
if check_exist(rp_name):
|
||||
if load != check_status(rp_name):
|
||||
# 状态不同
|
||||
if load:
|
||||
r = add_resource_pack(rp_name)
|
||||
else:
|
||||
r = remove_resource_pack(rp_name)
|
||||
rp_meta = get_resource_metadata(rp_name)
|
||||
reply += ulang.get(
|
||||
f"liteyuki.{'load' if load else 'unload'}_resource_{'success' if r else 'failed'}",
|
||||
NAME=rp_meta.name
|
||||
)
|
||||
else:
|
||||
# 重复操作
|
||||
reply += ulang.get(f"liteyuki.resource_already_{'load' if load else 'unload'}ed", NAME=rp_name)
|
||||
else:
|
||||
reply += ulang.get("liteyuki.resource_not_found", NAME=rp_name)
|
||||
if r:
|
||||
btn_reload = md.btn_cmd(
|
||||
ulang.get("liteyuki.reload_resources"),
|
||||
f"rpm reload"
|
||||
)
|
||||
reply += "\n" + ulang.get("liteyuki.need_reload", BTN=btn_reload)
|
||||
elif result.subcommands.get("up") or result.subcommands.get("down") or result.subcommands.get("top"):
|
||||
rp_name = result.args.get("name")
|
||||
if result.subcommands.get("up"):
|
||||
delta = -1
|
||||
elif result.subcommands.get("down"):
|
||||
delta = 1
|
||||
else:
|
||||
delta = 0
|
||||
if check_exist(rp_name):
|
||||
if check_status(rp_name):
|
||||
r = change_priority(rp_name, delta)
|
||||
reply += ulang.get(f"liteyuki.change_priority_{'success' if r else 'failed'}", NAME=rp_name)
|
||||
if r:
|
||||
btn_reload = md.btn_cmd(
|
||||
ulang.get("liteyuki.reload_resources"),
|
||||
f"rpm reload"
|
||||
)
|
||||
reply += "\n" + ulang.get("liteyuki.need_reload", BTN=btn_reload)
|
||||
else:
|
||||
reply += ulang.get("liteyuki.resource_not_found", NAME=rp_name)
|
||||
else:
|
||||
reply += ulang.get("liteyuki.resource_not_found", NAME=rp_name)
|
||||
elif result.subcommands.get("reload"):
|
||||
load_resources()
|
||||
reply = ulang.get(
|
||||
"liteyuki.reload_resources_success",
|
||||
NUM=len(get_loaded_resource_packs())
|
||||
)
|
||||
else:
|
||||
pass
|
||||
if send_as_md:
|
||||
await matcher.send(reply)
|
||||
else:
|
||||
await matcher.finish(reply)
|
@ -0,0 +1,18 @@
|
||||
from nonebot.plugin import PluginMetadata
|
||||
from .monitors import *
|
||||
from .matchers import *
|
||||
|
||||
|
||||
__author__ = "snowykami"
|
||||
__plugin_meta__ = PluginMetadata(
|
||||
name="轻雪智障回复",
|
||||
description="",
|
||||
usage="",
|
||||
type="application",
|
||||
homepage="https://github.com/snowykami/LiteyukiBot",
|
||||
extra={
|
||||
"liteyuki": True,
|
||||
"toggleable" : True,
|
||||
"default_enable" : True,
|
||||
}
|
||||
)
|
@ -0,0 +1,106 @@
|
||||
import asyncio
|
||||
import random
|
||||
|
||||
import nonebot
|
||||
from nonebot import Bot, on_message, get_driver, require
|
||||
from nonebot.internal.matcher import Matcher
|
||||
from nonebot.permission import SUPERUSER
|
||||
from nonebot.rule import to_me
|
||||
from nonebot.typing import T_State
|
||||
|
||||
from src.utils.base.ly_typing import T_MessageEvent
|
||||
from .utils import get_keywords
|
||||
from src.utils.base.word_bank import get_reply
|
||||
from src.utils.event import get_message_type
|
||||
from src.utils.base.permission import GROUP_ADMIN, GROUP_OWNER
|
||||
from src.utils.base.data_manager import group_db, Group
|
||||
|
||||
require("nonebot_plugin_alconna")
|
||||
from nonebot_plugin_alconna import on_alconna, Alconna, Args, Arparma
|
||||
|
||||
nicknames = set()
|
||||
driver = get_driver()
|
||||
group_reply_probability: dict[str, float] = {
|
||||
}
|
||||
default_reply_probability = 0.05
|
||||
cut_probability = 0.4 # 分几句话的概率
|
||||
|
||||
|
||||
@on_alconna(
|
||||
Alconna(
|
||||
"set-reply-probability",
|
||||
Args["probability", float, default_reply_probability],
|
||||
),
|
||||
aliases={"设置回复概率"},
|
||||
permission=SUPERUSER | GROUP_ADMIN | GROUP_OWNER,
|
||||
).handle()
|
||||
async def _(result: Arparma, event: T_MessageEvent, matcher: Matcher):
|
||||
# 修改内存和数据库的概率值
|
||||
if get_message_type(event) == "group":
|
||||
group_id = event.group_id
|
||||
probability = result.main_args.get("probability")
|
||||
# 保存到数据库
|
||||
group: Group = group_db.where_one(Group(), "group_id = ?", group_id, default=Group(group_id=str(group_id)))
|
||||
group.config["reply_probability"] = probability
|
||||
group_db.save(group)
|
||||
|
||||
await matcher.send(f"已将群组{group_id}的回复概率设置为{probability}")
|
||||
return
|
||||
|
||||
|
||||
@group_db.on_save
|
||||
def _(model: Group):
|
||||
"""
|
||||
在数据库更新时,更新内存中的回复概率
|
||||
Args:
|
||||
model:
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
group_reply_probability[model.group_id] = model.config.get("reply_probability", default_reply_probability)
|
||||
|
||||
|
||||
@driver.on_bot_connect
|
||||
async def _(bot: Bot):
|
||||
global nicknames
|
||||
nicknames.update(bot.config.nickname)
|
||||
# 从数据库加载群组的回复概率
|
||||
groups = group_db.where_all(Group(), default=[])
|
||||
for group in groups:
|
||||
group_reply_probability[group.group_id] = group.config.get("reply_probability", default_reply_probability)
|
||||
|
||||
|
||||
@on_message(priority=100).handle()
|
||||
async def _(event: T_MessageEvent, bot: Bot, state: T_State, matcher: Matcher):
|
||||
kws = await get_keywords(event.message.extract_plain_text())
|
||||
|
||||
tome = False
|
||||
if await to_me()(event=event, bot=bot, state=state):
|
||||
tome = True
|
||||
else:
|
||||
for kw in kws:
|
||||
if kw in nicknames:
|
||||
tome = True
|
||||
break
|
||||
|
||||
# 回复概率
|
||||
message_type = get_message_type(event)
|
||||
if tome or message_type == "private":
|
||||
p = 1.0
|
||||
else:
|
||||
p = group_reply_probability.get(event.group_id, default_reply_probability)
|
||||
|
||||
if random.random() < p:
|
||||
if reply := get_reply(kws):
|
||||
if random.random() < cut_probability:
|
||||
reply = reply.replace("。", "||").replace(",", "||").replace("!", "||").replace("?", "||")
|
||||
replies = reply.split("||")
|
||||
for r in replies:
|
||||
if r: # 防止空字符串
|
||||
await asyncio.sleep(random.random() * 2)
|
||||
await matcher.send(r)
|
||||
else:
|
||||
await asyncio.sleep(random.random() * 3)
|
||||
await matcher.send(reply)
|
||||
return
|
@ -0,0 +1,13 @@
|
||||
from jieba import lcut
|
||||
from nonebot.utils import run_sync
|
||||
|
||||
|
||||
@run_sync
|
||||
def get_keywords(text: str) -> list[str, ...]:
|
||||
"""
|
||||
获取关键词
|
||||
Args:
|
||||
text: 文本
|
||||
Returns:
|
||||
"""
|
||||
return lcut(text)
|
@ -0,0 +1,29 @@
|
||||
from nonebot.plugin import PluginMetadata
|
||||
from .stat_matchers import *
|
||||
from .stat_monitors import *
|
||||
from .stat_restful_api import *
|
||||
|
||||
__author__ = "snowykami"
|
||||
__plugin_meta__ = PluginMetadata(
|
||||
name="统计信息",
|
||||
description="统计机器人的信息,包括消息、群聊等,支持排名、图表等功能",
|
||||
usage=(
|
||||
"```\nstatistic message 查看统计消息\n"
|
||||
"可选参数:\n"
|
||||
" -g|--group [group_id] 指定群聊\n"
|
||||
" -u|--user [user_id] 指定用户\n"
|
||||
" -d|--duration [duration] 指定时长\n"
|
||||
" -p|--period [period] 指定次数统计周期\n"
|
||||
" -b|--bot [bot_id] 指定机器人\n"
|
||||
"命令别名:\n"
|
||||
" statistic|stat message|msg|m\n"
|
||||
"```"
|
||||
),
|
||||
type="application",
|
||||
homepage="https://github.com/snowykami/LiteyukiBot",
|
||||
extra={
|
||||
"liteyuki" : True,
|
||||
"toggleable" : False,
|
||||
"default_enable": True,
|
||||
}
|
||||
)
|
@ -0,0 +1,21 @@
|
||||
from src.utils.base.data import Database, LiteModel
|
||||
|
||||
|
||||
class MessageEventModel(LiteModel):
|
||||
TABLE_NAME: str = "message_event"
|
||||
time: int = 0
|
||||
|
||||
bot_id: str = ""
|
||||
adapter: str = ""
|
||||
|
||||
user_id: str = ""
|
||||
group_id: str = ""
|
||||
|
||||
message_id: str = ""
|
||||
message: list = []
|
||||
message_text: str = ""
|
||||
message_type: str = ""
|
||||
|
||||
|
||||
msg_db = Database("data/liteyuki/msg.ldb")
|
||||
msg_db.auto_migrate(MessageEventModel())
|
@ -0,0 +1,174 @@
|
||||
import time
|
||||
from typing import Any
|
||||
|
||||
from collections import Counter
|
||||
|
||||
from nonebot import Bot
|
||||
|
||||
from src.utils.message.html_tool import template2image
|
||||
from .common import MessageEventModel, msg_db
|
||||
from src.utils.base.language import Language
|
||||
from src.utils.base.resource import get_path
|
||||
from src.utils.message.string_tool import convert_seconds_to_time
|
||||
from src.utils.external.logo import get_group_icon, get_user_icon
|
||||
|
||||
|
||||
async def count_msg_by_bot_id(bot_id: str) -> int:
|
||||
condition = " AND bot_id = ?"
|
||||
condition_args = [bot_id]
|
||||
|
||||
msg_rows = msg_db.where_all(
|
||||
MessageEventModel(),
|
||||
condition,
|
||||
*condition_args
|
||||
)
|
||||
|
||||
return len(msg_rows)
|
||||
|
||||
|
||||
async def get_stat_msg_image(
|
||||
duration: int,
|
||||
period: int,
|
||||
group_id: str = None,
|
||||
bot_id: str = None,
|
||||
user_id: str = None,
|
||||
ulang: Language = Language()
|
||||
) -> bytes:
|
||||
"""
|
||||
获取统计消息
|
||||
Args:
|
||||
user_id:
|
||||
ulang:
|
||||
bot_id:
|
||||
group_id:
|
||||
duration: 统计时间,单位秒
|
||||
period: 统计周期,单位秒
|
||||
|
||||
Returns:
|
||||
tuple: [int,], [int,] 两个列表,分别为周期中心时间戳和消息数量
|
||||
"""
|
||||
now = int(time.time())
|
||||
start_time = (now - duration)
|
||||
|
||||
condition = "time > ?"
|
||||
condition_args = [start_time]
|
||||
|
||||
if group_id:
|
||||
condition += " AND group_id = ?"
|
||||
condition_args.append(group_id)
|
||||
if bot_id:
|
||||
condition += " AND bot_id = ?"
|
||||
condition_args.append(bot_id)
|
||||
|
||||
if user_id:
|
||||
condition += " AND user_id = ?"
|
||||
condition_args.append(user_id)
|
||||
|
||||
msg_rows = msg_db.where_all(
|
||||
MessageEventModel(),
|
||||
condition,
|
||||
*condition_args
|
||||
)
|
||||
if not msg_rows:
|
||||
msg_rows = []
|
||||
timestamps = []
|
||||
msg_count = []
|
||||
msg_rows.sort(key=lambda x: x.time)
|
||||
|
||||
start_time = max(msg_rows[0].time, start_time)
|
||||
|
||||
for i in range(start_time, now, period):
|
||||
timestamps.append(i + period // 2)
|
||||
msg_count.append(0)
|
||||
|
||||
for msg in msg_rows:
|
||||
period_start_time = start_time + (msg.time - start_time) // period * period
|
||||
period_center_time = period_start_time + period // 2
|
||||
index = timestamps.index(period_center_time)
|
||||
msg_count[index] += 1
|
||||
|
||||
templates = {
|
||||
"data": [
|
||||
{
|
||||
"name" : ulang.get("stat.message")
|
||||
+ f" Period {convert_seconds_to_time(period)}" + f" Duration {convert_seconds_to_time(duration)}"
|
||||
+ (f" Group {group_id}" if group_id else "") + (f" Bot {bot_id}" if bot_id else "") + (
|
||||
f" User {user_id}" if user_id else ""),
|
||||
"times" : timestamps,
|
||||
"counts": msg_count
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
return await template2image(get_path("templates/stat_msg.html"), templates)
|
||||
|
||||
|
||||
async def get_stat_rank_image(
|
||||
rank_type: str,
|
||||
limit: dict[str, Any],
|
||||
ulang: Language = Language(),
|
||||
bot: Bot = None,
|
||||
) -> bytes:
|
||||
if rank_type == "user":
|
||||
condition = "user_id != ''"
|
||||
condition_args = []
|
||||
else:
|
||||
condition = "group_id != ''"
|
||||
condition_args = []
|
||||
|
||||
for k, v in limit.items():
|
||||
match k:
|
||||
case "user_id":
|
||||
condition += " AND user_id = ?"
|
||||
condition_args.append(v)
|
||||
case "group_id":
|
||||
condition += " AND group_id = ?"
|
||||
condition_args.append(v)
|
||||
case "bot_id":
|
||||
condition += " AND bot_id = ?"
|
||||
condition_args.append(v)
|
||||
case "duration":
|
||||
condition += " AND time > ?"
|
||||
condition_args.append(v)
|
||||
|
||||
msg_rows = msg_db.where_all(
|
||||
MessageEventModel(),
|
||||
condition,
|
||||
*condition_args
|
||||
)
|
||||
|
||||
"""
|
||||
{
|
||||
name: string, # user name or group name
|
||||
count: int, # message count
|
||||
icon: string # icon url
|
||||
}
|
||||
"""
|
||||
|
||||
if rank_type == "user":
|
||||
ranking_counter = Counter([msg.user_id for msg in msg_rows])
|
||||
else:
|
||||
ranking_counter = Counter([msg.group_id for msg in msg_rows])
|
||||
sorted_data = sorted(ranking_counter.items(), key=lambda x: x[1], reverse=True)
|
||||
|
||||
ranking: list[dict[str, Any]] = [
|
||||
{
|
||||
"name" : _[0],
|
||||
"count": _[1],
|
||||
"icon" : await (get_group_icon(platform="qq", group_id=_[0]) if rank_type == "group" else get_user_icon(
|
||||
platform="qq", user_id=_[0]
|
||||
))
|
||||
}
|
||||
for _ in sorted_data[0:min(len(sorted_data), limit["rank"])]
|
||||
]
|
||||
|
||||
templates = {
|
||||
"data":
|
||||
{
|
||||
"name" : ulang.get("stat.rank") + f" Type {rank_type}" + f" Limit {limit}",
|
||||
"ranking": ranking
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return await template2image(get_path("templates/stat_rank.html"), templates, debug=True)
|
@ -0,0 +1,136 @@
|
||||
from nonebot import Bot, require
|
||||
from src.utils.message.string_tool import convert_duration, convert_time_to_seconds
|
||||
from .data_source import *
|
||||
from src.utils import event as event_utils
|
||||
from src.utils.base.language import Language
|
||||
from src.utils.base.ly_typing import T_MessageEvent
|
||||
|
||||
require("nonebot_plugin_alconna")
|
||||
|
||||
from nonebot_plugin_alconna import (
|
||||
UniMessage,
|
||||
on_alconna,
|
||||
Alconna,
|
||||
Args,
|
||||
Subcommand,
|
||||
Arparma,
|
||||
Option,
|
||||
MultiVar
|
||||
)
|
||||
|
||||
stat_msg = on_alconna(
|
||||
Alconna(
|
||||
"statistic",
|
||||
Subcommand(
|
||||
"message",
|
||||
# Args["duration", str, "2d"]["period", str, "60s"], # 默认为1天
|
||||
Option(
|
||||
"-d|--duration",
|
||||
Args["duration", str, "2d"],
|
||||
help_text="统计时间",
|
||||
),
|
||||
Option(
|
||||
"-p|--period",
|
||||
Args["period", str, "60s"],
|
||||
help_text="统计周期",
|
||||
),
|
||||
Option(
|
||||
"-b|--bot", # 生成图表
|
||||
Args["bot_id", str, "current"],
|
||||
help_text="是否指定机器人",
|
||||
),
|
||||
Option(
|
||||
"-g|--group",
|
||||
Args["group_id", str, "current"],
|
||||
help_text="指定群组"
|
||||
),
|
||||
Option(
|
||||
"-u|--user",
|
||||
Args["user_id", str, "current"],
|
||||
help_text="指定用户"
|
||||
),
|
||||
alias={"msg", "m"},
|
||||
help_text="查看统计次数内的消息"
|
||||
),
|
||||
Subcommand(
|
||||
"rank",
|
||||
Option(
|
||||
"-u|--user",
|
||||
help_text="以用户为指标",
|
||||
),
|
||||
Option(
|
||||
"-g|--group",
|
||||
help_text="以群组为指标",
|
||||
),
|
||||
Option(
|
||||
"-l|--limit",
|
||||
Args["limit", MultiVar(str)],
|
||||
help_text="限制参数,使用key=val格式",
|
||||
),
|
||||
Option(
|
||||
"-d|--duration",
|
||||
Args["duration", str, "1d"],
|
||||
help_text="统计时间",
|
||||
),
|
||||
Option(
|
||||
"-r|--rank",
|
||||
Args["rank", int, 20],
|
||||
help_text="指定排名",
|
||||
),
|
||||
alias={"r"},
|
||||
)
|
||||
),
|
||||
aliases={"stat"}
|
||||
)
|
||||
|
||||
|
||||
@stat_msg.assign("message")
|
||||
async def _(result: Arparma, event: T_MessageEvent, bot: Bot):
|
||||
ulang = Language(event_utils.get_user_id(event))
|
||||
try:
|
||||
duration = convert_time_to_seconds(result.other_args.get("duration", "2d")) # 秒数
|
||||
period = convert_time_to_seconds(result.other_args.get("period", "1m"))
|
||||
except BaseException as e:
|
||||
await stat_msg.send(ulang.get("liteyuki.invalid_command", TEXT=str(e.__str__())))
|
||||
return
|
||||
|
||||
group_id = result.other_args.get("group_id")
|
||||
bot_id = result.other_args.get("bot_id")
|
||||
user_id = result.other_args.get("user_id")
|
||||
|
||||
if group_id in ["current", "c"] and hasattr(event, "group_id"):
|
||||
group_id = str(event_utils.get_group_id(event))
|
||||
else:
|
||||
group_id = "all"
|
||||
|
||||
if group_id in ["all", "a"]:
|
||||
group_id = "all"
|
||||
|
||||
if bot_id in ["current", "c"]:
|
||||
bot_id = str(bot.self_id)
|
||||
|
||||
if user_id in ["current", "c"]:
|
||||
user_id = str(event_utils.get_user_id(event))
|
||||
|
||||
img = await get_stat_msg_image(duration=duration, period=period, group_id=group_id, bot_id=bot_id, user_id=user_id, ulang=ulang)
|
||||
await stat_msg.send(UniMessage.image(raw=img))
|
||||
|
||||
|
||||
@stat_msg.assign("rank")
|
||||
async def _(result: Arparma, event: T_MessageEvent, bot: Bot):
|
||||
ulang = Language(event_utils.get_user_id(event))
|
||||
rank_type = "user"
|
||||
duration = convert_time_to_seconds(result.other_args.get("duration", "1d"))
|
||||
if result.subcommands.get("rank").options.get("user"):
|
||||
rank_type = "user"
|
||||
elif result.subcommands.get("rank").options.get("group"):
|
||||
rank_type = "group"
|
||||
|
||||
limit = result.other_args.get("limit", {})
|
||||
if limit:
|
||||
limit = dict([i.split("=") for i in limit])
|
||||
limit["duration"] = time.time() - duration # 起始时间戳
|
||||
limit["rank"] = result.other_args.get("rank", 20)
|
||||
|
||||
img = await get_stat_rank_image(rank_type=rank_type, limit=limit, ulang=ulang)
|
||||
await stat_msg.send(UniMessage.image(raw=img))
|
@ -0,0 +1,92 @@
|
||||
import time
|
||||
|
||||
from nonebot import require
|
||||
from nonebot.message import event_postprocessor
|
||||
|
||||
from src.utils.base.data import Database, LiteModel
|
||||
from src.utils.base.ly_typing import v11, v12, satori
|
||||
|
||||
from src.utils.base.ly_typing import T_Bot, T_MessageEvent
|
||||
|
||||
from .common import MessageEventModel, msg_db
|
||||
from src.utils import event as event_utils
|
||||
|
||||
require("nonebot_plugin_alconna")
|
||||
|
||||
|
||||
async def general_event_monitor(bot: T_Bot, event: T_MessageEvent):
|
||||
pass
|
||||
# if isinstance(bot, satori.Bot):
|
||||
# print("POST PROCESS SATORI EVENT")
|
||||
# return await satori_event_monitor(bot, event)
|
||||
# elif isinstance(bot, v11.Bot):
|
||||
# print("POST PROCESS V11 EVENT")
|
||||
# return await onebot_v11_event_monitor(bot, event)
|
||||
|
||||
|
||||
@event_postprocessor
|
||||
async def onebot_v11_event_monitor(bot: v11.Bot, event: v11.MessageEvent):
|
||||
if event.message_type == "group":
|
||||
event: v11.GroupMessageEvent
|
||||
group_id = str(event.group_id)
|
||||
else:
|
||||
group_id = ""
|
||||
mem = MessageEventModel(
|
||||
time=int(time.time()),
|
||||
bot_id=bot.self_id,
|
||||
adapter="onebot.v11",
|
||||
group_id=group_id,
|
||||
user_id=str(event.user_id),
|
||||
|
||||
message_id=str(event.message_id),
|
||||
|
||||
message=[ms.__dict__ for ms in event.message],
|
||||
message_text=event.raw_message,
|
||||
message_type=event.message_type,
|
||||
)
|
||||
msg_db.save(mem)
|
||||
|
||||
|
||||
@event_postprocessor
|
||||
async def onebot_v12_event_monitor(bot: v12.Bot, event: v12.MessageEvent):
|
||||
if event.message_type == "group":
|
||||
event: v12.GroupMessageEvent
|
||||
group_id = str(event.group_id)
|
||||
else:
|
||||
group_id = ""
|
||||
mem = MessageEventModel(
|
||||
time=int(time.time()),
|
||||
bot_id=bot.self_id,
|
||||
adapter="onebot.v12",
|
||||
group_id=group_id,
|
||||
user_id=str(event.user_id),
|
||||
|
||||
message_id=[ms.__dict__ for ms in event.message],
|
||||
|
||||
message=event.message,
|
||||
message_text=event.raw_message,
|
||||
message_type=event.message_type,
|
||||
)
|
||||
msg_db.save(mem)
|
||||
|
||||
|
||||
@event_postprocessor
|
||||
async def satori_event_monitor(bot: satori.Bot, event: satori.MessageEvent):
|
||||
if event.guild is not None:
|
||||
event: satori.MessageEvent
|
||||
group_id = str(event.guild.id)
|
||||
else:
|
||||
group_id = ""
|
||||
|
||||
mem = MessageEventModel(
|
||||
time=int(time.time()),
|
||||
bot_id=bot.self_id,
|
||||
adapter="satori",
|
||||
group_id=group_id,
|
||||
user_id=str(event.user.id),
|
||||
message_id=[ms.__str__() for ms in event.message],
|
||||
message=event.message,
|
||||
message_text=event.message.content,
|
||||
message_type=event_utils.get_message_type(event),
|
||||
)
|
||||
msg_db.save(mem)
|
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2022 hemengyang
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
@ -0,0 +1,107 @@
|
||||
import asyncio
|
||||
import concurrent.futures
|
||||
import contextlib
|
||||
import re
|
||||
from functools import partial
|
||||
from io import BytesIO
|
||||
from random import choice
|
||||
from typing import Optional
|
||||
|
||||
import jieba
|
||||
import jieba.analyse
|
||||
import numpy as np
|
||||
from emoji import replace_emoji
|
||||
from PIL import Image
|
||||
from wordcloud import WordCloud
|
||||
|
||||
from .config import global_config, plugin_config
|
||||
|
||||
|
||||
def pre_precess(msg: str) -> str:
|
||||
"""对消息进行预处理"""
|
||||
# 去除网址
|
||||
# https://stackoverflow.com/a/17773849/9212748
|
||||
url_regex = re.compile(
|
||||
r"(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]"
|
||||
r"+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})"
|
||||
)
|
||||
msg = url_regex.sub("", msg)
|
||||
|
||||
# 去除 \u200b
|
||||
msg = re.sub(r"\u200b", "", msg)
|
||||
|
||||
# 去除 emoji
|
||||
# https://github.com/carpedm20/emoji
|
||||
msg = replace_emoji(msg)
|
||||
|
||||
return msg
|
||||
|
||||
|
||||
def analyse_message(msg: str) -> dict[str, float]:
|
||||
"""分析消息
|
||||
|
||||
分词,并统计词频
|
||||
"""
|
||||
# 设置停用词表
|
||||
if plugin_config.wordcloud_stopwords_path:
|
||||
jieba.analyse.set_stop_words(plugin_config.wordcloud_stopwords_path)
|
||||
# 加载用户词典
|
||||
if plugin_config.wordcloud_userdict_path:
|
||||
jieba.load_userdict(str(plugin_config.wordcloud_userdict_path))
|
||||
# 基于 TF-IDF 算法的关键词抽取
|
||||
# 返回所有关键词,因为设置了数量其实也只是 tags[:topK],不如交给词云库处理
|
||||
words = jieba.analyse.extract_tags(msg, topK=0, withWeight=True)
|
||||
return dict(words)
|
||||
|
||||
|
||||
def get_mask(key: str):
|
||||
"""获取 mask"""
|
||||
mask_path = plugin_config.get_mask_path(key)
|
||||
if mask_path.exists():
|
||||
return np.array(Image.open(mask_path))
|
||||
# 如果指定 mask 文件不存在,则尝试默认 mask
|
||||
default_mask_path = plugin_config.get_mask_path()
|
||||
if default_mask_path.exists():
|
||||
return np.array(Image.open(default_mask_path))
|
||||
|
||||
|
||||
def _get_wordcloud(messages: list[str], mask_key: str) -> Optional[bytes]:
|
||||
# 过滤掉命令
|
||||
command_start = tuple(i for i in global_config.command_start if i)
|
||||
message = " ".join(m for m in messages if not m.startswith(command_start))
|
||||
# 预处理
|
||||
message = pre_precess(message)
|
||||
# 分析消息。分词,并统计词频
|
||||
frequency = analyse_message(message)
|
||||
# 词云参数
|
||||
wordcloud_options = {}
|
||||
wordcloud_options.update(plugin_config.wordcloud_options)
|
||||
wordcloud_options.setdefault("font_path", str(plugin_config.wordcloud_font_path))
|
||||
wordcloud_options.setdefault("width", plugin_config.wordcloud_width)
|
||||
wordcloud_options.setdefault("height", plugin_config.wordcloud_height)
|
||||
wordcloud_options.setdefault(
|
||||
"background_color", plugin_config.wordcloud_background_color
|
||||
)
|
||||
# 如果 colormap 是列表,则随机选择一个
|
||||
colormap = (
|
||||
plugin_config.wordcloud_colormap
|
||||
if isinstance(plugin_config.wordcloud_colormap, str)
|
||||
else choice(plugin_config.wordcloud_colormap)
|
||||
)
|
||||
wordcloud_options.setdefault("colormap", colormap)
|
||||
wordcloud_options.setdefault("mask", get_mask(mask_key))
|
||||
with contextlib.suppress(ValueError):
|
||||
wordcloud = WordCloud(**wordcloud_options)
|
||||
image = wordcloud.generate_from_frequencies(frequency).to_image()
|
||||
image_bytes = BytesIO()
|
||||
image.save(image_bytes, format="PNG")
|
||||
return image_bytes.getvalue()
|
||||
|
||||
|
||||
async def get_wordcloud(messages: list[str], mask_key: str) -> Optional[bytes]:
|
||||
loop = asyncio.get_running_loop()
|
||||
pfunc = partial(_get_wordcloud, messages, mask_key)
|
||||
# 虽然不知道具体是哪里泄漏了,但是通过每次关闭线程池可以避免这个问题
|
||||
# https://github.com/he0119/nonebot-plugin-wordcloud/issues/99
|
||||
with concurrent.futures.ThreadPoolExecutor() as pool:
|
||||
return await loop.run_in_executor(pool, pfunc)
|
@ -0,0 +1,23 @@
|
||||
from nonebot.plugin import PluginMetadata
|
||||
from .status import *
|
||||
|
||||
__author__ = "snowykami"
|
||||
__plugin_meta__ = PluginMetadata(
|
||||
name="状态查看器",
|
||||
description="",
|
||||
usage=(
|
||||
"MARKDOWN### 状态查看器\n"
|
||||
"查看机器人的状态\n"
|
||||
"### 用法\n"
|
||||
"- `/status` 查看基本情况\n"
|
||||
"- `/status memory` 查看内存使用情况\n"
|
||||
"- `/status process` 查看进程情况\n"
|
||||
),
|
||||
type="application",
|
||||
homepage="https://github.com/snowykami/LiteyukiBot",
|
||||
extra={
|
||||
"liteyuki": True,
|
||||
"toggleable" : False,
|
||||
"default_enable" : True,
|
||||
}
|
||||
)
|
@ -0,0 +1,307 @@
|
||||
import platform
|
||||
import time
|
||||
|
||||
import nonebot
|
||||
import psutil
|
||||
from cpuinfo import cpuinfo
|
||||
from nonebot import require
|
||||
from nonebot.adapters import satori
|
||||
|
||||
from src.utils import __NAME__
|
||||
from liteyuki import __version__
|
||||
from src.utils.base.config import get_config
|
||||
from src.utils.base.data_manager import TempConfig, common_db
|
||||
from src.utils.base.language import Language
|
||||
from src.utils.base.resource import get_loaded_resource_packs, get_path
|
||||
from src.utils.message.html_tool import template2image
|
||||
from src.utils import satori_utils
|
||||
from .counter_for_satori import satori_counter
|
||||
from git import Repo
|
||||
|
||||
# require("nonebot_plugin_apscheduler")
|
||||
# from nonebot_plugin_apscheduler import scheduler
|
||||
|
||||
commit_hash = Repo(".").head.commit.hexsha
|
||||
|
||||
protocol_names = {
|
||||
0: "iPad",
|
||||
1: "Android Phone",
|
||||
2: "Android Watch",
|
||||
3: "Mac",
|
||||
5: "iPad",
|
||||
6: "Android Pad",
|
||||
}
|
||||
|
||||
"""
|
||||
Universal Interface
|
||||
data
|
||||
- bot
|
||||
- name: str
|
||||
icon: str
|
||||
id: int
|
||||
protocol_name: str
|
||||
groups: int
|
||||
friends: int
|
||||
message_sent: int
|
||||
message_received: int
|
||||
app_name: str
|
||||
- hardware
|
||||
- cpu
|
||||
- percent: float
|
||||
- name: str
|
||||
- mem
|
||||
- percent: float
|
||||
- total: int
|
||||
- used: int
|
||||
- free: int
|
||||
- swap
|
||||
- percent: float
|
||||
- total: int
|
||||
- used: int
|
||||
- free: int
|
||||
- disk: list
|
||||
- name: str
|
||||
- percent: float
|
||||
- total: int
|
||||
"""
|
||||
|
||||
|
||||
# status_card_cache = {} # lang -> bytes
|
||||
|
||||
|
||||
# 60s刷新一次
|
||||
# 之前写的什么鬼玩意,这么重要的功能这样写???
|
||||
# @scheduler.scheduled_job("cron", second="*/40")
|
||||
# async def refresh_status_card():
|
||||
# nonebot.logger.debug("Refreshing status card cache.")
|
||||
# global status_card_cache
|
||||
# status_card_cache = {}
|
||||
# bot_data = await get_bots_data()
|
||||
# hardware_data = await get_hardware_data()
|
||||
# liteyuki_data = await get_liteyuki_data()
|
||||
# for lang in status_card_cache.keys():
|
||||
# status_card_cache[lang] = await generate_status_card(
|
||||
# bot_data,
|
||||
# hardware_data,
|
||||
# liteyuki_data,
|
||||
# lang=lang,
|
||||
# use_cache=False
|
||||
# )
|
||||
|
||||
|
||||
# 获取状态卡片
|
||||
# bot_id 参数已经是bot参数的一部分了,不需要保留,但为了“兼容性”……
|
||||
async def generate_status_card(
|
||||
bot: dict,
|
||||
hardware: dict,
|
||||
liteyuki: dict,
|
||||
lang="zh-CN",
|
||||
bot_id="0",
|
||||
) -> bytes:
|
||||
return await template2image(
|
||||
get_path("templates/status.html", abs_path=True),
|
||||
{
|
||||
"data": {
|
||||
"bot": bot,
|
||||
"hardware": hardware,
|
||||
"liteyuki": liteyuki,
|
||||
"localization": get_local_data(lang),
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def get_local_data(lang_code) -> dict:
|
||||
lang = Language(lang_code)
|
||||
return {
|
||||
"friends": lang.get("status.friends"),
|
||||
"groups": lang.get("status.groups"),
|
||||
"plugins": lang.get("status.plugins"),
|
||||
"bots": lang.get("status.bots"),
|
||||
"message_sent": lang.get("status.message_sent"),
|
||||
"message_received": lang.get("status.message_received"),
|
||||
"cpu": lang.get("status.cpu"),
|
||||
"memory": lang.get("status.memory"),
|
||||
"swap": lang.get("status.swap"),
|
||||
"disk": lang.get("status.disk"),
|
||||
"usage": lang.get("status.usage"),
|
||||
"total": lang.get("status.total"),
|
||||
"used": lang.get("status.used"),
|
||||
"free": lang.get("status.free"),
|
||||
"days": lang.get("status.days"),
|
||||
"hours": lang.get("status.hours"),
|
||||
"minutes": lang.get("status.minutes"),
|
||||
"seconds": lang.get("status.seconds"),
|
||||
"runtime": lang.get("status.runtime"),
|
||||
"threads": lang.get("status.threads"),
|
||||
"cores": lang.get("status.cores"),
|
||||
"process": lang.get("status.process"),
|
||||
"resources": lang.get("status.resources"),
|
||||
"description": lang.get("status.description"),
|
||||
}
|
||||
|
||||
|
||||
async def get_bots_data(self_id: str = "0") -> dict:
|
||||
"""获取当前所有机器人数据
|
||||
Returns:
|
||||
"""
|
||||
result = {
|
||||
"self_id": self_id,
|
||||
"bots": [],
|
||||
}
|
||||
for bot_id, bot in nonebot.get_bots().items():
|
||||
groups = 0
|
||||
friends = 0
|
||||
status = {}
|
||||
bot_name = bot_id
|
||||
version_info = {}
|
||||
if isinstance(bot, satori.Bot):
|
||||
try:
|
||||
bot_name = (await satori_utils.user_infos.get(bot.self_id)).name
|
||||
groups = str(await satori_utils.count_groups(bot))
|
||||
friends = str(await satori_utils.count_friends(bot))
|
||||
status = {}
|
||||
version_info = await bot.get_version_info()
|
||||
except Exception:
|
||||
pass
|
||||
else:
|
||||
try:
|
||||
# API fetch
|
||||
bot_name = (await bot.get_login_info())["nickname"]
|
||||
groups = len(await bot.get_group_list())
|
||||
friends = len(await bot.get_friend_list())
|
||||
status = await bot.get_status()
|
||||
version_info = await bot.get_version_info()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
statistics = status.get("stat", {})
|
||||
app_name = version_info.get("app_name", "UnknownImplementation")
|
||||
if app_name in ["Lagrange.OneBot", "LLOneBot", "Shamrock", "NapCat.Onebot"]:
|
||||
icon = f"https://q.qlogo.cn/g?b=qq&nk={bot_id}&s=640"
|
||||
elif isinstance(bot, satori.Bot):
|
||||
app_name = "Satori"
|
||||
icon = (await bot.login_get()).user.avatar
|
||||
else:
|
||||
icon = None
|
||||
bot_data = {
|
||||
"name": bot_name,
|
||||
"icon": icon,
|
||||
"id": bot_id,
|
||||
"protocol_name": protocol_names.get(
|
||||
version_info.get("protocol_name"), "Online"
|
||||
),
|
||||
"groups": groups,
|
||||
"friends": friends,
|
||||
"message_sent": (
|
||||
satori_counter.msg_sent
|
||||
if isinstance(bot, satori.Bot)
|
||||
else statistics.get("message_sent", 0)
|
||||
),
|
||||
"message_received": (
|
||||
satori_counter.msg_received
|
||||
if isinstance(bot, satori.Bot)
|
||||
else statistics.get("message_received", 0)
|
||||
),
|
||||
"app_name": app_name,
|
||||
}
|
||||
result["bots"].append(bot_data)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
async def get_hardware_data() -> dict:
|
||||
mem = psutil.virtual_memory()
|
||||
all_processes = psutil.Process().children(recursive=True)
|
||||
all_processes.append(psutil.Process())
|
||||
|
||||
mem_used_process = 0
|
||||
process_mem = {}
|
||||
for process in all_processes:
|
||||
try:
|
||||
ps_name = process.name().replace(".exe", "")
|
||||
if ps_name not in process_mem:
|
||||
process_mem[ps_name] = 0
|
||||
process_mem[ps_name] += process.memory_info().rss
|
||||
mem_used_process += process.memory_info().rss
|
||||
except Exception:
|
||||
pass
|
||||
swap = psutil.swap_memory()
|
||||
cpu_brand_raw = cpuinfo.get_cpu_info().get("brand_raw", "Unknown")
|
||||
if "amd" in cpu_brand_raw.lower():
|
||||
brand = "AMD"
|
||||
elif "intel" in cpu_brand_raw.lower():
|
||||
brand = "Intel"
|
||||
elif "apple" in cpu_brand_raw.lower():
|
||||
brand = "Apple"
|
||||
elif "qualcomm" in cpu_brand_raw.lower():
|
||||
brand = "Qualcomm"
|
||||
elif "mediatek" in cpu_brand_raw.lower():
|
||||
brand = "MediaTek"
|
||||
elif "samsung" in cpu_brand_raw.lower():
|
||||
brand = "Samsung"
|
||||
elif "nvidia" in cpu_brand_raw.lower():
|
||||
brand = "NVIDIA"
|
||||
else:
|
||||
brand = "Unknown"
|
||||
result = {
|
||||
"cpu": {
|
||||
"percent": psutil.cpu_percent(),
|
||||
"name": f"{brand} {cpuinfo.get_cpu_info().get('arch', 'Unknown')}",
|
||||
"cores": psutil.cpu_count(logical=False),
|
||||
"threads": psutil.cpu_count(logical=True),
|
||||
"freq": psutil.cpu_freq().current, # MHz
|
||||
},
|
||||
"memory": {
|
||||
"percent": mem.percent,
|
||||
"total": mem.total,
|
||||
"used": mem.used,
|
||||
"free": mem.free,
|
||||
"usedProcess": mem_used_process,
|
||||
},
|
||||
"swap": {
|
||||
"percent": swap.percent,
|
||||
"total": swap.total,
|
||||
"used": swap.used,
|
||||
"free": swap.free,
|
||||
},
|
||||
"disk": [],
|
||||
}
|
||||
|
||||
for disk in psutil.disk_partitions(all=True):
|
||||
try:
|
||||
disk_usage = psutil.disk_usage(disk.mountpoint)
|
||||
if disk_usage.total == 0 or disk.mountpoint.startswith(
|
||||
("/var", "/boot", "/run", "/proc", "/sys", "/dev", "/tmp", "/snap")
|
||||
):
|
||||
continue # 虚拟磁盘
|
||||
result["disk"].append(
|
||||
{
|
||||
"name": disk.mountpoint,
|
||||
"percent": disk_usage.percent,
|
||||
"total": disk_usage.total,
|
||||
"used": disk_usage.used,
|
||||
"free": disk_usage.free,
|
||||
}
|
||||
)
|
||||
except:
|
||||
pass
|
||||
|
||||
return result
|
||||
|
||||
|
||||
async def get_liteyuki_data() -> dict:
|
||||
temp_data: TempConfig = common_db.where_one(TempConfig(), default=TempConfig())
|
||||
result = {
|
||||
"name": list(get_config("nickname", [__NAME__]))[0],
|
||||
"version": f"{__version__}{'-' + commit_hash[:7] if (commit_hash and len(commit_hash) > 8) else ''}",
|
||||
"plugins": len(nonebot.get_loaded_plugins()),
|
||||
"resources": len(get_loaded_resource_packs()),
|
||||
"nonebot": f"{nonebot.__version__}",
|
||||
"python": f"{platform.python_implementation()} {platform.python_version()}",
|
||||
"system": f"{platform.system()} {platform.release()}",
|
||||
"runtime": time.time()
|
||||
- temp_data.data.get("start_time", time.time()), # 运行时间秒数
|
||||
"bots": len(nonebot.get_bots()),
|
||||
}
|
||||
return result
|
@ -0,0 +1,10 @@
|
||||
class SatoriCounter:
|
||||
msg_sent: int
|
||||
msg_received: int
|
||||
|
||||
def __init__(self):
|
||||
self.msg_sent = 0
|
||||
self.msg_received = 0
|
||||
|
||||
|
||||
satori_counter = SatoriCounter()
|
@ -0,0 +1,60 @@
|
||||
from src.utils import event as event_utils
|
||||
from src.utils.base.language import get_user_lang
|
||||
from src.utils.base.ly_typing import T_Bot, T_MessageEvent
|
||||
from .api import *
|
||||
|
||||
require("nonebot_plugin_alconna")
|
||||
from nonebot_plugin_alconna import on_alconna, Alconna, Subcommand, UniMessage
|
||||
|
||||
status_alc = on_alconna(
|
||||
aliases={"状态"},
|
||||
command=Alconna(
|
||||
"status",
|
||||
Subcommand(
|
||||
"memory",
|
||||
alias={"mem", "m", "内存"},
|
||||
),
|
||||
Subcommand(
|
||||
"process",
|
||||
alias={"proc", "p", "进程"},
|
||||
),
|
||||
Subcommand(
|
||||
"refresh",
|
||||
alias={"refr", "r", "刷新"},
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
status_card_cache = {} # lang -> bytes
|
||||
|
||||
|
||||
@status_alc.handle()
|
||||
async def _(event: T_MessageEvent, bot: T_Bot):
|
||||
ulang = get_user_lang(event_utils.get_user_id(event))
|
||||
global status_card_cache
|
||||
if ulang.lang_code not in status_card_cache.keys() or (
|
||||
ulang.lang_code in status_card_cache.keys()
|
||||
and time.time() - status_card_cache[ulang.lang_code][1] > 60
|
||||
):
|
||||
status_card_cache[ulang.lang_code] = (
|
||||
await generate_status_card(
|
||||
bot=await get_bots_data(),
|
||||
hardware=await get_hardware_data(),
|
||||
liteyuki=await get_liteyuki_data(),
|
||||
lang=ulang.lang_code,
|
||||
bot_id=bot.self_id,
|
||||
),
|
||||
time.time(),
|
||||
)
|
||||
image = status_card_cache[ulang.lang_code][0]
|
||||
await status_alc.finish(UniMessage.image(raw=image))
|
||||
|
||||
|
||||
@status_alc.assign("memory")
|
||||
async def _():
|
||||
pass
|
||||
|
||||
|
||||
@status_alc.assign("process")
|
||||
async def _():
|
||||
pass
|
@ -0,0 +1,17 @@
|
||||
from nonebot.plugin import PluginMetadata
|
||||
from .api import *
|
||||
|
||||
__author__ = "snowykami"
|
||||
__plugin_meta__ = PluginMetadata(
|
||||
name="联合黑名单(测试中...)",
|
||||
description="",
|
||||
usage="",
|
||||
type="application",
|
||||
homepage="https://github.com/snowykami/LiteyukiBot",
|
||||
extra={
|
||||
"liteyuki": True,
|
||||
"toggleable" : True,
|
||||
"default_enable" : True,
|
||||
}
|
||||
)
|
||||
|
@ -0,0 +1,58 @@
|
||||
import datetime
|
||||
|
||||
import aiohttp
|
||||
import nonebot
|
||||
from nonebot import require
|
||||
from nonebot.exception import IgnoredException
|
||||
from nonebot.message import event_preprocessor
|
||||
from nonebot_plugin_alconna.typings import Event
|
||||
|
||||
require("nonebot_plugin_apscheduler")
|
||||
|
||||
from nonebot_plugin_apscheduler import scheduler
|
||||
|
||||
blacklist_data: dict[str, set[str]] = {}
|
||||
blacklist: set[str] = set()
|
||||
|
||||
|
||||
@scheduler.scheduled_job("interval", minutes=10, next_run_time=datetime.datetime.now())
|
||||
async def update_blacklist():
|
||||
await request_for_blacklist()
|
||||
|
||||
|
||||
async def request_for_blacklist():
|
||||
global blacklist
|
||||
urls = [
|
||||
"https://cdn.liteyuki.icu/static/ubl/"
|
||||
]
|
||||
|
||||
platforms = [
|
||||
"qq"
|
||||
]
|
||||
|
||||
for plat in platforms:
|
||||
for url in urls:
|
||||
url += f"{plat}.txt"
|
||||
async with aiohttp.ClientSession() as client:
|
||||
resp = await client.get(url)
|
||||
blacklist_data[plat] = set((await resp.text()).splitlines())
|
||||
blacklist = get_uni_set()
|
||||
nonebot.logger.info("blacklists updated")
|
||||
|
||||
|
||||
def get_uni_set() -> set:
|
||||
s = set()
|
||||
for new_set in blacklist_data.values():
|
||||
s.update(new_set)
|
||||
return s
|
||||
|
||||
|
||||
@event_preprocessor
|
||||
async def pre_handle(event: Event):
|
||||
try:
|
||||
user_id = str(event.get_user_id())
|
||||
except:
|
||||
return
|
||||
|
||||
if user_id in get_uni_set():
|
||||
raise IgnoredException("UserId in blacklist")
|
@ -0,0 +1,16 @@
|
||||
from nonebot.plugin import PluginMetadata
|
||||
|
||||
from .profile_manager import *
|
||||
|
||||
__author__ = "snowykami"
|
||||
__plugin_meta__ = PluginMetadata(
|
||||
name="轻雪用户管理",
|
||||
description="用户管理插件",
|
||||
usage="",
|
||||
homepage="https://github.com/snowykami/LiteyukiBot",
|
||||
extra={
|
||||
"liteyuki" : True,
|
||||
"toggleable" : False,
|
||||
"default_enable": True,
|
||||
}
|
||||
)
|
@ -0,0 +1,23 @@
|
||||
representative_timezones_list = [
|
||||
"Etc/GMT+12", # 国际日期变更线西
|
||||
"Pacific/Honolulu", # 夏威夷标准时间
|
||||
"America/Anchorage", # 阿拉斯加标准时间
|
||||
"America/Los_Angeles", # 美国太平洋标准时间
|
||||
"America/Denver", # 美国山地标准时间
|
||||
"America/Chicago", # 美国中部标准时间
|
||||
"America/New_York", # 美国东部标准时间
|
||||
"Europe/London", # 英国标准时间
|
||||
"Europe/Paris", # 中欧标准时间
|
||||
"Europe/Moscow", # 莫斯科标准时间
|
||||
"Asia/Dubai", # 阿联酋标准时间
|
||||
"Asia/Kolkata", # 印度标准时间
|
||||
"Asia/Shanghai", # 中国标准时间
|
||||
"Asia/Hong_Kong", # 中国香港标准时间
|
||||
"Asia/Chongqing", # 中国重庆标准时间
|
||||
"Asia/Macau", # 中国澳门标准时间
|
||||
"Asia/Taipei", # 中国台湾标准时间
|
||||
"Asia/Tokyo", # 日本标准时间
|
||||
"Australia/Sydney", # 澳大利亚东部标准时间
|
||||
"Pacific/Auckland" # 新西兰标准时间
|
||||
]
|
||||
representative_timezones_list.sort()
|
@ -0,0 +1,157 @@
|
||||
from typing import Optional
|
||||
|
||||
import pytz
|
||||
from nonebot import require
|
||||
|
||||
from src.utils.base.data import LiteModel, Database
|
||||
from src.utils.base.data_manager import User, user_db, group_db
|
||||
from src.utils.base.language import Language, change_user_lang, get_all_lang, get_user_lang
|
||||
from src.utils.base.ly_typing import T_Bot, T_MessageEvent
|
||||
from src.utils.message.message import MarkdownMessage as md
|
||||
from src.utils.message.html_tool import md_to_pic
|
||||
from .const import representative_timezones_list
|
||||
from src.utils import event as event_utils
|
||||
|
||||
|
||||
require("nonebot_plugin_alconna")
|
||||
from nonebot_plugin_alconna import Alconna, Args, Arparma, Subcommand, on_alconna
|
||||
|
||||
|
||||
profile_alc = on_alconna(
|
||||
Alconna(
|
||||
"profile",
|
||||
Subcommand(
|
||||
"set",
|
||||
Args["key", str]["value", str, None],
|
||||
alias=["s", "设置"],
|
||||
),
|
||||
Subcommand(
|
||||
"get",
|
||||
Args["key", str],
|
||||
alias=["g", "查询"],
|
||||
),
|
||||
),
|
||||
aliases={"用户信息"}
|
||||
)
|
||||
|
||||
|
||||
# json储存
|
||||
class Profile(LiteModel):
|
||||
lang: str = "zh-CN"
|
||||
nickname: str = ""
|
||||
timezone: str = "Asia/Shanghai"
|
||||
location: str = ""
|
||||
|
||||
|
||||
@profile_alc.handle()
|
||||
async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot):
|
||||
user: User = user_db.where_one(User(), "user_id = ?", event_utils.get_user_id(event),
|
||||
default=User(user_id=str(event_utils.get_user_id(event))))
|
||||
ulang = get_user_lang(str(event_utils.get_user_id(event)))
|
||||
if result.subcommands.get("set"):
|
||||
if result.subcommands["set"].args.get("value"):
|
||||
# 对合法性进行校验后设置
|
||||
r = set_profile(result.args["key"], result.args["value"], str(event_utils.get_user_id(event)))
|
||||
if r:
|
||||
user.profile[result.args["key"]] = result.args["value"]
|
||||
user_db.save(user) # 数据库保存
|
||||
await profile_alc.finish(
|
||||
ulang.get(
|
||||
"user.profile.set_success",
|
||||
ATTR=ulang.get(f"user.profile.{result.args['key']}"),
|
||||
VALUE=result.args["value"]
|
||||
)
|
||||
)
|
||||
else:
|
||||
await profile_alc.finish(ulang.get("user.profile.set_failed", ATTR=ulang.get(f"user.profile.{result.args['key']}")))
|
||||
else:
|
||||
# 未输入值,尝试呼出菜单
|
||||
menu = get_profile_menu(result.args["key"], ulang)
|
||||
if menu:
|
||||
# 请问这是在做什么?
|
||||
img_bytes = await md_to_pic(menu)
|
||||
await profile_alc.finish(menu)
|
||||
else:
|
||||
await profile_alc.finish(ulang.get("user.profile.input_value", ATTR=ulang.get(f"user.profile.{result.args['key']}")))
|
||||
|
||||
user.profile[result.args["key"]] = result.args["value"]
|
||||
|
||||
elif result.subcommands.get("get"):
|
||||
if result.args["key"] in user.profile:
|
||||
await profile_alc.finish(user.profile[result.args["key"]])
|
||||
else:
|
||||
await profile_alc.finish("无此键值")
|
||||
else:
|
||||
profile = Profile(**user.profile)
|
||||
|
||||
for k, v in user.profile.items():
|
||||
profile.__setattr__(k, v)
|
||||
|
||||
reply = f"# {ulang.get('user.profile.info')}\n***\n"
|
||||
|
||||
hidden_attr = ["id", "TABLE_NAME"]
|
||||
enter_attr = ["lang", "timezone"]
|
||||
|
||||
for key in sorted(profile.dict().keys()):
|
||||
if key in hidden_attr:
|
||||
continue
|
||||
val = profile.dict()[key]
|
||||
key_text = ulang.get(f"user.profile.{key}")
|
||||
btn_set = md.btn_cmd(ulang.get("user.profile.edit"), f"profile set {key}",
|
||||
enter=True if key in enter_attr else False)
|
||||
reply += (f"\n**{key_text}** **{val}**\n"
|
||||
f"\n> {ulang.get(f'user.profile.{key}.desc')}"
|
||||
f"\n> {btn_set} \n\n***\n")
|
||||
# 这又是在做什么
|
||||
img_bytes = await md_to_pic(reply)
|
||||
await profile_alc.finish(reply)
|
||||
|
||||
|
||||
def get_profile_menu(key: str, ulang: Language) -> Optional[str]:
|
||||
"""获取属性的markdown菜单
|
||||
Args:
|
||||
ulang: 用户语言
|
||||
key: 属性键
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
setting_name = ulang.get(f"user.profile.{key}")
|
||||
|
||||
no_menu = ["id", "nickname", "location"]
|
||||
|
||||
if key in no_menu:
|
||||
return None
|
||||
|
||||
reply = f"**{setting_name} {ulang.get('user.profile.settings')}**\n***\n"
|
||||
if key == "lang":
|
||||
for lang_code, lang_name in get_all_lang().items():
|
||||
btn_set_lang = md.btn_cmd(f"{lang_name}({lang_code})", f"profile set {key} {lang_code}")
|
||||
reply += f"\n{btn_set_lang}\n***\n"
|
||||
elif key == "timezone":
|
||||
for tz in representative_timezones_list:
|
||||
btn_set_tz = md.btn_cmd(tz, f"profile set {key} {tz}")
|
||||
reply += f"{btn_set_tz}\n***\n"
|
||||
return reply
|
||||
|
||||
|
||||
def set_profile(key: str, value: str, user_id: str) -> bool:
|
||||
"""设置属性,使用if分支对每一个合法性进行检查
|
||||
Args:
|
||||
user_id:
|
||||
key:
|
||||
value:
|
||||
|
||||
Returns:
|
||||
是否成功设置,输入合法性不通过返回False
|
||||
|
||||
"""
|
||||
if key == "lang":
|
||||
if value in get_all_lang():
|
||||
change_user_lang(user_id, value)
|
||||
return True
|
||||
elif key == "timezone":
|
||||
if value in pytz.all_timezones:
|
||||
return True
|
||||
elif key == "nickname":
|
||||
return True
|
@ -0,0 +1,27 @@
|
||||
from nonebot.plugin import PluginMetadata
|
||||
from nonebot import get_driver
|
||||
from .qweather import *
|
||||
|
||||
__plugin_meta__ = PluginMetadata(
|
||||
name="轻雪天气",
|
||||
description="基于和风天气api的天气插件",
|
||||
usage="",
|
||||
type="application",
|
||||
homepage="https://github.com/snowykami/LiteyukiBot",
|
||||
extra={
|
||||
"liteyuki" : True,
|
||||
"toggleable" : True,
|
||||
"default_enable": True,
|
||||
}
|
||||
)
|
||||
|
||||
from src.utils.base.data_manager import set_memory_data
|
||||
|
||||
driver = get_driver()
|
||||
|
||||
|
||||
@driver.on_startup
|
||||
async def _():
|
||||
# 检查是否为开发者模式
|
||||
is_dev = await check_key_dev(get_config("weather_key", ""))
|
||||
set_memory_data("weather.is_dev", is_dev)
|
@ -0,0 +1,197 @@
|
||||
import aiohttp
|
||||
|
||||
from .qw_models import *
|
||||
import httpx
|
||||
|
||||
from src.utils.base.data_manager import get_memory_data
|
||||
from src.utils.base.language import Language
|
||||
|
||||
dev_url = "https://devapi.qweather.com/" # 开发HBa
|
||||
com_url = "https://api.qweather.com/" # 正式环境
|
||||
|
||||
|
||||
def get_qw_lang(lang: str) -> str:
|
||||
if lang in ["zh-HK", "zh-TW"]:
|
||||
return "zh-hant"
|
||||
elif lang.startswith("zh"):
|
||||
return "zh"
|
||||
elif lang.startswith("en"):
|
||||
return "en"
|
||||
else:
|
||||
return lang
|
||||
|
||||
|
||||
async def check_key_dev(key: str) -> bool:
|
||||
url = "https://api.qweather.com/v7/weather/now?"
|
||||
params = {
|
||||
"location": "101010100",
|
||||
"key" : key,
|
||||
}
|
||||
async with aiohttp.ClientSession() as client:
|
||||
resp = await client.get(url, params=params)
|
||||
return (await resp.json()).get("code") != "200" # 查询不到付费数据为开发版
|
||||
|
||||
|
||||
def get_local_data(ulang_code: str) -> dict:
|
||||
"""
|
||||
获取本地化数据
|
||||
Args:
|
||||
ulang_code:
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
ulang = Language(ulang_code)
|
||||
return {
|
||||
"monday" : ulang.get("weather.monday"),
|
||||
"tuesday" : ulang.get("weather.tuesday"),
|
||||
"wednesday" : ulang.get("weather.wednesday"),
|
||||
"thursday" : ulang.get("weather.thursday"),
|
||||
"friday" : ulang.get("weather.friday"),
|
||||
"saturday" : ulang.get("weather.saturday"),
|
||||
"sunday" : ulang.get("weather.sunday"),
|
||||
"today" : ulang.get("weather.today"),
|
||||
"tomorrow" : ulang.get("weather.tomorrow"),
|
||||
"day" : ulang.get("weather.day"),
|
||||
"night" : ulang.get("weather.night"),
|
||||
"no_aqi" : ulang.get("weather.no_aqi"),
|
||||
"now-windVelocity" : ulang.get("weather.now-windVelocity"),
|
||||
"now-humidity" : ulang.get("weather.now-humidity"),
|
||||
"now-feelsLike" : ulang.get("weather.now-feelsLike"),
|
||||
"now-precip" : ulang.get("weather.now-precip"),
|
||||
"now-pressure" : ulang.get("weather.now-pressure"),
|
||||
"now-vis" : ulang.get("weather.now-vis"),
|
||||
"now-cloud" : ulang.get("weather.now-cloud"),
|
||||
"astronomy-sunrise" : ulang.get("weather.astronomy-sunrise"),
|
||||
"astronomy-sunset" : ulang.get("weather.astronomy-sunset"),
|
||||
}
|
||||
|
||||
|
||||
async def city_lookup(
|
||||
location: str,
|
||||
key: str,
|
||||
adm: str = "",
|
||||
number: int = 20,
|
||||
lang: str = "zh",
|
||||
) -> CityLookup:
|
||||
"""
|
||||
通过关键字搜索城市信息
|
||||
Args:
|
||||
location:
|
||||
key:
|
||||
adm:
|
||||
number:
|
||||
lang: 可传入标准i18n语言代码,如zh-CN、en-US等
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
url = "https://geoapi.qweather.com/v2/city/lookup?"
|
||||
params = {
|
||||
"location": location,
|
||||
"adm" : adm,
|
||||
"number" : number,
|
||||
"key" : key,
|
||||
"lang" : lang,
|
||||
}
|
||||
async with httpx.AsyncClient() as client:
|
||||
resp = await client.get(url, params=params)
|
||||
return CityLookup.parse_obj(resp.json())
|
||||
|
||||
|
||||
async def get_weather_now(
|
||||
key: str,
|
||||
location: str,
|
||||
lang: str = "zh",
|
||||
unit: str = "m",
|
||||
dev: bool = get_memory_data("is_dev", True),
|
||||
) -> dict:
|
||||
url_path = "v7/weather/now?"
|
||||
url = dev_url + url_path if dev else com_url + url_path
|
||||
params = {
|
||||
"location": location,
|
||||
"key" : key,
|
||||
"lang" : lang,
|
||||
"unit" : unit,
|
||||
}
|
||||
async with httpx.AsyncClient() as client:
|
||||
resp = await client.get(url, params=params)
|
||||
return resp.json()
|
||||
|
||||
|
||||
async def get_weather_daily(
|
||||
key: str,
|
||||
location: str,
|
||||
lang: str = "zh",
|
||||
unit: str = "m",
|
||||
dev: bool = get_memory_data("is_dev", True),
|
||||
) -> dict:
|
||||
url_path = "v7/weather/%dd?" % (7 if dev else 30)
|
||||
url = dev_url + url_path if dev else com_url + url_path
|
||||
params = {
|
||||
"location": location,
|
||||
"key" : key,
|
||||
"lang" : lang,
|
||||
"unit" : unit,
|
||||
}
|
||||
async with httpx.AsyncClient() as client:
|
||||
resp = await client.get(url, params=params)
|
||||
return resp.json()
|
||||
|
||||
|
||||
async def get_weather_hourly(
|
||||
key: str,
|
||||
location: str,
|
||||
lang: str = "zh",
|
||||
unit: str = "m",
|
||||
dev: bool = get_memory_data("is_dev", True),
|
||||
) -> dict:
|
||||
url_path = "v7/weather/%dh?" % (24 if dev else 168)
|
||||
url = dev_url + url_path if dev else com_url + url_path
|
||||
params = {
|
||||
"location": location,
|
||||
"key" : key,
|
||||
"lang" : lang,
|
||||
"unit" : unit,
|
||||
}
|
||||
async with httpx.AsyncClient() as client:
|
||||
resp = await client.get(url, params=params)
|
||||
return resp.json()
|
||||
|
||||
|
||||
async def get_airquality(
|
||||
key: str,
|
||||
location: str,
|
||||
lang: str,
|
||||
pollutant: bool = False,
|
||||
station: bool = False,
|
||||
dev: bool = get_memory_data("is_dev", True),
|
||||
) -> dict:
|
||||
url_path = f"airquality/v1/now/{location}?"
|
||||
url = dev_url + url_path if dev else com_url + url_path
|
||||
params = {
|
||||
"key" : key,
|
||||
"lang" : lang,
|
||||
"pollutant": pollutant,
|
||||
"station" : station,
|
||||
}
|
||||
async with httpx.AsyncClient() as client:
|
||||
resp = await client.get(url, params=params)
|
||||
return resp.json()
|
||||
|
||||
async def get_astronomy(
|
||||
key: str,
|
||||
location: str,
|
||||
date: str,
|
||||
dev: bool = get_memory_data("is_dev", True),
|
||||
) -> dict:
|
||||
url_path = f"v7/astronomy/sun?"
|
||||
url = dev_url + url_path if dev else com_url + url_path
|
||||
params = {
|
||||
"key" : key,
|
||||
"location" : location,
|
||||
"date" : date,
|
||||
}
|
||||
async with httpx.AsyncClient() as client:
|
||||
resp = await client.get(url, params=params)
|
||||
return resp.json()
|
@ -0,0 +1,62 @@
|
||||
from src.utils.base.data import LiteModel
|
||||
|
||||
|
||||
class Location(LiteModel):
|
||||
name: str = ""
|
||||
id: str = ""
|
||||
lat: str = ""
|
||||
lon: str = ""
|
||||
adm2: str = ""
|
||||
adm1: str = ""
|
||||
country: str = ""
|
||||
tz: str = ""
|
||||
utcOffset: str = ""
|
||||
isDst: str = ""
|
||||
type: str = ""
|
||||
rank: str = ""
|
||||
fxLink: str = ""
|
||||
sources: str = ""
|
||||
license: str = ""
|
||||
|
||||
|
||||
class CityLookup(LiteModel):
|
||||
code: str = ""
|
||||
location: list[Location] = [Location()]
|
||||
|
||||
|
||||
class Now(LiteModel):
|
||||
obsTime: str = ""
|
||||
temp: str = ""
|
||||
feelsLike: str = ""
|
||||
icon: str = ""
|
||||
text: str = ""
|
||||
wind360: str = ""
|
||||
windDir: str = ""
|
||||
windScale: str = ""
|
||||
windSpeed: str = ""
|
||||
humidity: str = ""
|
||||
precip: str = ""
|
||||
pressure: str = ""
|
||||
vis: str = ""
|
||||
cloud: str = ""
|
||||
dew: str = ""
|
||||
sources: str = ""
|
||||
license: str = ""
|
||||
|
||||
|
||||
class WeatherNow(LiteModel):
|
||||
code: str = ""
|
||||
updateTime: str = ""
|
||||
fxLink: str = ""
|
||||
now: Now = Now()
|
||||
|
||||
|
||||
class Daily(LiteModel):
|
||||
pass
|
||||
|
||||
|
||||
class WeatherDaily(LiteModel):
|
||||
code: str = ""
|
||||
updateTime: str = ""
|
||||
fxLink: str = ""
|
||||
daily: list[str] = []
|
@ -0,0 +1,112 @@
|
||||
import datetime
|
||||
|
||||
from nonebot import require, on_endswith
|
||||
from nonebot.adapters import satori
|
||||
from nonebot.adapters.onebot.v11 import MessageSegment
|
||||
from nonebot.internal.matcher import Matcher
|
||||
|
||||
from src.utils.base.config import get_config
|
||||
from src.utils.base.ly_typing import T_MessageEvent
|
||||
|
||||
from .qw_api import *
|
||||
from src.utils.base.data_manager import User, user_db
|
||||
from src.utils.base.language import Language, get_user_lang
|
||||
from src.utils.base.resource import get_path
|
||||
from src.utils.message.html_tool import template2image
|
||||
from src.utils import event as event_utils
|
||||
|
||||
require("nonebot_plugin_alconna")
|
||||
from nonebot_plugin_alconna import on_alconna, Alconna, Args, MultiVar, Arparma, UniMessage
|
||||
|
||||
wx_alc = on_alconna(
|
||||
aliases={"天气"},
|
||||
command=Alconna(
|
||||
"weather",
|
||||
Args["keywords", MultiVar(str), []],
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
@wx_alc.handle()
|
||||
async def _(result: Arparma, event: T_MessageEvent, matcher: Matcher):
|
||||
"""await alconna.send("weather", city)"""
|
||||
kws = result.main_args.get("keywords")
|
||||
image = await get_weather_now_card(matcher, event, kws)
|
||||
await wx_alc.finish(UniMessage.image(raw=image))
|
||||
|
||||
|
||||
@on_endswith(("天气", "weather")).handle()
|
||||
async def _(event: T_MessageEvent, matcher: Matcher):
|
||||
"""await alconna.send("weather", city)"""
|
||||
# kws = event.message.extract_plain_text()
|
||||
kws = event.get_plaintext()
|
||||
image = await get_weather_now_card(matcher, event, [kws.replace("天气", "").replace("weather", "")], False)
|
||||
if isinstance(event, satori.event.Event):
|
||||
await matcher.finish(satori.MessageSegment.image(raw=image, mime="image/png"))
|
||||
else:
|
||||
await matcher.finish(MessageSegment.image(image))
|
||||
|
||||
|
||||
async def get_weather_now_card(matcher: Matcher, event: T_MessageEvent, keyword: list[str], tip: bool = True):
|
||||
ulang = get_user_lang(event_utils.get_user_id(event))
|
||||
qw_lang = get_qw_lang(ulang.lang_code)
|
||||
key = get_config("weather_key")
|
||||
is_dev = get_memory_data("weather.is_dev", True)
|
||||
extra_info = get_config("weather_extra_info")
|
||||
attr = get_config("weather_attr")
|
||||
|
||||
user: User = user_db.where_one(User(), "user_id = ?", event_utils.get_user_id(event), default=User())
|
||||
# params
|
||||
unit = user.profile.get("unit", "m")
|
||||
stored_location = user.profile.get("location", None)
|
||||
|
||||
if not key:
|
||||
await matcher.finish(ulang.get("weather.no_key") if tip else None)
|
||||
|
||||
if keyword:
|
||||
if len(keyword) >= 2:
|
||||
adm = keyword[0]
|
||||
city = keyword[-1]
|
||||
else:
|
||||
adm = ""
|
||||
city = keyword[0]
|
||||
city_info = await city_lookup(city, key, adm=adm, lang=qw_lang)
|
||||
city_name = " ".join(keyword)
|
||||
else:
|
||||
if not stored_location:
|
||||
await matcher.finish(ulang.get("liteyuki.invalid_command", TEXT="location") if tip else None)
|
||||
city_info = await city_lookup(stored_location, key, lang=qw_lang)
|
||||
city_name = stored_location
|
||||
if city_info.code == "200":
|
||||
location_data = city_info.location[0]
|
||||
else:
|
||||
await matcher.finish(ulang.get("weather.city_not_found", CITY=city_name) if tip else None)
|
||||
weather_now = await get_weather_now(key, location_data.id, lang=qw_lang, unit=unit, dev=is_dev)
|
||||
weather_daily = await get_weather_daily(key, location_data.id, lang=qw_lang, unit=unit, dev=is_dev)
|
||||
weather_hourly = await get_weather_hourly(key, location_data.id, lang=qw_lang, unit=unit, dev=is_dev)
|
||||
aqi = await get_airquality(key, location_data.id, lang=qw_lang, dev=is_dev)
|
||||
weather_astronomy = await get_astronomy(key, location_data.id, date=datetime.datetime.now().strftime('%Y%m%d'), dev=is_dev)
|
||||
|
||||
image = await template2image(
|
||||
template=get_path("templates/weather_now.html", abs_path=True),
|
||||
templates={
|
||||
"data": {
|
||||
"params" : {
|
||||
"unit": unit,
|
||||
"lang": ulang.lang_code,
|
||||
},
|
||||
"weatherNow" : weather_now,
|
||||
"weatherDaily" : weather_daily,
|
||||
"weatherHourly" : weather_hourly,
|
||||
"aqi" : aqi,
|
||||
"location" : location_data.dump(),
|
||||
"localization" : get_local_data(ulang.lang_code),
|
||||
"weatherAstronomy" : weather_astronomy,
|
||||
"is_dev" : 1 if is_dev else 0,
|
||||
"extra_info" : extra_info,
|
||||
"attr" : attr
|
||||
|
||||
}
|
||||
},
|
||||
)
|
||||
return image
|
@ -0,0 +1,23 @@
|
||||
from nonebot.plugin import PluginMetadata
|
||||
from .npm import *
|
||||
from .rpm import *
|
||||
|
||||
__author__ = "snowykami"
|
||||
__plugin_meta__ = PluginMetadata(
|
||||
name="轻雪包管理器v2",
|
||||
description="npm & rpm",
|
||||
usage=(
|
||||
"npm list\n"
|
||||
"npm enable/disable <plugin_name>\n"
|
||||
"npm search <keywords...>\n"
|
||||
"npm install/uninstall <plugin_name>\n"
|
||||
),
|
||||
type="application",
|
||||
homepage="https://github.com/snowykami/LiteyukiBot",
|
||||
extra={
|
||||
"liteyuki" : True,
|
||||
"toggleable" : False,
|
||||
"always_on" : True,
|
||||
"default_enable": False,
|
||||
}
|
||||
)
|
@ -0,0 +1,69 @@
|
||||
# npm update/upgrade
|
||||
# npm search
|
||||
# npm install/uninstall
|
||||
# npm list
|
||||
from nonebot import require
|
||||
|
||||
require("nonebot_plugin_alconna")
|
||||
|
||||
from nonebot_plugin_alconna import (
|
||||
on_alconna,
|
||||
Alconna,
|
||||
Args,
|
||||
MultiVar,
|
||||
Subcommand,
|
||||
Option
|
||||
)
|
||||
|
||||
"""包管理器alc"""
|
||||
npm_alc = on_alconna(
|
||||
aliases={"插件", "nonebot-plugin-manager"},
|
||||
command=Alconna(
|
||||
"npm",
|
||||
Subcommand(
|
||||
"list",
|
||||
Args["page", int, 1]["num", int, 10],
|
||||
alias={"ls", "列表", "列出"},
|
||||
dest="list installed plugins",
|
||||
help_text="列出已安装插件",
|
||||
),
|
||||
Subcommand(
|
||||
"search",
|
||||
Args["keywords", MultiVar(str)],
|
||||
alias=["s", "搜索"],
|
||||
dest="search plugins",
|
||||
help_text="搜索本地商店插件,需自行更新",
|
||||
),
|
||||
Subcommand(
|
||||
"install",
|
||||
Args["package_name", str],
|
||||
alias=["i", "安装"],
|
||||
dest="install plugin",
|
||||
help_text="安装插件",
|
||||
),
|
||||
Subcommand(
|
||||
"uninstall",
|
||||
Args["package_name", str],
|
||||
alias=["u", "卸载"],
|
||||
dest="uninstall plugin",
|
||||
help_text="卸载插件",
|
||||
),
|
||||
Subcommand(
|
||||
"update",
|
||||
alias={"更新"},
|
||||
dest="update local store index",
|
||||
help_text="更新本地索引库",
|
||||
),
|
||||
Subcommand(
|
||||
"upgrade",
|
||||
Args["package_name", str],
|
||||
Option(
|
||||
"package_name",
|
||||
Args["package_name", str, None], # Optional
|
||||
),
|
||||
alias={"升级"},
|
||||
dest="upgrade all plugins without package name",
|
||||
help_text="升级插件",
|
||||
),
|
||||
),
|
||||
)
|
@ -0,0 +1,31 @@
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
import aiofiles
|
||||
from pydantic import BaseModel
|
||||
|
||||
from src.utils.base.config import get_config
|
||||
from src.utils.io import fetch
|
||||
|
||||
NONEBOT_PLUGIN_STORE_URL: str = "https://registry.nonebot.dev/plugins.json" # NoneBot商店地址
|
||||
LITEYUKI_PLUGIN_STORE_URL: str = "https://bot.liteyuki.icu/assets/plugins.json" # 轻雪商店地址
|
||||
|
||||
|
||||
class Session:
|
||||
def __init__(self, session_type: str, session_id: int | str):
|
||||
self.session_type = session_type
|
||||
self.session_id = session_id
|
||||
|
||||
|
||||
async def update_local_store_index() -> list[str]:
|
||||
"""
|
||||
更新本地插件索引库
|
||||
Returns:
|
||||
新增插件包名列表list[str]
|
||||
"""
|
||||
url = "https://registry.nonebot.dev/plugins.json"
|
||||
save_file = Path(get_config("data_path"), "data/liteyuki") / "pacman/plugins.json"
|
||||
raw_text = await fetch(url)
|
||||
data = json.loads(raw_text)
|
||||
with aiofiles.open(save_file, "w") as f:
|
||||
await f.write(raw_text)
|
55
src/liteyuki_plugins/nonebot/nonebot_plugins/to_liteyuki.py
Normal file
55
src/liteyuki_plugins/nonebot/nonebot_plugins/to_liteyuki.py
Normal file
@ -0,0 +1,55 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Copyright (C) 2020-2024 LiteyukiStudio. All Rights Reserved
|
||||
|
||||
@Time : 2024/8/20 上午5:10
|
||||
@Author : snowykami
|
||||
@Email : snowykami@outlook.com
|
||||
@File : to_liteyuki.py
|
||||
@Software: PyCharm
|
||||
"""
|
||||
import asyncio
|
||||
|
||||
from nonebot import Bot, get_bot, on_message, get_driver
|
||||
from nonebot.plugin import PluginMetadata
|
||||
from nonebot.adapters.onebot.v11 import MessageEvent, Bot
|
||||
|
||||
from liteyuki import Channel
|
||||
from liteyuki.comm import get_channel
|
||||
from liteyuki.comm.storage import shared_memory
|
||||
from liteyuki.message.event import MessageEvent as LiteyukiMessageEvent
|
||||
|
||||
__plugin_meta__ = PluginMetadata(
|
||||
name="轻雪push",
|
||||
description="把消息事件传递给轻雪框架进行处理",
|
||||
usage="用户无需使用",
|
||||
)
|
||||
|
||||
recv_channel = Channel[LiteyukiMessageEvent](name="event_to_nonebot")
|
||||
|
||||
|
||||
# @on_message().handle()
|
||||
# async def _(bot: Bot, event: MessageEvent):
|
||||
# liteyuki_event = LiteyukiMessageEvent(
|
||||
# message_type=event.message_type,
|
||||
# message=event.dict()["message"],
|
||||
# raw_message=event.raw_message,
|
||||
# data=event.dict(),
|
||||
# bot_id=bot.self_id,
|
||||
# user_id=str(event.user_id),
|
||||
# session_id=str(event.user_id if event.message_type == "private" else event.group_id),
|
||||
# session_type=event.message_type,
|
||||
# receive_channel=recv_channel,
|
||||
# )
|
||||
# shared_memory.publish("event_to_liteyuki", liteyuki_event)
|
||||
|
||||
|
||||
# @get_driver().on_bot_connect
|
||||
# async def _():
|
||||
# while True:
|
||||
# event = await recv_channel.async_receive()
|
||||
# bot: Bot = get_bot(event.bot_id) # type: ignore
|
||||
# if event.message_type == "private":
|
||||
# await bot.send_private_msg(user_id=int(event.session_id), message=event.data["message"])
|
||||
# elif event.message_type == "group":
|
||||
# await bot.send_group_msg(group_id=int(event.session_id), message=event.data["message"])
|
@ -0,0 +1,21 @@
|
||||
from nonebot.plugin import PluginMetadata
|
||||
|
||||
from .main import *
|
||||
|
||||
__author__ = "snowykami"
|
||||
__plugin_meta__ = PluginMetadata(
|
||||
name="网页监控面板",
|
||||
description="网页监控面板,用于查看机器人的状态和信息",
|
||||
usage=(
|
||||
"访问 127.0.0.1:port 查看机器人的状态信息\n"
|
||||
"stat msg -g|--group [group_id] 查看群的统计信息,不带参数为全群\n"
|
||||
"配置项:custom_domain,自定义域名,通常对外用,内网无需"
|
||||
),
|
||||
type="application",
|
||||
homepage="https://github.com/snowykami/LiteyukiBot",
|
||||
extra={
|
||||
"liteyuki" : True,
|
||||
"toggleable" : False,
|
||||
"default_enable": True,
|
||||
}
|
||||
)
|
@ -0,0 +1,4 @@
|
||||
from fastapi import FastAPI
|
||||
from nonebot import get_app
|
||||
|
||||
app: FastAPI = get_app()
|
10
src/liteyuki_plugins/nonebot/nonebot_plugins/webdash/main.py
Normal file
10
src/liteyuki_plugins/nonebot/nonebot_plugins/webdash/main.py
Normal file
@ -0,0 +1,10 @@
|
||||
from fastapi import FastAPI
|
||||
from nonebot import get_app
|
||||
from .restful_api import *
|
||||
|
||||
|
||||
@app.get("/ping")
|
||||
async def root():
|
||||
return {
|
||||
"message": "pong"
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
from fastapi import FastAPI, APIRouter
|
||||
from .common import *
|
||||
|
||||
device_info_router = APIRouter(prefix="/api/device-info")
|
||||
bot_info_router = APIRouter(prefix="/api/bot-info")
|
||||
|
||||
|
||||
@device_info_router.get("/")
|
||||
async def device_info():
|
||||
return {
|
||||
"message": "Hello Device Info"
|
||||
}
|
||||
|
||||
|
||||
@bot_info_router.get("/")
|
||||
async def bot_info():
|
||||
return {
|
||||
"message": "Hello Bot Info"
|
||||
}
|
||||
|
||||
|
||||
app.include_router(device_info_router)
|
||||
app.include_router(bot_info_router)
|
20
src/liteyuki_plugins/nonebot/np_main/__init__.py
Normal file
20
src/liteyuki_plugins/nonebot/np_main/__init__.py
Normal file
@ -0,0 +1,20 @@
|
||||
from nonebot.plugin import PluginMetadata
|
||||
|
||||
from .core import *
|
||||
from .loader import *
|
||||
__author__ = "snowykami"
|
||||
__plugin_meta__ = PluginMetadata(
|
||||
name="轻雪核心插件",
|
||||
description="轻雪主程序插件,包含了许多初始化的功能",
|
||||
usage="",
|
||||
homepage="https://github.com/snowykami/LiteyukiBot",
|
||||
extra={
|
||||
"liteyuki" : True,
|
||||
"toggleable": False,
|
||||
}
|
||||
)
|
||||
|
||||
from src.utils.base.language import Language, get_default_lang_code
|
||||
|
||||
sys_lang = Language(get_default_lang_code())
|
||||
nonebot.logger.info(sys_lang.get("main.current_language", LANG=sys_lang.get("language.name")))
|
47
src/liteyuki_plugins/nonebot/np_main/api.py
Normal file
47
src/liteyuki_plugins/nonebot/np_main/api.py
Normal file
@ -0,0 +1,47 @@
|
||||
import nonebot
|
||||
from git import Repo
|
||||
|
||||
from src.utils.base.config import get_config
|
||||
|
||||
remote_urls = [
|
||||
"https://github.com/LiteyukiStudio/LiteyukiBot.git",
|
||||
"https://gitee.com/snowykami/LiteyukiBot.git"
|
||||
]
|
||||
|
||||
|
||||
def detect_update() -> bool:
|
||||
# 对每个远程仓库进行检查,只要有一个仓库有更新,就返回True
|
||||
for remote_url in remote_urls:
|
||||
repo = Repo(".")
|
||||
repo.remotes.origin.set_url(remote_url)
|
||||
repo.remotes.origin.fetch()
|
||||
if repo.head.commit != repo.commit('origin/main'):
|
||||
return True
|
||||
|
||||
|
||||
def update_liteyuki() -> tuple[bool, str]:
|
||||
"""更新轻雪
|
||||
:return: 是否更新成功,更新变动"""
|
||||
|
||||
if get_config("allow_update", True):
|
||||
new_commit_detected = detect_update()
|
||||
if new_commit_detected:
|
||||
repo = Repo(".")
|
||||
logs = ""
|
||||
# 对每个远程仓库进行更新
|
||||
for remote_url in remote_urls:
|
||||
try:
|
||||
logs += f"\nremote: {remote_url}"
|
||||
repo.remotes.origin.set_url(remote_url)
|
||||
repo.remotes.origin.pull()
|
||||
diffs = repo.head.commit.diff("origin/main")
|
||||
for diff in diffs.iter_change_type('M'):
|
||||
logs += f"\n{diff.a_path}"
|
||||
return True, logs
|
||||
except:
|
||||
continue
|
||||
else:
|
||||
return False, "Nothing Changed"
|
||||
|
||||
else:
|
||||
raise PermissionError("Update is not allowed.")
|
301
src/liteyuki_plugins/nonebot/np_main/core.py
Normal file
301
src/liteyuki_plugins/nonebot/np_main/core.py
Normal file
@ -0,0 +1,301 @@
|
||||
import time
|
||||
from typing import AnyStr
|
||||
|
||||
import time
|
||||
from typing import AnyStr
|
||||
|
||||
import nonebot
|
||||
import pip
|
||||
from nonebot import get_driver, require
|
||||
from nonebot.adapters import onebot, satori
|
||||
from nonebot.adapters.onebot.v11 import Message, unescape
|
||||
from nonebot.internal.matcher import Matcher
|
||||
from nonebot.permission import SUPERUSER
|
||||
|
||||
# from src.liteyuki.core import Reloader
|
||||
from src.utils import event as event_utils, satori_utils
|
||||
from src.utils.base.config import get_config
|
||||
from src.utils.base.data_manager import TempConfig, common_db
|
||||
from src.utils.base.language import get_user_lang
|
||||
from src.utils.base.ly_typing import T_Bot, T_MessageEvent
|
||||
from src.utils.message.message import MarkdownMessage as md, broadcast_to_superusers
|
||||
from .api import update_liteyuki # type: ignore
|
||||
from src.utils.base import reload # type: ignore
|
||||
from src.utils.base.ly_function import get_function # type: ignore
|
||||
from src.utils.message.html_tool import md_to_pic
|
||||
|
||||
require("nonebot_plugin_alconna")
|
||||
require("nonebot_plugin_apscheduler")
|
||||
from nonebot_plugin_alconna import UniMessage, on_alconna, Alconna, Args, Arparma, MultiVar
|
||||
from nonebot_plugin_apscheduler import scheduler
|
||||
|
||||
|
||||
driver = get_driver()
|
||||
|
||||
|
||||
@on_alconna(
|
||||
command=Alconna(
|
||||
"liteecho",
|
||||
Args["text", str, ""],
|
||||
),
|
||||
permission=SUPERUSER
|
||||
).handle()
|
||||
# Satori OK
|
||||
async def _(bot: T_Bot, matcher: Matcher, result: Arparma):
|
||||
if text := result.main_args.get("text"):
|
||||
await matcher.finish(Message(unescape(text)))
|
||||
else:
|
||||
await matcher.finish(f"Hello, Liteyuki!\nBot {bot.self_id}")
|
||||
|
||||
|
||||
@on_alconna(
|
||||
aliases={"更新轻雪"},
|
||||
command=Alconna(
|
||||
"update-liteyuki"
|
||||
),
|
||||
permission=SUPERUSER
|
||||
).handle()
|
||||
# Satori OK
|
||||
async def _(bot: T_Bot, event: T_MessageEvent, matcher: Matcher):
|
||||
# 使用git pull更新
|
||||
|
||||
ulang = get_user_lang(str(event.user.id if isinstance(event, satori.event.Event) else event.user_id))
|
||||
success, logs = update_liteyuki()
|
||||
reply = "Liteyuki updated!\n"
|
||||
reply += f"```\n{logs}\n```\n"
|
||||
btn_restart = md.btn_cmd(ulang.get("liteyuki.restart_now"), "reload-liteyuki")
|
||||
pip.main(["install", "-r", "requirements.txt"])
|
||||
reply += f"{ulang.get('liteyuki.update_restart', RESTART=btn_restart)}"
|
||||
# await md.send_md(reply, bot)
|
||||
img_bytes = await md_to_pic(reply)
|
||||
await UniMessage.send(UniMessage.image(raw=img_bytes))
|
||||
|
||||
|
||||
@on_alconna(
|
||||
aliases={"重启轻雪"},
|
||||
command=Alconna(
|
||||
"reload-liteyuki"
|
||||
),
|
||||
permission=SUPERUSER
|
||||
).handle()
|
||||
# Satori OK
|
||||
async def _(matcher: Matcher, bot: T_Bot, event: T_MessageEvent):
|
||||
await matcher.send("Liteyuki reloading")
|
||||
temp_data = common_db.where_one(TempConfig(), default=TempConfig())
|
||||
|
||||
temp_data.data.update(
|
||||
{
|
||||
"reload" : True,
|
||||
"reload_time" : time.time(),
|
||||
"reload_bot_id" : bot.self_id,
|
||||
"reload_session_type": event_utils.get_message_type(event),
|
||||
"reload_session_id" : (event.group_id if event.message_type == "group" else event.user_id)
|
||||
if not isinstance(event, satori.event.Event) else event.chan_active.id,
|
||||
"delta_time" : 0
|
||||
}
|
||||
)
|
||||
|
||||
common_db.save(temp_data)
|
||||
reload()
|
||||
|
||||
|
||||
@on_alconna(
|
||||
command=Alconna(
|
||||
"liteyuki-docs",
|
||||
),
|
||||
aliases={"轻雪文档"},
|
||||
).handle()
|
||||
# Satori OK
|
||||
async def _(matcher: Matcher):
|
||||
await matcher.finish("https://bot.liteyuki.icu/")
|
||||
|
||||
|
||||
@on_alconna(
|
||||
command=Alconna(
|
||||
"/function",
|
||||
Args["function", str]["args", MultiVar(str), ()],
|
||||
),
|
||||
permission=SUPERUSER
|
||||
).handle()
|
||||
async def _(result: Arparma, bot: T_Bot, event: T_MessageEvent, matcher: Matcher):
|
||||
"""
|
||||
调用轻雪函数
|
||||
Args:
|
||||
result:
|
||||
bot:
|
||||
event:
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
function_name = result.main_args.get("function")
|
||||
args: tuple[str] = result.main_args.get("args", ())
|
||||
_args = []
|
||||
_kwargs = {
|
||||
"USER_ID" : str(event.user_id),
|
||||
"GROUP_ID": str(event.group_id) if event.message_type == "group" else "0",
|
||||
"BOT_ID" : str(bot.self_id)
|
||||
}
|
||||
|
||||
for arg in args:
|
||||
arg = arg.replace("\\=", "EQUAL_SIGN")
|
||||
if "=" in arg:
|
||||
key, value = arg.split("=", 1)
|
||||
value = unescape(value.replace("EQUAL_SIGN", "="))
|
||||
try:
|
||||
value = eval(value)
|
||||
except:
|
||||
value = value
|
||||
_kwargs[key] = value
|
||||
else:
|
||||
_args.append(arg.replace("EQUAL_SIGN", "="))
|
||||
|
||||
ly_func = get_function(function_name)
|
||||
ly_func.bot = bot if "BOT_ID" not in _kwargs else nonebot.get_bot(_kwargs["BOT_ID"])
|
||||
ly_func.matcher = matcher
|
||||
|
||||
await ly_func(*tuple(_args), **_kwargs)
|
||||
|
||||
|
||||
@on_alconna(
|
||||
command=Alconna(
|
||||
"/api",
|
||||
Args["api", str]["args", MultiVar(AnyStr), ()],
|
||||
),
|
||||
permission=SUPERUSER
|
||||
).handle()
|
||||
async def _(result: Arparma, bot: T_Bot, event: T_MessageEvent, matcher: Matcher):
|
||||
"""
|
||||
调用API
|
||||
Args:
|
||||
result:
|
||||
bot:
|
||||
event:
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
api_name = result.main_args.get("api")
|
||||
args: tuple[str] = result.main_args.get("args", ()) # 类似于url参数,但每个参数间用空格分隔,空格是%20
|
||||
args_dict = {}
|
||||
|
||||
for arg in args:
|
||||
key, value = arg.split("=", 1)
|
||||
|
||||
args_dict[key] = unescape(value.replace("%20", " "))
|
||||
|
||||
if api_name in need_user_id and "user_id" not in args_dict:
|
||||
args_dict["user_id"] = str(event.user_id)
|
||||
if api_name in need_group_id and "group_id" not in args_dict and event.message_type == "group":
|
||||
args_dict["group_id"] = str(event.group_id)
|
||||
|
||||
if "message" in args_dict:
|
||||
args_dict["message"] = Message(eval(args_dict["message"]))
|
||||
|
||||
if "messages" in args_dict:
|
||||
args_dict["messages"] = Message(eval(args_dict["messages"]))
|
||||
|
||||
try:
|
||||
result = await bot.call_api(api_name, **args_dict)
|
||||
except Exception as e:
|
||||
result = str(e)
|
||||
|
||||
args_show = "\n".join("- %s: %s" % (k, v) for k, v in args_dict.items())
|
||||
await matcher.finish(f"API: {api_name}\n\nArgs: \n{args_show}\n\nResult: {result}")
|
||||
|
||||
|
||||
@driver.on_startup
|
||||
async def on_startup():
|
||||
temp_data = common_db.where_one(TempConfig(), default=TempConfig())
|
||||
# 储存重启信息
|
||||
if temp_data.data.get("reload", False):
|
||||
delta_time = time.time() - temp_data.data.get("reload_time", 0)
|
||||
temp_data.data["delta_time"] = delta_time
|
||||
common_db.save(temp_data) # 更新数据
|
||||
"""
|
||||
该部分将迁移至轻雪生命周期
|
||||
Returns:
|
||||
|
||||
"""
|
||||
|
||||
|
||||
@driver.on_shutdown
|
||||
async def on_shutdown():
|
||||
pass
|
||||
|
||||
|
||||
@driver.on_bot_connect
|
||||
async def _(bot: T_Bot):
|
||||
temp_data = common_db.where_one(TempConfig(), default=TempConfig())
|
||||
if isinstance(bot, satori.Bot):
|
||||
await satori_utils.user_infos.load_friends(bot)
|
||||
# 用于重启计时
|
||||
if temp_data.data.get("reload", False):
|
||||
temp_data.data["reload"] = False
|
||||
reload_bot_id = temp_data.data.get("reload_bot_id", 0)
|
||||
if reload_bot_id != bot.self_id:
|
||||
return
|
||||
reload_session_type = temp_data.data.get("reload_session_type", "private")
|
||||
reload_session_id = temp_data.data.get("reload_session_id", 0)
|
||||
delta_time = temp_data.data.get("delta_time", 0)
|
||||
common_db.save(temp_data) # 更新数据
|
||||
|
||||
if delta_time <= 20.0: # 启动时间太长就别发了,丢人
|
||||
if isinstance(bot, satori.Bot):
|
||||
await bot.send_message(
|
||||
channel_id=reload_session_id,
|
||||
message="Liteyuki reloaded in %.2f s" % delta_time
|
||||
)
|
||||
elif isinstance(bot, onebot.v11.Bot):
|
||||
await bot.send_msg(
|
||||
message_type=reload_session_type,
|
||||
user_id=reload_session_id,
|
||||
group_id=reload_session_id,
|
||||
message="Liteyuki reloaded in %.2f s" % delta_time
|
||||
)
|
||||
|
||||
elif isinstance(bot, onebot.v12.Bot):
|
||||
await bot.send_message(
|
||||
message_type=reload_session_type,
|
||||
user_id=reload_session_id,
|
||||
group_id=reload_session_id,
|
||||
message="Liteyuki reloaded in %.2f s" % delta_time,
|
||||
detail_type="group"
|
||||
)
|
||||
|
||||
|
||||
# 每天4点更新
|
||||
@scheduler.scheduled_job("cron", hour=4)
|
||||
async def every_day_update():
|
||||
if get_config("auto_update", default=True):
|
||||
result, logs = update_liteyuki()
|
||||
pip.main(["install", "-r", "requirements.txt"])
|
||||
if result:
|
||||
await broadcast_to_superusers(f"Liteyuki updated: ```\n{logs}\n```")
|
||||
nonebot.logger.info(f"Liteyuki updated: {logs}")
|
||||
reload()
|
||||
else:
|
||||
nonebot.logger.info(logs)
|
||||
|
||||
|
||||
# 需要用户id的api
|
||||
need_user_id = (
|
||||
"send_private_msg",
|
||||
"send_msg",
|
||||
"set_group_card",
|
||||
"set_group_special_title",
|
||||
"get_stranger_info",
|
||||
"get_group_member_info"
|
||||
)
|
||||
|
||||
need_group_id = (
|
||||
"send_group_msg",
|
||||
"send_msg",
|
||||
"set_group_card",
|
||||
"set_group_name",
|
||||
|
||||
"set_group_special_title",
|
||||
"get_group_member_info",
|
||||
"get_group_member_list",
|
||||
"get_group_honor_info"
|
||||
)
|
39
src/liteyuki_plugins/nonebot/np_main/loader.py
Normal file
39
src/liteyuki_plugins/nonebot/np_main/loader.py
Normal file
@ -0,0 +1,39 @@
|
||||
import asyncio
|
||||
import os.path
|
||||
from pathlib import Path
|
||||
|
||||
import nonebot.plugin
|
||||
from nonebot import get_driver
|
||||
from src.utils import init_log
|
||||
from src.utils.base.config import get_config
|
||||
from src.utils.base.data_manager import InstalledPlugin, plugin_db
|
||||
from src.utils.base.resource import load_resources
|
||||
from src.utils.message.tools import check_for_package
|
||||
|
||||
load_resources()
|
||||
init_log()
|
||||
|
||||
driver = get_driver()
|
||||
|
||||
|
||||
@driver.on_startup
|
||||
async def load_plugins():
|
||||
print("load from", os.path.join(os.path.dirname(__file__), "../nonebot_plugins"))
|
||||
nonebot.plugin.load_plugins(os.path.abspath(os.path.join(os.path.dirname(__file__), "../nonebot_plugins")))
|
||||
# 从数据库读取已安装的插件
|
||||
if not get_config("safe_mode", False):
|
||||
# 安全模式下,不加载插件
|
||||
installed_plugins: list[InstalledPlugin] = plugin_db.where_all(
|
||||
InstalledPlugin()
|
||||
)
|
||||
if installed_plugins:
|
||||
for installed_plugin in installed_plugins:
|
||||
if not check_for_package(installed_plugin.module_name):
|
||||
nonebot.logger.error(
|
||||
f"{installed_plugin.module_name} not installed, but still in loader index."
|
||||
)
|
||||
else:
|
||||
nonebot.load_plugin(installed_plugin.module_name)
|
||||
nonebot.plugin.load_plugins("plugins")
|
||||
else:
|
||||
nonebot.logger.info("Safe mode is on, no plugin loaded.")
|
0
src/liteyuki_plugins/nonebot/np_main/uitls.py
Normal file
0
src/liteyuki_plugins/nonebot/np_main/uitls.py
Normal file
Reference in New Issue
Block a user