(重构链接过滤器以优化数据同步和日志记录)

This commit is contained in:
wood 2024-09-04 21:21:00 +08:00
parent fc35e8f19a
commit 9b8070594d
3 changed files with 14 additions and 21 deletions

View File

@ -55,27 +55,24 @@ async def handle_keyword_command(event, command, args):
keyword = ' '.join(args)
if keyword not in link_filter.keywords:
link_filter.add_keyword(keyword)
link_filter.load_data_from_file() # 重新加载以确保数据同步
await event.reply(f"关键词 '{keyword}' 已添加。")
else:
await event.reply(f"关键词 '{keyword}' 已存在。")
elif command == '/delete' and args:
keyword = ' '.join(args)
matching_keywords = [k for k in link_filter.keywords if k.lower() == keyword.lower()]
if matching_keywords:
for k in matching_keywords:
link_filter.remove_keyword(k)
link_filter.load_data_from_file() # 重新加载以确保数据同步
if link_filter.remove_keyword(keyword):
await event.reply(f"关键词 '{keyword}' 已删除。")
else:
# 如果没有精确匹配,尝试查找部分匹配的关键词
similar_keywords = [k for k in link_filter.keywords if keyword.lower() in k.lower()]
if similar_keywords:
await event.reply(f"未找到精确匹配的关键词 '{keyword}'\n\n是否要删除以下相似的关键词?\n" + "\n".join(similar_keywords))
await event.reply(f"未找到精确匹配的关键词 '{keyword}'\n\n以下是相似的关键词:\n" + "\n".join(similar_keywords))
else:
await event.reply(f"关键词 '{keyword}' 不存在。")
else:
await event.reply("无效的命令或参数。")
async def handle_whitelist_command(event, command, args):
if command == '/listwhite':
link_filter.load_data_from_file() # 确保使用最新数据

View File

@ -9,6 +9,14 @@ from link_filter import LinkFilter
from bot_commands import handle_command
import logging
# 环境变量
BOT_TOKEN = os.environ.get('BOT_TOKEN')
ADMIN_ID = int(os.environ.get('ADMIN_ID'))
KEYWORDS_FILE = '/app/data/keywords.json'
WHITELIST_FILE = '/app/data/whitelist.json'
# 设置日志
DEBUG_MODE = os.environ.get('DEBUG_MODE', 'False').lower() == 'true'
@ -22,16 +30,6 @@ link_filter_logger.setLevel(logging.DEBUG if DEBUG_MODE else logging.INFO)
# 调整第三方库的日志级别
logging.getLogger('telethon').setLevel(logging.WARNING)
# 环境变量
BOT_TOKEN = os.environ.get('BOT_TOKEN')
ADMIN_ID = int(os.environ.get('ADMIN_ID'))
KEYWORDS_FILE = '/app/data/keywords.json'
WHITELIST_FILE = '/app/data/whitelist.json'
# 设置日志
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger('TeleGuard')
# 创建 LinkFilter 实例
link_filter = LinkFilter(KEYWORDS_FILE, WHITELIST_FILE)

View File

@ -29,7 +29,6 @@ class LinkFilter:
)
\b
''', re.VERBOSE | re.IGNORECASE)
logger.info(f"LinkFilter initialized with keywords file: {keywords_file} and whitelist file: {whitelist_file}")
def load_json(self, file_path):
@ -81,15 +80,14 @@ class LinkFilter:
logger.debug(f"Keyword already exists: {keyword}")
def remove_keyword(self, keyword):
if self.link_pattern.match(keyword):
keyword = self.normalize_link(keyword)
if keyword in self.keywords:
self.keywords.remove(keyword)
self.load_data_from_file() # 重新加载文件
self.save_keywords()
self.load_data_from_file() # 重新加载以确保数据同步
return True
return False
def should_filter(self, text):
logger.debug(f"Checking text: {text}")
if any(keyword.lower() in text.lower() for keyword in self.keywords):