POE2_Message_Forwarder/config.py

90 lines
2.5 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
import json
import sys
import tkinter as tk
from tkinter import messagebox
# 默认配置
DEFAULT_CONFIG = {
# Path of Exile客户端日志路径
"CLIENT_LOG_PATH": "C:/Program Files (x86)/Grinding Gear Games/Path of Exile 2/logs/Client.txt",
# 消息发送API配置
"API_URL": "https://al.xj.rs/api/v1/send",
"API_KEY": "", # 需要用户填写
# 交易通知配置
"TRADE_MESSAGES_CONFIG": {
"enabled": True, # 是否启用交易通知
"message_forward": True, # 是否转发到消息服务
},
# 私聊消息相关配置
"CHAT_MESSAGES": {
"enabled": True, # 是否启用私聊转发
"message_forward": True, # 是否转发到消息服务
}
}
CONFIG_FILE = "config.json"
def show_error(title, message):
"""显示错误消息对话框"""
root = tk.Tk()
root.withdraw() # 隐藏主窗口
messagebox.showerror(title, message)
root.destroy()
sys.exit(1)
def load_config():
"""加载或创建配置文件"""
if not os.path.exists(CONFIG_FILE):
# 创建默认配置文件
try:
with open(CONFIG_FILE, 'w', encoding='utf-8') as f:
json.dump(DEFAULT_CONFIG, f, indent=4, ensure_ascii=False)
show_error(
"配置文件创建成功",
f"已创建默认配置文件: {CONFIG_FILE}\n请编辑配置文件填写必要信息后重启程序"
)
except Exception as e:
show_error(
"错误",
f"创建配置文件失败: {str(e)}"
)
try:
# 读取配置文件
with open(CONFIG_FILE, 'r', encoding='utf-8') as f:
config = json.load(f)
# 检查必要的配置项
if not config.get("API_KEY"):
show_error(
"配置错误",
"请在配置文件中设置API_KEY后重启程序"
)
return config
except json.JSONDecodeError:
show_error(
"配置错误",
"配置文件格式错误请检查JSON格式是否正确"
)
except Exception as e:
show_error(
"错误",
f"读取配置文件失败: {str(e)}"
)
# 加载配置
config = load_config()
# 导出配置项
CLIENT_LOG_PATH = config["CLIENT_LOG_PATH"]
API_URL = config["API_URL"]
API_KEY = config["API_KEY"]
TRADE_MESSAGES_CONFIG = config["TRADE_MESSAGES_CONFIG"]
CHAT_MESSAGES = config["CHAT_MESSAGES"]