Update lichess-bot.py
Browse files- lichess-bot.py +7 -98
lichess-bot.py
CHANGED
|
@@ -1,101 +1,10 @@
|
|
| 1 |
-
"""Starting point for lichess-bot."""
|
| 2 |
-
import subprocess
|
| 3 |
import threading
|
| 4 |
-
import
|
| 5 |
-
import time
|
| 6 |
-
import requests
|
| 7 |
-
from lib.lichess_bot import start_program
|
| 8 |
|
| 9 |
-
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
url = f"https://lichess.org/api/bot/game/{game_id}/chat"
|
| 17 |
-
data = {"room": room, "text": str(text).upper()}
|
| 18 |
-
try:
|
| 19 |
-
requests.post(url, headers=HEADERS, data=data, timeout=5)
|
| 20 |
-
except Exception:
|
| 21 |
-
pass
|
| 22 |
-
|
| 23 |
-
def monitor_lichess_events():
|
| 24 |
-
"""Listens continuously to live account events to trigger your text chat rules."""
|
| 25 |
-
while True:
|
| 26 |
-
try:
|
| 27 |
-
# Stream incoming events straight from the official Lichess API endpoint
|
| 28 |
-
url = "https://lichess.org/api/stream/event"
|
| 29 |
-
response = requests.get(url, headers=HEADERS, stream=True, timeout=60)
|
| 30 |
-
|
| 31 |
-
for line in response.iter_lines():
|
| 32 |
-
if line:
|
| 33 |
-
event = json.loads(line.decode("utf-8"))
|
| 34 |
-
|
| 35 |
-
# Whenever a new game starts, spin up an independent chat monitor
|
| 36 |
-
if event.get("type") == "gameStart":
|
| 37 |
-
game_id = event["game"]["id"]
|
| 38 |
-
threading.Thread(target=watch_game_chat, args=(game_id,), daemon=True).start()
|
| 39 |
-
except Exception:
|
| 40 |
-
time.sleep(5)
|
| 41 |
-
|
| 42 |
-
def watch_game_chat(game_id):
|
| 43 |
-
"""Watches a specific game's live state stream for moves, game over results, or custom commands."""
|
| 44 |
-
url = f"https://lichess.org/api/bot/game/stream/{game_id}"
|
| 45 |
-
last_eval = 0.0
|
| 46 |
-
|
| 47 |
-
try:
|
| 48 |
-
response = requests.get(url, headers=HEADERS, stream=True, timeout=60)
|
| 49 |
-
for line in response.iter_lines():
|
| 50 |
-
if line:
|
| 51 |
-
state = json.loads(line.decode("utf-8"))
|
| 52 |
-
|
| 53 |
-
# A. Handle Real-Time Chat Triggers (!help, !explain, fast, slow)
|
| 54 |
-
if state.get("type") == "chatLine":
|
| 55 |
-
text = state.get("text", "").strip().lower()
|
| 56 |
-
username = state.get("username", "")
|
| 57 |
-
|
| 58 |
-
# Ignore the bot talking to avoid loops
|
| 59 |
-
if text == "!help":
|
| 60 |
-
send_chat_message(game_id, "player", "AVAILABLE ACTIONS: !NAME (SHOWS ENGINE GROUP NAME), !EXPLAIN (SHOWS CREDITS), !HELP (SHOWS THIS MESSAGE). SAY 'FAST' OR 'SLOW' IN CHAT TO REQUEST A CHANGE IN PACING.")
|
| 61 |
-
elif text == "!name":
|
| 62 |
-
send_chat_message(game_id, "player", "CBOT ORIGINAL ENGINES")
|
| 63 |
-
elif text == "!explain":
|
| 64 |
-
send_chat_message(game_id, "player", "I AM CBOT AND I USE A SPECIAL C++ ENGINE TO MOVE. THIS ENGINE IS CREATED BY @HECTOZAR.")
|
| 65 |
-
elif text == "fast":
|
| 66 |
-
send_chat_message(game_id, "player", "SPEED MODE CHANGED! MOVING INSTANTLY.")
|
| 67 |
-
elif text == "slow":
|
| 68 |
-
send_chat_message(game_id, "player", "SLOW THINKING ENGAGED! MOVING WITH A 5 SECOND DELAY.")
|
| 69 |
-
|
| 70 |
-
# B. Handle Dynamic Evaluation Moves & Blunder Reaction Alerts
|
| 71 |
-
elif state.get("type") == "gameState":
|
| 72 |
-
# Parse evaluation score metrics safely
|
| 73 |
-
# Drops trigger "OH NO, I DIDNT SEE THAT!" or "ALL IS LOST"
|
| 74 |
-
# Spikes trigger "NICE MOVE!!!" or "YAY, I MIGHT WIN!"
|
| 75 |
-
pass
|
| 76 |
-
|
| 77 |
-
# C. Handle Game Results (Win, Loss conditions)
|
| 78 |
-
elif state.get("type") == "gameFull":
|
| 79 |
-
# Fallback structural checking logic
|
| 80 |
-
pass
|
| 81 |
-
|
| 82 |
-
# Handle official final status strings
|
| 83 |
-
status = state.get("status", {}).get("name", "")
|
| 84 |
-
if status in ["mate", "resign", "outoftime"]:
|
| 85 |
-
winner = state.get("winner")
|
| 86 |
-
# If winner string matches, the opponent lost, otherwise the bot lost
|
| 87 |
-
if winner:
|
| 88 |
-
send_chat_message(game_id, "player", "YOU PLAYED WELL, TRY AGAIN NEXT TIME!")
|
| 89 |
-
else:
|
| 90 |
-
send_chat_message(game_id, "player", "YOU DID WELL, I WILL TRY TO WIN YOU NEXT TIME")
|
| 91 |
-
break
|
| 92 |
-
except Exception:
|
| 93 |
-
pass
|
| 94 |
-
|
| 95 |
-
# Run your custom triggers inside a protected system safe background threat
|
| 96 |
-
background_worker = threading.Thread(target=monitor_lichess_events, daemon=True)
|
| 97 |
-
background_worker.start()
|
| 98 |
-
|
| 99 |
-
# --- INITIALIZE MAIN CORE BOOT UP ---
|
| 100 |
-
if __name__ == "__main__":
|
| 101 |
-
start_program()
|
|
|
|
|
|
|
|
|
|
| 1 |
import threading
|
| 2 |
+
from http.server import SimpleHTTPRequestHandler, HTTPServer
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
def run_dummy_server():
|
| 5 |
+
# Hugging Face looks for port 7860 by default
|
| 6 |
+
server = HTTPServer(('0.0.0.0', 7860), SimpleHTTPRequestHandler)
|
| 7 |
+
server.serve_forever()
|
| 8 |
|
| 9 |
+
# Start the server in a separate background thread so it doesn't block the bot
|
| 10 |
+
threading.Thread(target=run_dummy_server, daemon=True).start()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|