关键词提醒
2024-6-16
功能设计
1.接受到群聊(所有群聊)的消息,如果包含监控的内容,则将发送该消息内容的QQ号及QQ昵称、发送的消息内容、发送消息的时间(YYYY-MM-DD HH:MM)通过私聊的形式发送给添加人QQ号
2.所有用户均可进行添加,指令为/tips add [监控的内容] 其他指令分别是:/tips info(查询当前个人监控内容) /tips del [监控的内容](删除当前个人监控的内容)
文件存放在/scripts/tips/下,其中:
/tips.py-->处理提示
/init_tips.py-->建立sqlite3数据库存储内容
其中tips.py包含添加关键词、查询关键词、删除关键词、监控消息中是否包含指定内容
tips.py
import sqlite3
import os
import datetime
import logging
DATABASE_PATH = os.path.join(os.path.dirname(__file__), 'tips.db')
def connect_db():
return sqlite3.connect(DATABASE_PATH)
def add_tips(qq_id, content):
conn = connect_db()
cursor = conn.cursor()
cursor.execute('''
INSERT INTO tips (qq_id, content)
VALUES (?, ?)
''', (qq_id, content))
conn.commit()
conn.close()
#查询tip记录(个人)
def get_tips(qq_id):
conn = connect_db()
cursor = conn.cursor()
cursor.execute('''
SELECT content FROM tips
WHERE qq_id = ?
''', (qq_id,))
rows = cursor.fetchall()
conn.close()
return [row[0] for row in rows]
def remove_tips(qq_id, content):
conn = connect_db()
cursor = conn.cursor()
cursor.execute('''
DELETE FROM tips
WHERE qq_id = ? AND content = ?
''', (qq_id, content))
conn.commit()
conn.close()
#检查消息是否包含监控内容
def check_tips(message, group_id, qq_id, qq_nickname, send_private_message):
conn = connect_db()
cursor = conn.cursor()
cursor.execute('''
SELECT qq_id, content FROM tips
''')
rows = cursor.fetchall()
conn.close()
for row in rows:
tip_qq_id, content = row
if content in message:
now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
send_private_message(tip_qq_id, f"监测到关键词 {content}\n{qq_id}({qq_nickname})在{now}\n于群聊{group_id}中发送消息:\n{message}")
def handle_tips_command(command, qq_id, group_id, send_group_message):
logging.info(f"Handling tips command: {command} from user {qq_id}")
parts = command.split()
if len(parts) < 2:
send_group_message(group_id, "命令格式错误, 使用 /help 查看帮助")
return
action = parts[1]
if action == "add" and len(parts) == 3:
content = parts[2]
add_tips(qq_id, content)
send_group_message(group_id, f"已添加监控内容: {content}")
logging.info(f"Added tips content: {content} for user {qq_id}")
elif action == "info" and len(parts) == 2:
tips = get_tips(qq_id)
if tips:
send_group_message(group_id, "你当前监控的内容:\n" + "\n".join(tips))
else:
send_group_message(group_id, "你当前没有监控的内容")
logging.info(f"Retrieved tips info for user {qq_id}")
elif action == "del" and len(parts) == 3:
content = parts[2]
remove_tips(qq_id, content)
send_group_message(group_id, f"已删除监控内容: {content}")
logging.info(f"Deleted tips content: {content} for user {qq_id}")
else:
send_group_message(group_id, "命令格式错误, 使用 /help 查看帮助")
logging.info(f"Invalid tips command format from user {qq_id}")
init_tips.py
import sqlite3
import os
DATABASE_PATH = os.path.join(os.path.dirname(__file__), 'tips.db')
def create_db():
conn = sqlite3.connect(DATABASE_PATH)
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS tips (
id INTEGER PRIMARY KEY AUTOINCREMENT,
qq_id TEXT NOT NULL,
content TEXT NOT NULL
)
''')
conn.commit()
conn.close()
if __name__ == "__main__":
create_db()
然后修改receive_event函数的代码:
if data['post_type'] == 'message' and data['message_type'] == 'private':
……
elif data['post_type'] == 'message' and data['message_type'] == 'group':
……
elif message.startswith('/tip'):
handle_tips_command(message, user_id, group_id, send_group_message)
else:
record_message(group_id, message)
check_tips(message, group_id, user_id, qq_nickname, send_private_message)