mirror of
https://github.com/TriM-Organization/LiteyukiBot-TriM.git
synced 2024-12-01 09:44:44 +08:00
docs: 添加了部分内容
This commit is contained in:
parent
bb27eea0c2
commit
788bca7113
@ -32,16 +32,15 @@
|
|||||||
|
|
||||||
### 轻雪配置项(Nonebot插件配置项也可以写在此)
|
### 轻雪配置项(Nonebot插件配置项也可以写在此)
|
||||||
```yaml
|
```yaml
|
||||||
# 建议修改的配置项目
|
# 配置项,如果不确定字段的含义,请不要修改
|
||||||
command_start: [ "/", " " ] # 指令前缀
|
command_start: [ "/", " " ] # 指令前缀
|
||||||
host: 127.0.0.1 # 反向监听地址
|
host: 127.0.0.1 # 反向监听地址
|
||||||
port: 20216 # 绑定端口
|
port: 20216 # 绑定端口
|
||||||
nickname: [ "liteyuki" ] # 机器人昵称
|
nickname: [ "liteyuki" ] # 机器人昵称
|
||||||
superusers: [ "1919810" ] # 超级用户
|
superusers: [ "1919810" ] # 超级用户
|
||||||
show_icon: true # 是否显示日志等级图标(某些控制台不可用)
|
default_language: "zh-CN" # 默认语言
|
||||||
|
log_icon: true # 是否显示日志等级图标(某些控制台不可用)
|
||||||
auto_report: true # 是否自动上报设备信息给轻雪服务器,该信息仅包含硬件信息和运行软件版本
|
auto_report: true # 是否自动上报设备信息给轻雪服务器,该信息仅包含硬件信息和运行软件版本
|
||||||
|
|
||||||
# 下面是不建议修改,且默认没有列出的配置项,除非你有特殊需求
|
|
||||||
log_level: "INFO" # 日志等级
|
log_level: "INFO" # 日志等级
|
||||||
|
|
||||||
# 其他Nonebot插件的配置项
|
# 其他Nonebot插件的配置项
|
||||||
|
@ -2,6 +2,7 @@ import os
|
|||||||
|
|
||||||
import nonebot.plugin
|
import nonebot.plugin
|
||||||
|
|
||||||
|
from liteyuki.utils import init_log
|
||||||
from liteyuki.utils.data_manager import InstalledPlugin, plugin_db
|
from liteyuki.utils.data_manager import InstalledPlugin, plugin_db
|
||||||
from liteyuki.utils.resource import load_resource_from_dir
|
from liteyuki.utils.resource import load_resource_from_dir
|
||||||
from liteyuki.utils.tools import check_for_package
|
from liteyuki.utils.tools import check_for_package
|
||||||
@ -13,6 +14,8 @@ load_resource_from_dir(RESOURCE_PATH)
|
|||||||
nonebot.plugin.load_plugins("liteyuki/plugins")
|
nonebot.plugin.load_plugins("liteyuki/plugins")
|
||||||
nonebot.plugin.load_plugins("plugins")
|
nonebot.plugin.load_plugins("plugins")
|
||||||
|
|
||||||
|
init_log()
|
||||||
|
|
||||||
installed_plugins = plugin_db.all(InstalledPlugin)
|
installed_plugins = plugin_db.all(InstalledPlugin)
|
||||||
if installed_plugins:
|
if installed_plugins:
|
||||||
for installed_plugin in plugin_db.all(InstalledPlugin):
|
for installed_plugin in plugin_db.all(InstalledPlugin):
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
language.name=简体中文(轻雪版)
|
language.name=简体中文(轻雪版)
|
||||||
|
|
||||||
log.debug=调试
|
log.debug=调试中
|
||||||
log.info=信息
|
log.info=是信息
|
||||||
log.warning=有问题哦
|
log.warning=有问题
|
||||||
log.error=出错啦
|
log.error=出错辣
|
||||||
log.success=成功啦
|
log.success=成功啦
|
||||||
|
|
||||||
main.current_language=现在系统用的语言是:{LANG} 喔!
|
main.current_language=现在系统用的语言是:{LANG} 喔!
|
||||||
|
@ -10,7 +10,8 @@ __VERSION__ = "6.2.4" # 60201
|
|||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
from liteyuki.utils.config import load_from_yaml
|
from liteyuki.utils.config import load_from_yaml, config
|
||||||
|
from .log import init_log
|
||||||
|
|
||||||
major, minor, patch = map(int, __VERSION__.split("."))
|
major, minor, patch = map(int, __VERSION__.split("."))
|
||||||
__VERSION_I__ = major * 10000 + minor * 100 + patch
|
__VERSION_I__ = major * 10000 + minor * 100 + patch
|
||||||
@ -51,9 +52,9 @@ def init():
|
|||||||
if sys.version_info < (3, 10):
|
if sys.version_info < (3, 10):
|
||||||
nonebot.logger.error("This project requires Python3.10+ to run, please upgrade your Python Environment.")
|
nonebot.logger.error("This project requires Python3.10+ to run, please upgrade your Python Environment.")
|
||||||
exit(1)
|
exit(1)
|
||||||
|
# 在加载完成语言后再初始化日志
|
||||||
load_from_yaml("config.yml")
|
init_log()
|
||||||
from .log import logger
|
nonebot.logger.info("Liteyuki is initializing...")
|
||||||
|
|
||||||
if not os.path.exists("data/liteyuki/liteyuki.json"):
|
if not os.path.exists("data/liteyuki/liteyuki.json"):
|
||||||
register_bot()
|
register_bot()
|
||||||
@ -73,5 +74,4 @@ $$$$$$$$/ $$$$$$/ $$/ $$$$$$$$/ $$/ $$$$$$/ $$/ $$/ $$$$$$/
|
|||||||
f"Run Liteyuki with Python{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro} "
|
f"Run Liteyuki with Python{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro} "
|
||||||
f"at {sys.executable}"
|
f"at {sys.executable}"
|
||||||
)
|
)
|
||||||
|
|
||||||
nonebot.logger.info(f"{__NAME__} {__VERSION__}({__VERSION_I__}) is running")
|
nonebot.logger.info(f"{__NAME__} {__VERSION__}({__VERSION_I__}) is running")
|
||||||
|
@ -4,7 +4,7 @@ import nonebot
|
|||||||
import yaml
|
import yaml
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
config = {}
|
config = {} # 全局配置
|
||||||
|
|
||||||
|
|
||||||
class BasicConfig(BaseModel):
|
class BasicConfig(BaseModel):
|
||||||
@ -16,8 +16,8 @@ class BasicConfig(BaseModel):
|
|||||||
|
|
||||||
|
|
||||||
def load_from_yaml(file: str) -> dict:
|
def load_from_yaml(file: str) -> dict:
|
||||||
nonebot.logger.debug("Loading config from %s" % file)
|
|
||||||
global config
|
global config
|
||||||
|
nonebot.logger.debug("Loading config from %s" % file)
|
||||||
if not os.path.exists(file):
|
if not os.path.exists(file):
|
||||||
nonebot.logger.warning(f'Config file {file} not found, created default config, please modify it and restart')
|
nonebot.logger.warning(f'Config file {file} not found, created default config, please modify it and restart')
|
||||||
with open(file, 'w', encoding='utf-8') as f:
|
with open(file, 'w', encoding='utf-8') as f:
|
||||||
|
@ -9,8 +9,8 @@ from typing import Any
|
|||||||
|
|
||||||
import nonebot
|
import nonebot
|
||||||
|
|
||||||
from liteyuki.utils.config import config
|
from .config import config
|
||||||
from liteyuki.utils.data_manager import User, user_db
|
from .data_manager import User, user_db
|
||||||
|
|
||||||
_default_lang_code = "en"
|
_default_lang_code = "en"
|
||||||
_language_data = {
|
_language_data = {
|
||||||
@ -62,7 +62,6 @@ def load_from_json(file_path: str, lang_code: str = None):
|
|||||||
if lang_code not in _language_data:
|
if lang_code not in _language_data:
|
||||||
_language_data[lang_code] = {}
|
_language_data[lang_code] = {}
|
||||||
_language_data[lang_code].update(data)
|
_language_data[lang_code].update(data)
|
||||||
nonebot.logger.debug(f"Loaded language data from {file_path}")
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
nonebot.logger.error(f"Failed to load language data from {file_path}: {e}")
|
nonebot.logger.error(f"Failed to load language data from {file_path}: {e}")
|
||||||
|
|
||||||
@ -113,20 +112,23 @@ class Language:
|
|||||||
Args:
|
Args:
|
||||||
item: 文本键
|
item: 文本键
|
||||||
*args: 格式化参数
|
*args: 格式化参数
|
||||||
|
**kwargs: 格式化参数
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
str: 当前语言的文本
|
str: 当前语言的文本
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
default = kwargs.pop("default", None)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if self.lang_code in _language_data and item in _language_data[self.lang_code]:
|
if self.lang_code in _language_data and item in _language_data[self.lang_code]:
|
||||||
return _language_data[self.lang_code][item].format(*args, **kwargs)
|
return _language_data[self.lang_code][item].format(*args, **kwargs)
|
||||||
if self.fallback_lang_code in _language_data and item in _language_data[self.fallback_lang_code]:
|
if self.fallback_lang_code in _language_data and item in _language_data[self.fallback_lang_code]:
|
||||||
return _language_data[self.fallback_lang_code][item].format(*args, **kwargs)
|
return _language_data[self.fallback_lang_code][item].format(*args, **kwargs)
|
||||||
return item
|
return default or item
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
nonebot.logger.error(f"Failed to get language text or format: {e}")
|
nonebot.logger.error(f"Failed to get language text or format: {e}")
|
||||||
return item
|
return default or item
|
||||||
|
|
||||||
|
|
||||||
def get_user_lang(user_id: str) -> Language:
|
def get_user_lang(user_id: str) -> Language:
|
||||||
@ -150,7 +152,7 @@ def get_system_lang_code() -> str:
|
|||||||
|
|
||||||
def get_default_lang() -> Language:
|
def get_default_lang() -> Language:
|
||||||
"""
|
"""
|
||||||
获取默认/系统语言
|
获取配置默认/系统语言
|
||||||
"""
|
"""
|
||||||
return Language(config.get("default_language", get_system_lang_code()))
|
return Language(config.get("default_language", get_system_lang_code()))
|
||||||
|
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
import sys
|
import sys
|
||||||
import loguru
|
import loguru
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
from .config import config, load_from_yaml
|
from .config import load_from_yaml
|
||||||
|
from .language import Language, get_default_lang
|
||||||
|
|
||||||
logger = loguru.logger
|
logger = loguru.logger
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
@ -41,20 +42,33 @@ def get_format(level: str) -> str:
|
|||||||
return default_format
|
return default_format
|
||||||
|
|
||||||
|
|
||||||
logger = loguru.logger.bind(get_format=get_format)
|
logger = loguru.logger.bind()
|
||||||
|
|
||||||
logger.remove()
|
|
||||||
logger_id = logger.add(
|
|
||||||
sys.stdout,
|
|
||||||
level=0,
|
|
||||||
diagnose=False,
|
|
||||||
filter=default_filter,
|
|
||||||
format=get_format(config.get("log_level", "INFO")),
|
|
||||||
)
|
|
||||||
|
|
||||||
show_icon = config.get("show_icon", True)
|
def init_log():
|
||||||
logger.level("DEBUG", color="<blue>", icon=f"*️⃣==DEBUG")
|
global logger
|
||||||
logger.level("INFO", color="<white>", icon=f"ℹ️===INFO")
|
|
||||||
logger.level("SUCCESS", color="<green>", icon=f"✅SUCCESS")
|
config = load_from_yaml("config.yml")
|
||||||
logger.level("WARNING", color="<yellow>", icon=f"⚠️WARNING")
|
|
||||||
logger.level("ERROR", color="<red>", icon=f"⭕==ERROR")
|
logger.remove()
|
||||||
|
logger.add(
|
||||||
|
sys.stdout,
|
||||||
|
level=0,
|
||||||
|
diagnose=False,
|
||||||
|
filter=default_filter,
|
||||||
|
format=get_format(config.get("log_level", "INFO")),
|
||||||
|
)
|
||||||
|
show_icon = config.get("log_icon", True)
|
||||||
|
lang = Language(config.get("default_language", "en"))
|
||||||
|
|
||||||
|
debug = lang.get('log.debug', default="==DEBUG")
|
||||||
|
info = lang.get('log.info', default="===INFO")
|
||||||
|
success = lang.get('log.success', default="SUCCESS")
|
||||||
|
warning = lang.get('log.warning', default="WARNING")
|
||||||
|
error = lang.get('log.error', default="==ERROR")
|
||||||
|
|
||||||
|
logger.level("DEBUG", color="<blue>", icon=f"{'*️⃣' if show_icon else ''}{debug}")
|
||||||
|
logger.level("INFO", color="<white>", icon=f"{'ℹ️' if show_icon else ''}{info}")
|
||||||
|
logger.level("SUCCESS", color="<green>", icon=f"{'✅' if show_icon else ''}{success}")
|
||||||
|
logger.level("WARNING", color="<yellow>", icon=f"{'⚠️' if show_icon else ''}{warning}")
|
||||||
|
logger.level("ERROR", color="<red>", icon=f"{'⭕' if show_icon else ''}{error}")
|
||||||
|
2
main.py
2
main.py
@ -1,6 +1,6 @@
|
|||||||
import nonebot
|
import nonebot
|
||||||
from nonebot.adapters.onebot import v11, v12
|
from nonebot.adapters.onebot import v11, v12
|
||||||
from liteyuki.utils.config import load_from_yaml
|
from liteyuki.utils.config import config, load_from_yaml
|
||||||
from liteyuki.utils import init
|
from liteyuki.utils import init
|
||||||
|
|
||||||
init()
|
init()
|
||||||
|
Loading…
Reference in New Issue
Block a user