mirror of
https://github.com/woodchen-ink/Q58Bot.git
synced 2025-07-18 13:52:07 +08:00
重构白名单和关键词处理逻辑
重构白名单和关键词命令的处理,以改进/list和/listwhite命令的交互模式。该更改还优化了文件`bot_commands.py`中的函数调用流程,并重新排序了命令检查逻辑。
This commit is contained in:
parent
0ae0f4dbdb
commit
48d7df5b5d
@ -1,5 +1,3 @@
|
||||
[
|
||||
"q58.org",
|
||||
"czl.net",
|
||||
"woodchen.ink"
|
||||
"q58.org"
|
||||
]
|
||||
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user