mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-06-04 04:54:36 +00:00
feat: 支持插件开关
This commit is contained in:
@@ -7,6 +7,7 @@ import sys
|
||||
import traceback
|
||||
|
||||
import pkg.utils.context as context
|
||||
import pkg.plugin.switch as switch
|
||||
|
||||
from mirai import Mirai
|
||||
|
||||
@@ -17,6 +18,8 @@ __plugins__ = {}
|
||||
示例:
|
||||
{
|
||||
"example": {
|
||||
"path": "plugins/example/main.py",
|
||||
"enabled: True,
|
||||
"name": "example",
|
||||
"description": "example",
|
||||
"version": "0.0.1",
|
||||
@@ -58,6 +61,8 @@ def initialize_plugins():
|
||||
""" 初始化插件 """
|
||||
logging.info("初始化插件")
|
||||
for plugin in __plugins__.values():
|
||||
if not plugin['enabled']:
|
||||
continue
|
||||
try:
|
||||
plugin['instance'] = plugin["class"]()
|
||||
except:
|
||||
@@ -195,6 +200,18 @@ class PluginHost:
|
||||
event_context = EventContext(event_name)
|
||||
logging.debug("触发事件: {} ({})".format(event_name, event_context.eid))
|
||||
for plugin in __plugins__.values():
|
||||
|
||||
if not plugin['enabled']:
|
||||
continue
|
||||
|
||||
if plugin['instance'] is None:
|
||||
# 从关闭状态切到开启状态之后,重新加载插件
|
||||
try:
|
||||
plugin['instance'] = plugin["class"]()
|
||||
except:
|
||||
logging.error("插件{}初始化时发生错误: {}".format(plugin['name'], sys.exc_info()))
|
||||
continue
|
||||
|
||||
for hook in plugin['hooks'].get(event_name, []):
|
||||
try:
|
||||
already_prevented_default = event_context.is_prevented_default()
|
||||
|
||||
@@ -3,8 +3,6 @@ import logging
|
||||
import pkg.plugin.host as host
|
||||
import pkg.utils.context
|
||||
|
||||
__current_registering_plugin__ = ""
|
||||
|
||||
PersonMessageReceived = "person_message_received"
|
||||
"""收到私聊消息时,在判断是否应该响应前触发
|
||||
kwargs:
|
||||
@@ -143,6 +141,9 @@ def on(event: str):
|
||||
return Plugin.on(event)
|
||||
|
||||
|
||||
__current_registering_plugin__ = ""
|
||||
|
||||
|
||||
class Plugin:
|
||||
|
||||
host: host.PluginHost
|
||||
@@ -193,7 +194,9 @@ def register(name: str, description: str, version: str, author: str):
|
||||
"description": description,
|
||||
"version": version,
|
||||
"author": author,
|
||||
"hooks": {}
|
||||
"hooks": {},
|
||||
"path": host.__current_module_path__,
|
||||
"enabled": True,
|
||||
}
|
||||
|
||||
def wrapper(cls: Plugin):
|
||||
@@ -202,6 +205,8 @@ def register(name: str, description: str, version: str, author: str):
|
||||
cls.version = version
|
||||
cls.author = author
|
||||
cls.host = pkg.utils.context.get_plugin_host()
|
||||
cls.enabled = True
|
||||
cls.path = host.__current_module_path__
|
||||
|
||||
# 存到插件列表
|
||||
host.__plugins__[name]["class"] = cls
|
||||
|
||||
86
pkg/plugin/switch.py
Normal file
86
pkg/plugin/switch.py
Normal file
@@ -0,0 +1,86 @@
|
||||
# 控制插件的开关
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
|
||||
import pkg.plugin.host as host
|
||||
|
||||
|
||||
def wrapper_dict_from_plugin_list() -> dict:
|
||||
""" 将插件列表转换为开关json """
|
||||
switch = {}
|
||||
|
||||
for plugin_name in host.__plugins__:
|
||||
plugin = host.__plugins__[plugin_name]
|
||||
|
||||
switch[plugin_name] = {
|
||||
"path": plugin["path"],
|
||||
"enabled": plugin["enabled"],
|
||||
}
|
||||
|
||||
return switch
|
||||
|
||||
|
||||
def apply_switch(switch: dict):
|
||||
"""将开关数据应用到插件列表中"""
|
||||
for plugin_name in switch:
|
||||
host.__plugins__[plugin_name]["enabled"] = switch[plugin_name]["enabled"]
|
||||
|
||||
|
||||
def dump_switch():
|
||||
""" 保存开关数据 """
|
||||
logging.debug("保存开关数据")
|
||||
# 将开关数据写入plugins/switch.json
|
||||
|
||||
switch = wrapper_dict_from_plugin_list()
|
||||
|
||||
with open("plugins/switch.json", "w", encoding="utf-8") as f:
|
||||
json.dump(switch, f, indent=4, ensure_ascii=False)
|
||||
|
||||
|
||||
def load_switch():
|
||||
""" 加载开关数据 """
|
||||
logging.debug("加载开关数据")
|
||||
# 读取plugins/switch.json
|
||||
|
||||
switch = {}
|
||||
|
||||
# 检查文件是否存在
|
||||
if not os.path.exists("plugins/switch.json"):
|
||||
# 不存在则创建
|
||||
with open("plugins/switch.json", "w", encoding="utf-8") as f:
|
||||
json.dump(switch, f, indent=4, ensure_ascii=False)
|
||||
|
||||
with open("plugins/switch.json", "r", encoding="utf-8") as f:
|
||||
switch = json.load(f)
|
||||
|
||||
if switch is None:
|
||||
switch = {}
|
||||
|
||||
switch_modified = False
|
||||
|
||||
# 检查switch中多余的和path不相符的
|
||||
for plugin_name in switch:
|
||||
if plugin_name not in host.__plugins__:
|
||||
del switch[plugin_name]
|
||||
switch_modified = True
|
||||
elif switch[plugin_name]["path"] != host.__plugins__[plugin_name]["path"]:
|
||||
# 删除此不相符的
|
||||
del switch[plugin_name]
|
||||
switch_modified = True
|
||||
|
||||
# 检查plugin中多余的
|
||||
for plugin_name in host.__plugins__:
|
||||
if plugin_name not in switch:
|
||||
switch[plugin_name] = {
|
||||
"path": host.__plugins__[plugin_name]["path"],
|
||||
"enabled": host.__plugins__[plugin_name]["enabled"],
|
||||
}
|
||||
switch_modified = True
|
||||
|
||||
# 如果switch有修改,保存
|
||||
if switch_modified:
|
||||
dump_switch()
|
||||
|
||||
# 应用开关数据
|
||||
apply_switch(switch)
|
||||
Reference in New Issue
Block a user