mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2025-12-15 06:12:53 +08:00
✨ 提供database.json配置文件
This commit is contained in:
parent
d05b1fb9b2
commit
ab05d8a1b5
@ -5,6 +5,7 @@ from nonebot_plugin_alconna import UniMsg
|
||||
from nonebot_plugin_saa import Mention, MessageFactory, Text
|
||||
from nonebot_plugin_session import EventSession
|
||||
from pydantic import BaseModel
|
||||
from tortoise.exceptions import IntegrityError
|
||||
|
||||
from zhenxun.configs.config import Config
|
||||
from zhenxun.models.group_console import GroupConsole
|
||||
@ -203,7 +204,13 @@ class AuthChecker:
|
||||
group_id = channel_id
|
||||
channel_id = None
|
||||
if user_id and matcher.plugin and (module_path := matcher.plugin.module_name):
|
||||
user = await UserConsole.get_user(user_id, session.platform)
|
||||
try:
|
||||
user = await UserConsole.get_user(user_id, session.platform)
|
||||
except IntegrityError as e:
|
||||
logger.debug(
|
||||
"重复创建用户,已跳过全选该次权限...", "HOOK", session=session, e=e
|
||||
)
|
||||
return
|
||||
if plugin := await PluginInfo.get_or_none(module_path=module_path):
|
||||
if plugin.plugin_type == PluginType.HIDDEN and plugin.name != "帮助":
|
||||
logger.debug("插件为HIDDEN且不是帮助功能,已跳过...")
|
||||
|
||||
@ -25,15 +25,16 @@ __plugin_meta__ = PluginMetadata(
|
||||
async def _(
|
||||
matcher: Matcher, exception: Exception | None, bot: Bot, session: EventSession
|
||||
):
|
||||
plugin = await PluginInfo.get_or_none(module=matcher.plugin_name)
|
||||
plugin_type = plugin.plugin_type if plugin else None
|
||||
if plugin_type == PluginType.NORMAL and matcher.plugin_name not in [
|
||||
"update_info",
|
||||
"statistics_handle",
|
||||
]:
|
||||
await Statistics.create(
|
||||
user_id=session.id1,
|
||||
group_id=session.id3 or session.id2,
|
||||
plugin_name=matcher.plugin_name,
|
||||
create_time=datetime.now(),
|
||||
)
|
||||
if session.id1:
|
||||
plugin = await PluginInfo.get_or_none(module=matcher.plugin_name)
|
||||
plugin_type = plugin.plugin_type if plugin else None
|
||||
if plugin_type == PluginType.NORMAL and matcher.plugin_name not in [
|
||||
"update_info",
|
||||
"statistics_handle",
|
||||
]:
|
||||
await Statistics.create(
|
||||
user_id=session.id1,
|
||||
group_id=session.id3 or session.id2,
|
||||
plugin_name=matcher.plugin_name,
|
||||
create_time=datetime.now(),
|
||||
)
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
import ujson as json
|
||||
from nonebot.utils import is_coroutine_callable
|
||||
from tortoise import Tortoise, fields
|
||||
from tortoise.connection import connections
|
||||
@ -12,6 +13,7 @@ from zhenxun.configs.config import (
|
||||
sql_name,
|
||||
user,
|
||||
)
|
||||
from zhenxun.configs.path_config import DATA_PATH
|
||||
|
||||
from .log import logger
|
||||
|
||||
@ -19,6 +21,8 @@ MODELS: list[str] = []
|
||||
|
||||
SCRIPT_METHOD = []
|
||||
|
||||
DATABASE_SETTING_FILE = DATA_PATH / "database.json"
|
||||
|
||||
|
||||
class Model(Model_):
|
||||
"""
|
||||
@ -46,11 +50,35 @@ class TestSQL(Model):
|
||||
|
||||
|
||||
async def init():
|
||||
if not bind and not any([user, password, address, port, database]):
|
||||
if DATABASE_SETTING_FILE.exists():
|
||||
with open(DATABASE_SETTING_FILE, "r", encoding="utf-8") as f:
|
||||
setting_data = json.load(f)
|
||||
else:
|
||||
i_bind = bind
|
||||
if not i_bind and any([user, password, address, port, database]):
|
||||
i_bind = f"{sql_name}://{user}:{password}@{address}:{port}/{database}"
|
||||
setting_data = {
|
||||
"bind": i_bind,
|
||||
"sql_name": sql_name,
|
||||
"user": user,
|
||||
"password": password,
|
||||
"address": address,
|
||||
"port": port,
|
||||
"database": database,
|
||||
}
|
||||
with open(DATABASE_SETTING_FILE, "w", encoding="utf-8") as f:
|
||||
json.dump(setting_data, f, ensure_ascii=False, indent=4)
|
||||
i_bind = setting_data.get("bind")
|
||||
_sql_name = setting_data.get("sql_name")
|
||||
_user = setting_data.get("user")
|
||||
_password = setting_data.get("password")
|
||||
_address = setting_data.get("address")
|
||||
_port = setting_data.get("port")
|
||||
_database = setting_data.get("database")
|
||||
if not i_bind and not any([_user, _password, _address, _port, _database]):
|
||||
raise ValueError("\n数据库配置未填写...")
|
||||
i_bind = bind
|
||||
if not i_bind:
|
||||
i_bind = f"{sql_name}://{user}:{password}@{address}:{port}/{database}"
|
||||
i_bind = f"{_sql_name}://{_user}:{_password}@{_address}:{_port}/{_database}"
|
||||
try:
|
||||
await Tortoise.init(
|
||||
db_url=i_bind, modules={"models": MODELS}, timezone="Asia/Shanghai"
|
||||
|
||||
@ -28,7 +28,11 @@ class MessageUtils:
|
||||
返回:
|
||||
list[Text | Text]: 构造完成的消息列表
|
||||
"""
|
||||
is_bytes = driver.config.image_to_bytes == "True"
|
||||
is_bytes = False
|
||||
try:
|
||||
is_bytes = driver.config.image_to_bytes in ["True", "true"]
|
||||
except AttributeError:
|
||||
pass
|
||||
message_list = []
|
||||
for msg in msg_list:
|
||||
if isinstance(msg, (Image, Text, At)):
|
||||
|
||||
Loading…
Reference in New Issue
Block a user