This commit is contained in:
SengokuCola
2025-05-29 21:31:41 +08:00
2 changed files with 124 additions and 107 deletions

View File

@@ -3,7 +3,7 @@ from tkinter import ttk, messagebox, filedialog
import tomli
import tomli_w
import os
from typing import Any, Dict, List, Union
from typing import Any, Dict, List
import threading
import time
import re
@@ -271,8 +271,11 @@ class ConfigEditor:
already_in_quick = any(item.get("path") == full_config_path_key for item in quick_settings)
icon = "" if already_in_quick else ""
icon_fg = "#FFD600" # 始终金色
def on_star_click():
self.toggle_quick_setting(full_config_path_key, widget_type, item_name_to_display, item_desc_to_display, already_in_quick)
self.toggle_quick_setting(
full_config_path_key, widget_type, item_name_to_display, item_desc_to_display, already_in_quick
)
# 立即刷新本分组
for widget in parent.winfo_children():
widget.destroy()
@@ -335,13 +338,17 @@ class ConfigEditor:
elif isinstance(value, list):
# 列表使用每行一个输入框的形式
frame_list = ttk.Frame(frame)
frame_list.grid(row=widget_row, column=0, columnspan=content_col_offset_for_star +1, sticky=tk.W+tk.E, padx=5)
frame_list.grid(
row=widget_row, column=0, columnspan=content_col_offset_for_star + 1, sticky=tk.W + tk.E, padx=5
)
# 创建添加和删除按钮
button_frame = ttk.Frame(frame_list)
button_frame.pack(side=tk.RIGHT, padx=5)
add_button = ttk.Button(button_frame, text="+", width=3, command=lambda p=path + [key]: self.add_list_item(frame_list, p))
add_button = ttk.Button(
button_frame, text="+", width=3, command=lambda p=path + [key]: self.add_list_item(frame_list, p)
)
add_button.pack(side=tk.TOP, pady=2)
# 创建列表项框架
@@ -548,9 +555,9 @@ class ConfigEditor:
if setting_type == "bool":
value = bool(value) if value is not None else False
var = tk.BooleanVar(value=value)
checkbox = ttk.Checkbutton(frame, text="",
variable=var,
command=lambda p=path, v=var: self.on_quick_setting_changed(p, v))
checkbox = ttk.Checkbutton(
frame, text="", variable=var, command=lambda p=path, v=var: self.on_quick_setting_changed(p, v)
)
checkbox.pack(anchor=tk.W, padx=5, pady=(0, 5))
elif setting_type == "text":
@@ -569,8 +576,9 @@ class ConfigEditor:
elif setting_type == "list":
# 对于列表类型,创建一个按钮来打开编辑窗口
button = ttk.Button(frame, text="编辑列表",
command=lambda p=path, s=setting: self.open_list_editor(p, s))
button = ttk.Button(
frame, text="编辑列表", command=lambda p=path, s=setting: self.open_list_editor(p, s)
)
button.pack(anchor=tk.W, padx=5, pady=(0, 5))
def create_list_item(self, parent, value, index, entry_vars, path):
@@ -585,8 +593,12 @@ class ConfigEditor:
var.trace_add("write", lambda *args: self.on_value_changed())
# 创建删除按钮
del_button = ttk.Button(item_frame, text="-", width=3,
command=lambda: self.remove_list_item(parent, item_frame, entry_vars, index, path))
del_button = ttk.Button(
item_frame,
text="-",
width=3,
command=lambda: self.remove_list_item(parent, item_frame, entry_vars, index, path),
)
del_button.pack(side=tk.RIGHT, padx=5)
# 存储变量引用
@@ -757,13 +769,13 @@ class ConfigEditor:
button_frame.pack(fill=tk.X, pady=10)
# 添加按钮
add_button = ttk.Button(button_frame, text="添加",
command=lambda: self.add_list_item(items_frame, path))
add_button = ttk.Button(button_frame, text="添加", command=lambda: self.add_list_item(items_frame, path))
add_button.pack(side=tk.LEFT, padx=5)
# 保存按钮
save_button = ttk.Button(button_frame, text="保存",
command=lambda: self.save_list_editor(dialog, path, entry_vars))
save_button = ttk.Button(
button_frame, text="保存", command=lambda: self.save_list_editor(dialog, path, entry_vars)
)
save_button.pack(side=tk.RIGHT, padx=5)
def save_list_editor(self, dialog, path, entry_vars):
@@ -810,20 +822,21 @@ class ConfigEditor:
self.on_value_changed()
def toggle_quick_setting(self, full_path, widget_type, name, desc, already_in_quick):
quick_settings = self.editor_config.setdefault("editor", {}).setdefault("quick_settings", {}).setdefault("items", [])
quick_settings = (
self.editor_config.setdefault("editor", {}).setdefault("quick_settings", {}).setdefault("items", [])
)
if already_in_quick:
# 移除
self.editor_config["editor"]["quick_settings"]["items"] = [item for item in quick_settings if item.get("path") != full_path]
self.editor_config["editor"]["quick_settings"]["items"] = [
item for item in quick_settings if item.get("path") != full_path
]
else:
# 添加
quick_settings.append({
"name": name,
"description": desc,
"path": full_path,
"type": widget_type
})
quick_settings.append({"name": name, "description": desc, "path": full_path, "type": widget_type})
# 保存到configexe.toml
import tomli_w, os
import tomli_w
import os
config_path = os.path.join(os.path.dirname(__file__), "configexe.toml")
with open(config_path, "wb") as f:
tomli_w.dump(self.editor_config, f)
@@ -1087,8 +1100,9 @@ class ConfigEditor:
def main():
root = tk.Tk()
app = ConfigEditor(root)
_app = ConfigEditor(root)
root.mainloop()
if __name__ == "__main__":
main()

View File

@@ -2,7 +2,11 @@ from src.common.logger_manager import get_logger
from src.chat.message_receive.message import MessageRecv
from src.chat.message_receive.storage import MessageStorage
from src.config.config import global_config
from src.chat.message_receive.chat_stream import ChatStream
from maim_message import UserInfo
from datetime import datetime
import re
logger = get_logger("pfc")
@@ -14,7 +18,7 @@ class MessageProcessor:
self.storage = MessageStorage()
@staticmethod
def _check_ban_words(text: str, chat, userinfo) -> bool:
def _check_ban_words(text: str, chat: ChatStream, userinfo: UserInfo) -> bool:
"""检查消息中是否包含过滤词"""
for word in global_config.message_receive.ban_words:
if word in text:
@@ -26,13 +30,12 @@ class MessageProcessor:
return False
@staticmethod
def _check_ban_regex(text: str, chat, userinfo) -> bool:
def _check_ban_regex(text: str, chat: ChatStream, userinfo: UserInfo) -> bool:
"""检查消息是否匹配过滤正则表达式"""
for pattern in global_config.chat.ban_msgs_regex:
if pattern.search(text):
logger.info(
f"[{chat.group_info.group_name if chat.group_info else '私聊'}]{userinfo.user_nickname}:{text}"
)
for pattern in global_config.message_receive.ban_msgs_regex:
if re.search(pattern, text):
chat_name = chat.group_info.group_name if chat.group_info else "私聊"
logger.info(f"[{chat_name}]{userinfo.user_nickname}:{text}")
logger.info(f"[正则表达式过滤]消息匹配到{pattern}filtered")
return True
return False