From 48d7df5b5d0004bd8c2612965d02af8720f5f903 Mon Sep 17 00:00:00 2001 From: wood Date: Wed, 4 Sep 2024 16:06:09 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E7=99=BD=E5=90=8D=E5=8D=95?= =?UTF-8?q?=E5=92=8C=E5=85=B3=E9=94=AE=E8=AF=8D=E5=A4=84=E7=90=86=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 重构白名单和关键词命令的处理,以改进/list和/listwhite命令的交互模式。该更改还优化了文件`bot_commands.py`中的函数调用流程,并重新排序了命令检查逻辑。 --- data/whitelist.json | 4 +--- src/bot_commands.py | 42 ++++++++++++++++++++++++------------------ 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/data/whitelist.json b/data/whitelist.json index 9f3ba42..b8c4ada 100644 --- a/data/whitelist.json +++ b/data/whitelist.json @@ -1,5 +1,3 @@ [ - "q58.org", - "czl.net", - "woodchen.ink" + "q58.org" ] diff --git a/src/bot_commands.py b/src/bot_commands.py index c7d0891..44dc46f 100644 --- a/src/bot_commands.py +++ b/src/bot_commands.py @@ -9,6 +9,7 @@ import json KEYWORDS_FILE = '/app/data/keywords.json' WHITELIST_FILE = '/app/data/whitelist.json' ADMIN_ID = int(os.environ.get('ADMIN_ID')) + def load_json(file_path): try: with open(file_path, 'r') as f: @@ -47,11 +48,11 @@ async def handle_keyword_command(event, client): command = event.message.text.split(maxsplit=1) - if len(command) > 1: - # 直接执行命令 + if command[0].lower() == '/list': + await execute_keyword_command(event, '/list', '') + elif len(command) > 1: await execute_keyword_command(event, command[0], command[1]) else: - # 进入交互模式 await event.reply(f"请输入你要{command[0][1:]}的关键词:") async with client.conversation(sender) as conv: @@ -61,6 +62,13 @@ async def handle_keyword_command(event, client): async def execute_keyword_command(event, command, keyword): keywords = load_json(KEYWORDS_FILE) + if command.lower() == '/list': + if keywords: + await event.reply(f"当前关键词和语句列表:\n" + "\n".join(keywords)) + else: + await event.reply("关键词列表为空。") + return + if command.lower() == '/add': if keyword.lower() not in keywords: keywords.append(keyword.lower()) @@ -76,12 +84,6 @@ async def execute_keyword_command(event, command, keyword): await event.reply(f"关键词或语句 '{keyword}' 已从列表中删除。") else: await event.reply(f"关键词或语句 '{keyword}' 不在列表中。") - - elif command.lower() == '/list': - if keywords: - await event.reply(f"当前关键词和语句列表:\n" + "\n".join(keywords)) - else: - await event.reply("关键词列表为空。") async def handle_whitelist_command(event, client): sender = await event.get_sender() @@ -90,11 +92,11 @@ async def handle_whitelist_command(event, client): command = event.message.text.split(maxsplit=1) - if len(command) > 1: - # 直接执行命令 + if command[0].lower() == '/listwhite': + await execute_whitelist_command(event, '/listwhite', '') + elif len(command) > 1: await execute_whitelist_command(event, command[0], command[1]) else: - # 进入交互模式 await event.reply(f"请输入你要{command[0][1:]}的域名:") async with client.conversation(sender) as conv: @@ -104,6 +106,13 @@ async def handle_whitelist_command(event, client): async def execute_whitelist_command(event, command, domain): whitelist = load_json(WHITELIST_FILE) + if command.lower() == '/listwhite': + if whitelist: + await event.reply("白名单域名列表:\n" + "\n".join(whitelist)) + else: + await event.reply("白名单为空。") + return + if command.lower() == '/addwhite': if domain.lower() not in whitelist: whitelist.append(domain.lower()) @@ -119,12 +128,9 @@ async def execute_whitelist_command(event, command, domain): await event.reply(f"域名 '{domain}' 已从白名单中移除。") else: await event.reply(f"域名 '{domain}' 不在白名单中。") - - elif command.lower() == '/listwhite': - if whitelist: - await event.reply("白名单域名列表:\n" + "\n".join(whitelist)) - else: - await event.reply("白名单为空。") def get_keywords(): return load_json(KEYWORDS_FILE) + +def get_whitelist(): + return load_json(WHITELIST_FILE)