mirror of
https://github.com/RockChinQ/QChatGPT.git
synced 2024-11-16 19:57:04 +08:00
parent
c902822723
commit
3c08741cb6
|
@ -9,6 +9,7 @@ import traceback
|
||||||
|
|
||||||
import pkg.utils.context as context
|
import pkg.utils.context as context
|
||||||
import pkg.plugin.switch as switch
|
import pkg.plugin.switch as switch
|
||||||
|
import pkg.plugin.settings as settings
|
||||||
|
|
||||||
from mirai import Mirai
|
from mirai import Mirai
|
||||||
|
|
||||||
|
@ -35,6 +36,23 @@ __plugins__ = {}
|
||||||
}
|
}
|
||||||
}"""
|
}"""
|
||||||
|
|
||||||
|
__plugins_order__ = []
|
||||||
|
"""插件顺序"""
|
||||||
|
|
||||||
|
|
||||||
|
def generate_plugin_order():
|
||||||
|
""" 根据__plugin__生成插件初始顺序,无视是否启用 """
|
||||||
|
global __plugins_order__
|
||||||
|
__plugins_order__ = []
|
||||||
|
for plugin_name in __plugins__:
|
||||||
|
__plugins_order__.append(plugin_name)
|
||||||
|
|
||||||
|
|
||||||
|
def iter_plugins():
|
||||||
|
""" 按照顺序迭代插件 """
|
||||||
|
for plugin_name in __plugins_order__:
|
||||||
|
yield __plugins__[plugin_name]
|
||||||
|
|
||||||
|
|
||||||
__current_module_path__ = ""
|
__current_module_path__ = ""
|
||||||
|
|
||||||
|
@ -70,11 +88,16 @@ def load_plugins():
|
||||||
# 加载开关数据
|
# 加载开关数据
|
||||||
switch.load_switch()
|
switch.load_switch()
|
||||||
|
|
||||||
|
# 生成初始顺序
|
||||||
|
generate_plugin_order()
|
||||||
|
# 加载插件顺序
|
||||||
|
settings.load_settings()
|
||||||
|
|
||||||
|
|
||||||
def initialize_plugins():
|
def initialize_plugins():
|
||||||
""" 初始化插件 """
|
""" 初始化插件 """
|
||||||
logging.info("初始化插件")
|
logging.info("初始化插件")
|
||||||
for plugin in __plugins__.values():
|
for plugin in iter_plugins():
|
||||||
if not plugin['enabled']:
|
if not plugin['enabled']:
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
|
@ -243,7 +266,7 @@ class PluginHost:
|
||||||
""" 触发事件 """
|
""" 触发事件 """
|
||||||
event_context = EventContext(event_name)
|
event_context = EventContext(event_name)
|
||||||
logging.debug("触发事件: {} ({})".format(event_name, event_context.eid))
|
logging.debug("触发事件: {} ({})".format(event_name, event_context.eid))
|
||||||
for plugin in __plugins__.values():
|
for plugin in iter_plugins():
|
||||||
|
|
||||||
if not plugin['enabled']:
|
if not plugin['enabled']:
|
||||||
continue
|
continue
|
||||||
|
|
84
pkg/plugin/settings.py
Normal file
84
pkg/plugin/settings.py
Normal file
|
@ -0,0 +1,84 @@
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
|
||||||
|
import pkg.plugin.host as host
|
||||||
|
import logging
|
||||||
|
|
||||||
|
|
||||||
|
def wrapper_dict_from_runtime_context() -> dict:
|
||||||
|
"""从变量中包装settings.json的数据字典"""
|
||||||
|
settings = {
|
||||||
|
"order": []
|
||||||
|
}
|
||||||
|
|
||||||
|
for plugin_name in host.__plugins_order__:
|
||||||
|
settings["order"].append(plugin_name)
|
||||||
|
|
||||||
|
return settings
|
||||||
|
|
||||||
|
|
||||||
|
def apply_settings(settings: dict):
|
||||||
|
"""将settings.json数据应用到变量中"""
|
||||||
|
if "order" in settings:
|
||||||
|
host.__plugins_order__ = settings["order"]
|
||||||
|
|
||||||
|
|
||||||
|
def dump_settings():
|
||||||
|
"""保存settings.json数据"""
|
||||||
|
logging.debug("保存plugins/settings.json数据")
|
||||||
|
|
||||||
|
settings = wrapper_dict_from_runtime_context()
|
||||||
|
|
||||||
|
with open("plugins/settings.json", "w", encoding="utf-8") as f:
|
||||||
|
json.dump(settings, f, indent=4, ensure_ascii=False)
|
||||||
|
|
||||||
|
|
||||||
|
def load_settings():
|
||||||
|
"""加载settings.json数据"""
|
||||||
|
logging.debug("加载plugins/settings.json数据")
|
||||||
|
|
||||||
|
# 读取plugins/settings.json
|
||||||
|
settings = {
|
||||||
|
}
|
||||||
|
|
||||||
|
# 检查文件是否存在
|
||||||
|
if not os.path.exists("plugins/settings.json"):
|
||||||
|
# 不存在则创建
|
||||||
|
with open("plugins/settings.json", "w", encoding="utf-8") as f:
|
||||||
|
json.dump(wrapper_dict_from_runtime_context(), f, indent=4, ensure_ascii=False)
|
||||||
|
|
||||||
|
with open("plugins/settings.json", "r", encoding="utf-8") as f:
|
||||||
|
settings = json.load(f)
|
||||||
|
|
||||||
|
if settings is None:
|
||||||
|
settings = {
|
||||||
|
}
|
||||||
|
|
||||||
|
# 检查每个设置项
|
||||||
|
if "order" not in settings:
|
||||||
|
settings["order"] = []
|
||||||
|
|
||||||
|
settings_modified = False
|
||||||
|
|
||||||
|
settings_copy = settings.copy()
|
||||||
|
|
||||||
|
# 检查settings中多余的插件项
|
||||||
|
|
||||||
|
# order
|
||||||
|
for plugin_name in settings_copy["order"]:
|
||||||
|
if plugin_name not in host.__plugins_order__:
|
||||||
|
settings["order"].remove(plugin_name)
|
||||||
|
settings_modified = True
|
||||||
|
|
||||||
|
# 检查settings中缺少的插件项
|
||||||
|
|
||||||
|
# order
|
||||||
|
for plugin_name in host.__plugins_order__:
|
||||||
|
if plugin_name not in settings_copy["order"]:
|
||||||
|
settings["order"].append(plugin_name)
|
||||||
|
settings_modified = True
|
||||||
|
|
||||||
|
if settings_modified:
|
||||||
|
dump_settings()
|
||||||
|
|
||||||
|
apply_settings(settings)
|
Loading…
Reference in New Issue
Block a user