mirror of
https://github.com/zhayujie/chatgpt-on-wechat.git
synced 2026-07-20 21:57:14 +08:00
feat: move loading config method to base class
This commit is contained in:
@@ -24,18 +24,17 @@ class Banwords(Plugin):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
try:
|
try:
|
||||||
curdir = os.path.dirname(__file__)
|
# load config
|
||||||
config_path = os.path.join(curdir, "config.json")
|
|
||||||
# loading config from global plugin config
|
|
||||||
conf = super().load_config()
|
conf = super().load_config()
|
||||||
|
curdir = os.path.dirname(__file__)
|
||||||
if not conf:
|
if not conf:
|
||||||
|
# 配置不存在则写入默认配置
|
||||||
|
config_path = os.path.join(curdir, "config.json")
|
||||||
if not os.path.exists(config_path):
|
if not os.path.exists(config_path):
|
||||||
conf = {"action": "ignore"}
|
conf = {"action": "ignore"}
|
||||||
with open(config_path, "w") as f:
|
with open(config_path, "w") as f:
|
||||||
json.dump(conf, f, indent=4)
|
json.dump(conf, f, indent=4)
|
||||||
else:
|
|
||||||
with open(config_path, "r") as f:
|
|
||||||
conf = super().load_config() or json.load(f)
|
|
||||||
self.searchr = WordsSearch()
|
self.searchr = WordsSearch()
|
||||||
self.action = conf["action"]
|
self.action = conf["action"]
|
||||||
banwords_path = os.path.join(curdir, "banwords.txt")
|
banwords_path = os.path.join(curdir, "banwords.txt")
|
||||||
|
|||||||
@@ -29,15 +29,9 @@ class BDunit(Plugin):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
try:
|
try:
|
||||||
curdir = os.path.dirname(__file__)
|
|
||||||
config_path = os.path.join(curdir, "config.json")
|
|
||||||
conf = super().load_config()
|
conf = super().load_config()
|
||||||
if not conf:
|
if not conf:
|
||||||
if not os.path.exists(config_path):
|
raise Exception("config.json not found")
|
||||||
raise Exception("config.json not found")
|
|
||||||
else:
|
|
||||||
with open(config_path, "r") as f:
|
|
||||||
conf = json.load(f)
|
|
||||||
self.service_id = conf["service_id"]
|
self.service_id = conf["service_id"]
|
||||||
self.api_key = conf["api_key"]
|
self.api_key = conf["api_key"]
|
||||||
self.secret_key = conf["secret_key"]
|
self.secret_key = conf["secret_key"]
|
||||||
|
|||||||
@@ -178,18 +178,13 @@ class Godcmd(Plugin):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
curdir = os.path.dirname(__file__)
|
config_path = os.path.join(os.path.dirname(__file__), "config.json")
|
||||||
config_path = os.path.join(curdir, "config.json")
|
|
||||||
# loading config from global plugin config
|
|
||||||
gconf = super().load_config()
|
gconf = super().load_config()
|
||||||
if not gconf:
|
if not gconf:
|
||||||
if not os.path.exists(config_path):
|
if not os.path.exists(config_path):
|
||||||
gconf = {"password": "", "admin_users": []}
|
gconf = {"password": "", "admin_users": []}
|
||||||
with open(config_path, "w") as f:
|
with open(config_path, "w") as f:
|
||||||
json.dump(gconf, f, indent=4)
|
json.dump(gconf, f, indent=4)
|
||||||
else:
|
|
||||||
with open(config_path, "r") as f:
|
|
||||||
gconf = json.load(f)
|
|
||||||
if gconf["password"] == "":
|
if gconf["password"] == "":
|
||||||
self.temp_password = "".join(random.sample(string.digits, 4))
|
self.temp_password = "".join(random.sample(string.digits, 4))
|
||||||
logger.info("[Godcmd] 因未设置口令,本次的临时口令为%s。" % self.temp_password)
|
logger.info("[Godcmd] 因未设置口令,本次的临时口令为%s。" % self.temp_password)
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
import os
|
import os
|
||||||
|
import json
|
||||||
from config import pconf
|
from config import pconf
|
||||||
from common.log import logger
|
from common.log import logger
|
||||||
|
|
||||||
|
|
||||||
class Plugin:
|
class Plugin:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.handlers = {}
|
self.handlers = {}
|
||||||
@@ -11,9 +13,16 @@ class Plugin:
|
|||||||
加载当前插件配置
|
加载当前插件配置
|
||||||
:return: 插件配置字典
|
:return: 插件配置字典
|
||||||
"""
|
"""
|
||||||
conf = pconf(self.name)
|
# 优先获取 plugins/config.json 中的全局配置
|
||||||
logger.info(f"loading from global plugin config, plugin_name={self.name}, conf={conf}")
|
plugin_conf = pconf(self.name)
|
||||||
return conf
|
if not plugin_conf:
|
||||||
|
# 全局配置不存在,则获取插件目录下的配置
|
||||||
|
plugin_config_path = os.path.join(self.path, "config.json")
|
||||||
|
if os.path.exists(plugin_config_path):
|
||||||
|
with open(plugin_config_path, "r") as f:
|
||||||
|
plugin_conf = json.load(f)
|
||||||
|
logger.debug(f"loading plugin config, plugin_name={self.name}, conf={plugin_conf}")
|
||||||
|
return plugin_conf
|
||||||
|
|
||||||
def get_help_text(self, **kwargs):
|
def get_help_text(self, **kwargs):
|
||||||
return "暂无帮助信息"
|
return "暂无帮助信息"
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ from bridge.bridge import Bridge
|
|||||||
from bridge.context import ContextType
|
from bridge.context import ContextType
|
||||||
from bridge.reply import Reply, ReplyType
|
from bridge.reply import Reply, ReplyType
|
||||||
from common import const
|
from common import const
|
||||||
from common.log import logger
|
|
||||||
from config import conf
|
from config import conf
|
||||||
from plugins import *
|
from plugins import *
|
||||||
|
|
||||||
@@ -119,16 +118,8 @@ class Tool(Plugin):
|
|||||||
return
|
return
|
||||||
|
|
||||||
def _read_json(self) -> dict:
|
def _read_json(self) -> dict:
|
||||||
curdir = os.path.dirname(__file__)
|
default_config = {"tools": [], "kwargs": {}}
|
||||||
config_path = os.path.join(curdir, "config.json")
|
return super().load_config() or default_config
|
||||||
tool_config = super().load_config()
|
|
||||||
if not tool_config:
|
|
||||||
if not os.path.exists(config_path):
|
|
||||||
return {"tools": [], "kwargs": {}}
|
|
||||||
else:
|
|
||||||
with open(config_path, "r") as f:
|
|
||||||
tool_config = json.load(f)
|
|
||||||
return tool_config
|
|
||||||
|
|
||||||
def _build_tool_kwargs(self, kwargs: dict):
|
def _build_tool_kwargs(self, kwargs: dict):
|
||||||
tool_model_name = kwargs.get("model_name")
|
tool_model_name = kwargs.get("model_name")
|
||||||
|
|||||||
Reference in New Issue
Block a user