C++from selenium import webdriver
from selenium.webdriver.edge.service import Service
from selenium.webdriver.edge.options import Options
from selenium.webdriver.common.by import By
import time
import os
import ctypes
import sys
import threading
import keyboard # 新增
# ====== 配置 ======
CHANNEL_URL = "https://discord.com/channels/668997237010055189/1349166028126556160"
driver_path = os.path.join(os.path.dirname(__file__), "msedgedriver.exe")
service = Service(executable_path=driver_path)
MAX_MESSAGES_TO_CHECK = 20
# ==================
# 全局变量:模式开关(线程安全)
rift_only_mode = True # 初始为仅 rift 模式
mode_lock = threading.Lock()
def toggle_rift_mode(enable):
global rift_only_mode
with mode_lock:
rift_only_mode = enable
status = "开启" if enable else "关闭"
print(f"\n🔔 已{status}「仅 rift 模式」")
def show_alert(title, message):
try:
from plyer import notification
notification.notify(title=title, message=message, timeout=5)
except:
pass
if sys.platform == "win32":
ctypes.windll.user32.MessageBoxW(0, message, title, 0x40 | 0x1000)
def monitor_messages():
"""主监控线程"""
options = Options()
options.add_argument("--start-maximized")
driver = webdriver.Edge(service=service, options=options)
try:
print("正在打开 Discord...")
driver.get(CHANNEL_URL)
input("✅ 进入 #game 频道后按回车开始监控...\n")
print("⌨️ 快捷键:")
print(" Ctrl + F1 → 仅监控 rift 消息")
print(" Ctrl + F2 → 监控所有 Flower 消息")
print("-" * 50)
processed_contents = set()
while True:
try:
# 获取最新消息(限制数量)
containers = driver.find_elements(
By.CSS_SELECTOR,
'div[data-list-item-id*="chat-messages"]'
)[-MAX_MESSAGES_TO_CHECK:]
new_found = False
for container in containers:
try:
content_elem = container.find_element(
By.CSS_SELECTOR,
'div[class*="embedDescription"] > span'
)
content = content_elem.text.strip()
if not content or len(content) < 5:
continue
if content in processed_contents:
continue
# 读取当前模式(线程安全)
current_mode = rift_only_mode
if current_mode and "rift" not in content.lower():
processed_contents.add(content)
continue
print(f"🕒 {time.strftime('%H:%M:%S')}")
print(f"💬 {'[Rift]' if current_mode else '[All]'}: {content}")
print("-" * 50)
title = "🌀 Rift 警报" if current_mode else "🌸 Flower 事件"
show_alert(title, content)
processed_contents.add(content)
new_found = True
except Exception:
continue
if not new_found:
mode_str = "rift" if rift_only_mode else "所有"
print(f"[{time.strftime('%H:%M:%S')}] 无新 {mode_str} 消息。")
time.sleep(5)
except KeyboardInterrupt:
break
except Exception as e:
print(f"[错误] {e}")
time.sleep(5)
finally:
driver.quit()
print("监控已结束。")
def listen_hotkeys():
"""监听快捷键的后台线程"""
print("监听页面快捷键...")
# 注册全局热键
keyboard.add_hotkey('ctrl+f1', lambda: toggle_rift_mode(True))
keyboard.add_hotkey('ctrl+f2', lambda: toggle_rift_mode(False))
# 保持监听
keyboard.wait() # 阻塞直到程序退出
if __name__ == "__main__":
# 启动快捷键监听(后台线程)
hk_thread = threading.Thread(target=listen_hotkeys, daemon=True)
hk_thread.start()
# 启动主监控
monitor_messages()