From 9b8070594de2b3942cb380c67b50b5c06d4ba093 Mon Sep 17 00:00:00 2001 From: wood Date: Wed, 4 Sep 2024 21:21:00 +0800 Subject: [PATCH] =?UTF-8?q?(=E9=87=8D=E6=9E=84=E9=93=BE=E6=8E=A5=E8=BF=87?= =?UTF-8?q?=E6=BB=A4=E5=99=A8=E4=BB=A5=E4=BC=98=E5=8C=96=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=90=8C=E6=AD=A5=E5=92=8C=E6=97=A5=E5=BF=97=E8=AE=B0=E5=BD=95?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bot_commands.py | 11 ++++------- src/guard.py | 18 ++++++++---------- src/link_filter.py | 6 ++---- 3 files changed, 14 insertions(+), 21 deletions(-) diff --git a/src/bot_commands.py b/src/bot_commands.py index 4d05c3b..628a5e1 100644 --- a/src/bot_commands.py +++ b/src/bot_commands.py @@ -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() # 确保使用最新数据 diff --git a/src/guard.py b/src/guard.py index 326e541..80cb2c0 100644 --- a/src/guard.py +++ b/src/guard.py @@ -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) diff --git a/src/link_filter.py b/src/link_filter.py index 98c69bd..c6105f5 100644 --- a/src/link_filter.py +++ b/src/link_filter.py @@ -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):