重构白名单和关键词处理逻辑

重构白名单和关键词命令的处理,以改进/list和/listwhite命令的交互模式。该更改还优化了文件`bot_commands.py`中的函数调用流程,并重新排序了命令检查逻辑。
This commit is contained in:
wood 2024-09-04 16:06:09 +08:00
parent 0ae0f4dbdb
commit 48d7df5b5d
2 changed files with 25 additions and 21 deletions

View File

@ -1,5 +1,3 @@
[ [
"q58.org", "q58.org"
"czl.net",
"woodchen.ink"
] ]

View File

@ -9,6 +9,7 @@ import json
KEYWORDS_FILE = '/app/data/keywords.json' KEYWORDS_FILE = '/app/data/keywords.json'
WHITELIST_FILE = '/app/data/whitelist.json' WHITELIST_FILE = '/app/data/whitelist.json'
ADMIN_ID = int(os.environ.get('ADMIN_ID')) ADMIN_ID = int(os.environ.get('ADMIN_ID'))
def load_json(file_path): def load_json(file_path):
try: try:
with open(file_path, 'r') as f: 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) 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]) await execute_keyword_command(event, command[0], command[1])
else: else:
# 进入交互模式
await event.reply(f"请输入你要{command[0][1:]}的关键词:") await event.reply(f"请输入你要{command[0][1:]}的关键词:")
async with client.conversation(sender) as conv: 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): async def execute_keyword_command(event, command, keyword):
keywords = load_json(KEYWORDS_FILE) 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 command.lower() == '/add':
if keyword.lower() not in keywords: if keyword.lower() not in keywords:
keywords.append(keyword.lower()) keywords.append(keyword.lower())
@ -77,12 +85,6 @@ async def execute_keyword_command(event, command, keyword):
else: else:
await event.reply(f"关键词或语句 '{keyword}' 不在列表中。") 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): async def handle_whitelist_command(event, client):
sender = await event.get_sender() sender = await event.get_sender()
if sender.id != ADMIN_ID: if sender.id != ADMIN_ID:
@ -90,11 +92,11 @@ async def handle_whitelist_command(event, client):
command = event.message.text.split(maxsplit=1) 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]) await execute_whitelist_command(event, command[0], command[1])
else: else:
# 进入交互模式
await event.reply(f"请输入你要{command[0][1:]}的域名:") await event.reply(f"请输入你要{command[0][1:]}的域名:")
async with client.conversation(sender) as conv: 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): async def execute_whitelist_command(event, command, domain):
whitelist = load_json(WHITELIST_FILE) 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 command.lower() == '/addwhite':
if domain.lower() not in whitelist: if domain.lower() not in whitelist:
whitelist.append(domain.lower()) whitelist.append(domain.lower())
@ -120,11 +129,8 @@ async def execute_whitelist_command(event, command, domain):
else: else:
await event.reply(f"域名 '{domain}' 不在白名单中。") 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(): def get_keywords():
return load_json(KEYWORDS_FILE) return load_json(KEYWORDS_FILE)
def get_whitelist():
return load_json(WHITELIST_FILE)