mirror of
https://github.com/LiteyukiStudio/LiteyukiBot.git
synced 2025-07-27 08:41:22 +00:00
fix: 数据库支持
This commit is contained in:
@ -1,38 +1,35 @@
|
||||
import sys
|
||||
from typing import Optional
|
||||
|
||||
import nonebot
|
||||
from nonebot import on_message, require
|
||||
|
||||
from nonebot.plugin import PluginMetadata
|
||||
|
||||
from liteyuki.utils.data import LiteModel
|
||||
from liteyuki.utils.message import send_markdown
|
||||
from liteyuki.utils.data import Database, LiteModel
|
||||
from liteyuki.utils.ly_typing import T_Bot, T_MessageEvent
|
||||
from liteyuki.utils.data import Database
|
||||
from liteyuki.utils.message import send_markdown
|
||||
|
||||
require("nonebot_plugin_alconna")
|
||||
from nonebot_plugin_alconna import on_alconna
|
||||
from arclet.alconna import Arparma, Alconna, Args, Option, Subcommand, Arg
|
||||
from arclet.alconna import Arparma, Alconna, Args, Option, Subcommand
|
||||
|
||||
|
||||
class Node(LiteModel):
|
||||
bot_id: str
|
||||
session_type: str
|
||||
session_id: str
|
||||
TABLE_NAME = "node"
|
||||
bot_id: str = ""
|
||||
session_type: str = ""
|
||||
session_id: str = ""
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.bot_id}.{self.session_type}.{self.session_id}"
|
||||
|
||||
|
||||
class Push(LiteModel):
|
||||
source: Node
|
||||
target: Node
|
||||
inde: int
|
||||
TABLE_NAME = "push"
|
||||
source: Node = Node()
|
||||
target: Node = Node()
|
||||
inde: int = 0
|
||||
|
||||
|
||||
pushes_db = Database("data/pushes.ldb")
|
||||
pushes_db.auto_migrate(Push, Node)
|
||||
pushes_db.auto_migrate(Push(), Node())
|
||||
|
||||
alc = Alconna(
|
||||
"lep",
|
||||
@ -67,7 +64,7 @@ async def _(result: Arparma):
|
||||
push1 = Push(
|
||||
source=Node(bot_id=source[0], session_type=source[1], session_id=source[2]),
|
||||
target=Node(bot_id=target[0], session_type=target[1], session_id=target[2]),
|
||||
inde=len(pushes_db.all(Push, default=[]))
|
||||
inde=len(pushes_db.all(Push(), default=[]))
|
||||
)
|
||||
pushes_db.upsert(push1)
|
||||
|
||||
@ -75,7 +72,7 @@ async def _(result: Arparma):
|
||||
push2 = Push(
|
||||
source=Node(bot_id=target[0], session_type=target[1], session_id=target[2]),
|
||||
target=Node(bot_id=source[0], session_type=source[1], session_id=source[2]),
|
||||
inde=len(pushes_db.all(Push, default=[]))
|
||||
inde=len(pushes_db.all(Push(), default=[]))
|
||||
)
|
||||
pushes_db.upsert(push2)
|
||||
await add_push.finish("添加成功")
|
||||
@ -85,7 +82,7 @@ async def _(result: Arparma):
|
||||
index = result.subcommands["rm"].args.get("index")
|
||||
if index is not None:
|
||||
try:
|
||||
pushes_db.delete(Push, "inde = ?", index)
|
||||
pushes_db.delete(Push(), "inde = ?", index)
|
||||
await add_push.finish("删除成功")
|
||||
except IndexError:
|
||||
await add_push.finish("索引错误")
|
||||
@ -95,19 +92,19 @@ async def _(result: Arparma):
|
||||
await add_push.finish(
|
||||
"\n".join([f"{push.inde} {push.source.bot_id}.{push.source.session_type}.{push.source.session_id} -> "
|
||||
f"{push.target.bot_id}.{push.target.session_type}.{push.target.session_id}" for i, push in
|
||||
enumerate(pushes_db.all(Push, default=[]))]))
|
||||
enumerate(pushes_db.all(Push(), default=[]))]))
|
||||
else:
|
||||
await add_push.finish("参数错误")
|
||||
|
||||
|
||||
@on_message(block=False).handle()
|
||||
async def _(event: T_MessageEvent, bot: T_Bot):
|
||||
for push in pushes_db.all(Push, default=[]):
|
||||
for push in pushes_db.all(Push(), default=[]):
|
||||
if str(push.source) == f"{bot.self_id}.{event.message_type}.{event.user_id if event.message_type == 'private' else event.group_id}":
|
||||
bot2 = nonebot.get_bot(push.target.bot_id)
|
||||
msg_formatted = ""
|
||||
for l in str(event.message).split("\n"):
|
||||
msg_formatted += f"**{l.strip()}**\n"
|
||||
for line in str(event.message).split("\n"):
|
||||
msg_formatted += f"**{line.strip()}**\n"
|
||||
push_message = (
|
||||
f"> From {event.sender.nickname}@{push.source.session_type}.{push.source.session_id}\n> Bot {bot.self_id}\n\n"
|
||||
f"{msg_formatted}")
|
||||
|
@ -5,7 +5,7 @@ import aiofiles
|
||||
import nonebot.plugin
|
||||
|
||||
from liteyuki.utils.data import Database, LiteModel
|
||||
from liteyuki.utils.data_manager import GroupChat, InstalledPlugin, User, group_db, plugin_db, user_db
|
||||
from liteyuki.utils.data_manager import Group, InstalledPlugin, User, group_db, plugin_db, user_db
|
||||
from liteyuki.utils.ly_typing import T_MessageEvent
|
||||
|
||||
LNPM_COMMAND_START = "lnpm"
|
||||
@ -75,9 +75,9 @@ def get_plugin_session_enable(event: T_MessageEvent, plugin_module_name: str) ->
|
||||
bool: 插件当前状态
|
||||
"""
|
||||
if event.message_type == "group":
|
||||
session: GroupChat = group_db.first(GroupChat, "group_id = ?", event.group_id, default=GroupChat(group_id=str(event.group_id)))
|
||||
session: Group = group_db.first(Group(), "group_id = ?", event.group_id, default=Group(group_id=str(event.group_id)))
|
||||
else:
|
||||
session: User = user_db.first(User, "user_id = ?", event.user_id, default=User(user_id=str(event.user_id)))
|
||||
session: User = user_db.first(User(), "user_id = ?", event.user_id, default=User(user_id=str(event.user_id)))
|
||||
# 默认停用插件在启用列表内表示启用
|
||||
# 默认停用插件不在启用列表内表示停用
|
||||
# 默认启用插件在停用列表内表示停用
|
||||
@ -90,7 +90,11 @@ def get_plugin_session_enable(event: T_MessageEvent, plugin_module_name: str) ->
|
||||
|
||||
|
||||
def get_plugin_global_enable(plugin_module_name: str) -> bool:
|
||||
return True
|
||||
return plugin_db.first(
|
||||
InstalledPlugin(),
|
||||
"module_name = ?",
|
||||
plugin_module_name,
|
||||
default=InstalledPlugin(module_name=plugin_module_name, enabled=True)).enabled
|
||||
|
||||
|
||||
def get_plugin_can_be_toggle(plugin_module_name: str) -> bool:
|
||||
|
@ -98,7 +98,7 @@ async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot):
|
||||
|
||||
r_load = nonebot.load_plugin(plugin_module_name) # 加载插件
|
||||
installed_plugin = InstalledPlugin(module_name=plugin_module_name) # 构造插件信息模型
|
||||
found_in_db_plugin = plugin_db.first(InstalledPlugin, "module_name = ?", plugin_module_name) # 查询数据库中是否已经安装
|
||||
found_in_db_plugin = plugin_db.first(InstalledPlugin(), "module_name = ?", plugin_module_name) # 查询数据库中是否已经安装
|
||||
|
||||
if r_load:
|
||||
if found_in_db_plugin is None:
|
||||
@ -131,7 +131,7 @@ async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot):
|
||||
|
||||
elif result.subcommands.get("uninstall"):
|
||||
plugin_module_name: str = result.subcommands["uninstall"].args.get("plugin_name")
|
||||
found_installed_plugin: InstalledPlugin = plugin_db.first(InstalledPlugin, "module_name = ?", plugin_module_name)
|
||||
found_installed_plugin: InstalledPlugin = plugin_db.first(InstalledPlugin(), "module_name = ?", plugin_module_name)
|
||||
if found_installed_plugin:
|
||||
plugin_db.delete(InstalledPlugin, "module_name = ?", plugin_module_name)
|
||||
reply = f"{ulang.get('npm.uninstall_success', NAME=found_installed_plugin.module_name)}"
|
||||
|
@ -7,7 +7,7 @@ from nonebot.internal.matcher import Matcher
|
||||
from nonebot.message import run_preprocessor
|
||||
from nonebot.permission import SUPERUSER
|
||||
|
||||
from liteyuki.utils.data_manager import GroupChat, InstalledPlugin, User, group_db, plugin_db, user_db
|
||||
from liteyuki.utils.data_manager import Group, InstalledPlugin, User, group_db, plugin_db, user_db
|
||||
from liteyuki.utils.message import Markdown as md, send_markdown
|
||||
from liteyuki.utils.permission import GROUP_ADMIN, GROUP_OWNER
|
||||
from liteyuki.utils.ly_typing import T_Bot, T_MessageEvent
|
||||
@ -26,11 +26,19 @@ list_plugins = on_alconna(
|
||||
|
||||
toggle_plugin = on_alconna(
|
||||
Alconna(
|
||||
["enable-plugin", "disable-plugin"],
|
||||
["enable", "disable"],
|
||||
Args["plugin_name", str],
|
||||
)
|
||||
)
|
||||
|
||||
toggle_plugin_global = on_alconna(
|
||||
Alconna(
|
||||
["enable-global", "disable-global"],
|
||||
Args["plugin_name", str],
|
||||
),
|
||||
permission=SUPERUSER
|
||||
)
|
||||
|
||||
global_toggle = on_alconna(
|
||||
Alconna(
|
||||
["toggle-global"],
|
||||
@ -82,7 +90,7 @@ async def _(event: T_MessageEvent, bot: T_Bot):
|
||||
|
||||
if await GROUP_ADMIN(bot, event) or await GROUP_OWNER(bot, event) or await SUPERUSER(bot, event):
|
||||
# 添加启用/停用插件按钮
|
||||
cmd_toggle = f"{'disable' if session_enable else 'enable'}-plugin {plugin.module_name}"
|
||||
cmd_toggle = f"{'disable' if session_enable else 'enable'} {plugin.module_name}"
|
||||
text_toggle = lang.get("npm.disable" if session_enable else "npm.enable")
|
||||
can_be_toggle = get_plugin_can_be_toggle(plugin.module_name)
|
||||
btn_toggle = text_toggle if not can_be_toggle else md.button(text_toggle, cmd_toggle)
|
||||
@ -90,7 +98,7 @@ async def _(event: T_MessageEvent, bot: T_Bot):
|
||||
reply += f" {btn_toggle}"
|
||||
|
||||
if await SUPERUSER(bot, event):
|
||||
plugin_in_database = plugin_db.first(InstalledPlugin, "module_name = ?", plugin.module_name)
|
||||
plugin_in_database = plugin_db.first(InstalledPlugin(), "module_name = ?", plugin.module_name)
|
||||
# 添加移除插件和全局切换按钮
|
||||
global_enable = get_plugin_global_enable(plugin.module_name)
|
||||
btn_uninstall = (
|
||||
@ -98,7 +106,7 @@ async def _(event: T_MessageEvent, bot: T_Bot):
|
||||
'npm.uninstall')
|
||||
|
||||
btn_toggle_global_text = lang.get("npm.disable_global" if global_enable else "npm.enable_global")
|
||||
cmd_toggle_global = f"npm toggle-global {plugin.module_name}"
|
||||
cmd_toggle_global = f"{'disable-global' if global_enable else 'enable-global'} {plugin.module_name}"
|
||||
btn_toggle_global = btn_toggle_global_text if not can_be_toggle else md.button(btn_toggle_global_text, cmd_toggle_global)
|
||||
|
||||
reply += f" {btn_uninstall} {btn_toggle_global}"
|
||||
@ -131,10 +139,10 @@ async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot):
|
||||
ulang.get("npm.plugin_already", NAME=plugin_module_name, STATUS=ulang.get("npm.enable") if toggle else ulang.get("npm.disable")))
|
||||
|
||||
if event.message_type == "private":
|
||||
session = user_db.first(User, "user_id = ?", event.user_id, default=User(user_id=event.user_id))
|
||||
session = user_db.first(User(), "user_id = ?", event.user_id, default=User(user_id=event.user_id))
|
||||
else:
|
||||
if await GROUP_ADMIN(bot, event) or await GROUP_OWNER(bot, event) or await SUPERUSER(bot, event):
|
||||
session = group_db.first(GroupChat, "group_id = ?", event.group_id, default=GroupChat(group_id=str(event.group_id)))
|
||||
session = group_db.first(Group(), "group_id = ?", event.group_id, default=Group(group_id=str(event.group_id)))
|
||||
else:
|
||||
raise FinishedException(ulang.get("Permission Denied"))
|
||||
try:
|
||||
@ -170,6 +178,48 @@ async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot):
|
||||
)
|
||||
|
||||
|
||||
@toggle_plugin_global.handle()
|
||||
async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot):
|
||||
if not os.path.exists("data/liteyuki/plugins.json"):
|
||||
await npm_update()
|
||||
# 判断会话类型
|
||||
ulang = get_user_lang(str(event.user_id))
|
||||
plugin_module_name = result.args.get("plugin_name")
|
||||
|
||||
toggle = result.header_result == "enable-global"
|
||||
can_be_toggled = get_plugin_can_be_toggle(plugin_module_name)
|
||||
if not can_be_toggled:
|
||||
await toggle_plugin_global.finish(ulang.get("npm.plugin_cannot_be_toggled", NAME=plugin_module_name))
|
||||
|
||||
global_enable = get_plugin_global_enable(plugin_module_name)
|
||||
if global_enable == toggle:
|
||||
await toggle_plugin_global.finish(
|
||||
ulang.get("npm.plugin_already", NAME=plugin_module_name, STATUS=ulang.get("npm.enable") if toggle else ulang.get("npm.disable")))
|
||||
|
||||
try:
|
||||
plugin = plugin_db.first(InstalledPlugin(), "module_name = ?", plugin_module_name, default=InstalledPlugin(module_name=plugin_module_name))
|
||||
if toggle:
|
||||
plugin.enabled = True
|
||||
else:
|
||||
plugin.enabled = False
|
||||
plugin_db.upsert(plugin)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
await toggle_plugin_global.finish(
|
||||
ulang.get(
|
||||
"npm.toggle_failed",
|
||||
NAME=plugin_module_name,
|
||||
STATUS=ulang.get("npm.enable") if toggle else ulang.get("npm.disable"),
|
||||
ERROR=str(e))
|
||||
)
|
||||
|
||||
await toggle_plugin_global.finish(
|
||||
ulang.get(
|
||||
"npm.toggle_success",
|
||||
NAME=plugin_module_name,
|
||||
STATUS=ulang.get("npm.enable") if toggle else ulang.get("npm.disable"))
|
||||
)
|
||||
|
||||
@run_preprocessor
|
||||
async def _(event: T_MessageEvent, matcher: Matcher):
|
||||
plugin = matcher.plugin
|
||||
|
@ -40,7 +40,7 @@ class Profile(LiteModel):
|
||||
|
||||
@profile_alc.handle()
|
||||
async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot):
|
||||
user: User = user_db.first(User, "user_id = ?", event.user_id, default=User(user_id=str(event.user_id)))
|
||||
user: User = user_db.first(User(), "user_id = ?", event.user_id, default=User(user_id=str(event.user_id)))
|
||||
ulang = get_user_lang(str(event.user_id))
|
||||
if result.subcommands.get("set"):
|
||||
if result.subcommands["set"].args.get("value"):
|
||||
|
Reference in New Issue
Block a user