From 038fa512aaaeed94965a45f13866e9b761f4a95d Mon Sep 17 00:00:00 2001 From: KawaiiYusora Date: Tue, 4 Mar 2025 01:17:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E6=AC=A1=E5=90=AF=E5=8A=A8=E6=A3=80?= =?UTF-8?q?=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env | 2 - .env.prod | 24 ---------- .gitignore | 3 +- bot.py | 46 +++++++++++++++---- ...t_config.toml => bot_config_template.toml} | 0 5 files changed, 38 insertions(+), 37 deletions(-) delete mode 100644 .env delete mode 100644 .env.prod rename config/{bot_config.toml => bot_config_template.toml} (100%) diff --git a/.env b/.env deleted file mode 100644 index cd0d3f3e4..000000000 --- a/.env +++ /dev/null @@ -1,2 +0,0 @@ -# 您不应该修改默认值,这个文件被仓库索引,请修改.env.prod -ENVIRONMENT=.dev \ No newline at end of file diff --git a/.env.prod b/.env.prod deleted file mode 100644 index d70bba206..000000000 --- a/.env.prod +++ /dev/null @@ -1,24 +0,0 @@ -HOST=127.0.0.1 -PORT=8080 - -# 插件配置 -PLUGINS=["src2.plugins.chat"] - -# 默认配置 -MONGODB_HOST=127.0.0.1 -MONGODB_PORT=27017 -DATABASE_NAME=MegBot - -MONGODB_USERNAME = "" # 默认空值 -MONGODB_PASSWORD = "" # 默认空值 -MONGODB_AUTH_SOURCE = "" # 默认空值 - -#key and url -CHAT_ANY_WHERE_BASE_URL=https://api.chatanywhere.tech/v1 -SILICONFLOW_BASE_URL=https://api.siliconflow.cn/v1/ -DEEP_SEEK_BASE_URL=https://api.deepseek.com/v1 - -#定义你要用的api的base_url -DEEP_SEEK_KEY= -CHAT_ANY_WHERE_KEY= -SILICONFLOW_KEY= \ No newline at end of file diff --git a/.gitignore b/.gitignore index 5ce300145..77917fe0e 100644 --- a/.gitignore +++ b/.gitignore @@ -9,9 +9,10 @@ message_queue_window.bat message_queue_window.txt queue_update.txt memory_graph.gml +.env .env.* config/bot_config_dev.toml - +config/bot_config.toml # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] diff --git a/bot.py b/bot.py index d24c82ac6..6cc216eeb 100644 --- a/bot.py +++ b/bot.py @@ -6,6 +6,7 @@ from loguru import logger '''彩蛋''' from colorama import init, Fore + init() text = "多年以后,面对行刑队,张三将会回想起他2023年在会议上讨论人工智能的那个下午" rainbow_colors = [Fore.RED, Fore.YELLOW, Fore.GREEN, Fore.CYAN, Fore.BLUE, Fore.MAGENTA] @@ -15,22 +16,48 @@ for i, char in enumerate(text): print(rainbow_text) '''彩蛋''' +# 初次启动检测 +if not os.path.exists("config/bot_config.toml") or not os.path.exists(".env"): + logger.info("检测到bot_config.toml不存在,正在从模板复制") + import shutil + + shutil.copy("config/bot_config_template.toml", "config/bot_config.toml") + logger.info("复制完成,请修改config/bot_config.toml和.env.prod中的配置后重新启动") + +# 初始化.env 默认ENVIRONMENT=prod +if not os.path.exists(".env"): + with open(".env", "w") as f: + f.write("ENVIRONMENT=prod") + + # 检测.env.prod文件是否存在 + if not os.path.exists(".env.prod"): + logger.error("检测到.env.prod文件不存在") + logger.info("请在.env.prod文件中至少填写SILICONFLOW_KEY变量后重新启动") + shutil.copy("./.env.template", "./.env.prod") + # 首先加载基础环境变量.env if os.path.exists(".env"): load_dotenv(".env") logger.success("成功加载基础环境变量配置") -else: - logger.error("基础环境变量配置文件 .env 不存在") - exit(1) -if os.path.exists(".env.dev"): +# 根据 ENVIRONMENT 加载对应的环境配置 +if os.getenv("ENVIRONMENT") == "prod": + logger.success("加载生产环境变量配置") + load_dotenv(".env.prod", override=True) # override=True 允许覆盖已存在的环境变量 +elif os.getenv("ENVIRONMENT") == "dev": logger.success("加载开发环境变量配置") load_dotenv(".env.dev", override=True) # override=True 允许覆盖已存在的环境变量 -elif os.path.exists(".env.prod"): - logger.success("加载环境变量配置") - load_dotenv(".env.prod", override=True) # override=True 允许覆盖已存在的环境变量 +elif os.path.exists(f".env.{os.getenv('ENVIRONMENT')}"): + logger.success(f"加载{os.getenv('ENVIRONMENT')}环境变量配置") + load_dotenv(f".env.{os.getenv('ENVIRONMENT')}", override=True) # override=True 允许覆盖已存在的环境变量 else: - logger.error(f".env对应的环境配置文件不存在,请修改.env文件中的ENVIRONMENT变量为 prod.") + logger.error(f"ENVIRONMENT配置错误,请检查.env文件中的ENVIRONMENT变量对应的.env.{os.getenv('ENVIRONMENT')}是否存在") + exit(1) + +# 检测Key是否存在 +if not os.getenv("DEEP_SEEK_KEY") and not os.getenv("SILICONFLOW_KEY") and not os.getenv("CHAT_ANY_WHERE_KEY"): + logger.error("缺失必要的API KEY") + logger.info(f"请至少在.env.{os.getenv('ENVIRONMENT')}文件中填写SILICONFLOW_KEY后重新启动") exit(1) # 获取所有环境变量 @@ -54,5 +81,4 @@ driver.register_adapter(Adapter) nonebot.load_plugins("src/plugins") if __name__ == "__main__": - - nonebot.run() \ No newline at end of file + nonebot.run() diff --git a/config/bot_config.toml b/config/bot_config_template.toml similarity index 100% rename from config/bot_config.toml rename to config/bot_config_template.toml