Spaces:
Sleeping
Sleeping
Merge branch 'master' of https://github.com/careless25/lichess-bot
Browse files- README.md +26 -4
- config.py +2 -2
- config.yml.default +1 -0
- conversation.py +2 -2
- engine_wrapper.py +10 -7
- lichess-bot.py +6 -3
README.md
CHANGED
|
@@ -3,7 +3,8 @@ A bridge between [Lichess API](https://lichess.org/api#tag/Chess-Bot) and bots.
|
|
| 3 |
|
| 4 |
|
| 5 |
## How to Install
|
| 6 |
-
|
|
|
|
| 7 |
- NOTE: Currently, only Python3 is supported
|
| 8 |
- Download the repo into lichess-bot directory
|
| 9 |
- Navigate to the directory in cmd/Terminal: `cd lichess-bot`
|
|
@@ -14,10 +15,10 @@ virtualenv .venv -p python3 #if this fails you probably need to add Python3 to y
|
|
| 14 |
source .venv/bin/activate
|
| 15 |
pip install -r requirements.txt
|
| 16 |
```
|
| 17 |
-
-
|
| 18 |
- Edit the variants: `supported_variants` and time controls: `supported_tc` from the config.yml as necessary
|
| 19 |
|
| 20 |
-
Windows:
|
| 21 |
- Here is a video on how to install the bot: (https://youtu.be/AuncFctntYs). Or you may proceed to the next steps.
|
| 22 |
- NOTE: Currently, only Python 3 is supported.
|
| 23 |
- If you don't have Python, you may download it here: (https://www.python.org/downloads/). When installing it, enable "add Python to PATH", then go to custom installation (this may be not necessary, but on some computers it won't work otherwise) and enable all options (especially "install for all users"), except the last . It's better to install Python in a path without spaces, like "C:\Python\".
|
|
@@ -32,7 +33,7 @@ virtualenv .venv -p python (if this fails you probably need to add Python to you
|
|
| 32 |
./.venv/Scripts/activate (This may not work on Windows, and in this case you need to execute "Set-ExecutionPolicy RemoteSigned" first and choose "Y" there [you may need to run Powershell as administrator]. After you executed the script, change execution policy back with "Set-ExecutionPolicy Restricted" and pressing "Y")
|
| 33 |
pip install -r requirements.txt
|
| 34 |
```
|
| 35 |
-
-
|
| 36 |
- Edit the variants: `supported_variants` and time controls: `supported_tc` from the config.yml as necessary (use # to disable certain ones)
|
| 37 |
|
| 38 |
|
|
@@ -65,6 +66,27 @@ pip install -r requirements.txt
|
|
| 65 |
- To start: `python lichess-bot.py`
|
| 66 |
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
# Acknowledgements
|
| 69 |
Thanks to the Lichess team, especially T. Alexander Lystad and Thibault Duplessis for working with the LeelaChessZero
|
| 70 |
team to get this API up. Thanks to the Niklas Fiekas and his [python-chess](https://github.com/niklasf/python-chess) code which allows engine communication seamlessly.
|
|
|
|
| 3 |
|
| 4 |
|
| 5 |
## How to Install
|
| 6 |
+
|
| 7 |
+
### Mac/Linux:
|
| 8 |
- NOTE: Currently, only Python3 is supported
|
| 9 |
- Download the repo into lichess-bot directory
|
| 10 |
- Navigate to the directory in cmd/Terminal: `cd lichess-bot`
|
|
|
|
| 15 |
source .venv/bin/activate
|
| 16 |
pip install -r requirements.txt
|
| 17 |
```
|
| 18 |
+
- Copy `config.yml.default` to `config.yml`
|
| 19 |
- Edit the variants: `supported_variants` and time controls: `supported_tc` from the config.yml as necessary
|
| 20 |
|
| 21 |
+
### Windows:
|
| 22 |
- Here is a video on how to install the bot: (https://youtu.be/AuncFctntYs). Or you may proceed to the next steps.
|
| 23 |
- NOTE: Currently, only Python 3 is supported.
|
| 24 |
- If you don't have Python, you may download it here: (https://www.python.org/downloads/). When installing it, enable "add Python to PATH", then go to custom installation (this may be not necessary, but on some computers it won't work otherwise) and enable all options (especially "install for all users"), except the last . It's better to install Python in a path without spaces, like "C:\Python\".
|
|
|
|
| 33 |
./.venv/Scripts/activate (This may not work on Windows, and in this case you need to execute "Set-ExecutionPolicy RemoteSigned" first and choose "Y" there [you may need to run Powershell as administrator]. After you executed the script, change execution policy back with "Set-ExecutionPolicy Restricted" and pressing "Y")
|
| 34 |
pip install -r requirements.txt
|
| 35 |
```
|
| 36 |
+
- Copy `config.yml.default` to `config.yml`
|
| 37 |
- Edit the variants: `supported_variants` and time controls: `supported_tc` from the config.yml as necessary (use # to disable certain ones)
|
| 38 |
|
| 39 |
|
|
|
|
| 66 |
- To start: `python lichess-bot.py`
|
| 67 |
|
| 68 |
|
| 69 |
+
## Tips & Tricks
|
| 70 |
+
- You can specify a different config file with the `--config` argument.
|
| 71 |
+
- Here's an example systemd service definition:
|
| 72 |
+
```
|
| 73 |
+
[Unit]
|
| 74 |
+
Description=lichess-bot
|
| 75 |
+
After=network-online.target
|
| 76 |
+
Wants=network-online.target
|
| 77 |
+
|
| 78 |
+
[Service]
|
| 79 |
+
Environment="PYTHONUNBUFFERED=1"
|
| 80 |
+
ExecStart=/usr/bin/python3 /home/thibault/lichess-bot/lichess-bot.py
|
| 81 |
+
WorkingDirectory=/home/thibault/lichess-bot/
|
| 82 |
+
User=thibault
|
| 83 |
+
Group=thibault
|
| 84 |
+
Restart=always
|
| 85 |
+
|
| 86 |
+
[Install]
|
| 87 |
+
WantedBy=multi-user.target
|
| 88 |
+
```
|
| 89 |
+
|
| 90 |
# Acknowledgements
|
| 91 |
Thanks to the Lichess team, especially T. Alexander Lystad and Thibault Duplessis for working with the LeelaChessZero
|
| 92 |
team to get this API up. Thanks to the Niklas Fiekas and his [python-chess](https://github.com/niklasf/python-chess) code which allows engine communication seamlessly.
|
config.py
CHANGED
|
@@ -2,8 +2,8 @@ import yaml
|
|
| 2 |
import os
|
| 3 |
import os.path
|
| 4 |
|
| 5 |
-
def load_config():
|
| 6 |
-
with open(
|
| 7 |
try:
|
| 8 |
CONFIG = yaml.load(stream)
|
| 9 |
except Exception as e:
|
|
|
|
| 2 |
import os
|
| 3 |
import os.path
|
| 4 |
|
| 5 |
+
def load_config(config_file):
|
| 6 |
+
with open(config_file) as stream:
|
| 7 |
try:
|
| 8 |
CONFIG = yaml.load(stream)
|
| 9 |
except Exception as e:
|
config.yml.default
CHANGED
|
@@ -13,6 +13,7 @@ engine: # engine settings
|
|
| 13 |
polyglot_max_depth: 8 # half move max depth
|
| 14 |
polyglot_min_weight: 1 # best move weight
|
| 15 |
polyglot_random: true # select move randomly otherwise select by min weight
|
|
|
|
| 16 |
|
| 17 |
ucioptions: # arbitrary UCI options passed to the engine
|
| 18 |
Move Overhead: 100 # increase if your bot flags games too often
|
|
|
|
| 13 |
polyglot_max_depth: 8 # half move max depth
|
| 14 |
polyglot_min_weight: 1 # best move weight
|
| 15 |
polyglot_random: true # select move randomly otherwise select by min weight
|
| 16 |
+
silence_stderr: false # some engines (yes you, leela) are very noisy
|
| 17 |
|
| 18 |
ucioptions: # arbitrary UCI options passed to the engine
|
| 19 |
Move Overhead: 100 # increase if your bot flags games too often
|
conversation.py
CHANGED
|
@@ -19,9 +19,9 @@ class Conversation():
|
|
| 19 |
if cmd == "wait" and game.is_abortable():
|
| 20 |
game.abort_in(60)
|
| 21 |
self.send_reply(line, "Waiting 60 seconds...")
|
| 22 |
-
|
| 23 |
self.send_reply(line, "{} (lichess-bot v{})".format(self.engine.name(), self.version))
|
| 24 |
-
|
| 25 |
self.send_reply(line, "How to run your own bot: lichess.org/api#tag/Chess-Bot")
|
| 26 |
|
| 27 |
def send_reply(self, line, reply):
|
|
|
|
| 19 |
if cmd == "wait" and game.is_abortable():
|
| 20 |
game.abort_in(60)
|
| 21 |
self.send_reply(line, "Waiting 60 seconds...")
|
| 22 |
+
elif cmd == "name":
|
| 23 |
self.send_reply(line, "{} (lichess-bot v{})".format(self.engine.name(), self.version))
|
| 24 |
+
elif cmd == "howto":
|
| 25 |
self.send_reply(line, "How to run your own bot: lichess.org/api#tag/Chess-Bot")
|
| 26 |
|
| 27 |
def send_reply(self, line, reply):
|
engine_wrapper.py
CHANGED
|
@@ -3,6 +3,7 @@ import chess
|
|
| 3 |
import chess.xboard
|
| 4 |
import chess.uci
|
| 5 |
import backoff
|
|
|
|
| 6 |
|
| 7 |
@backoff.on_exception(backoff.expo, BaseException, max_time=120)
|
| 8 |
def create_engine(config, board):
|
|
@@ -26,15 +27,17 @@ def create_engine(config, board):
|
|
| 26 |
commands.append("--gpu")
|
| 27 |
commands.append(str(gpu))
|
| 28 |
|
|
|
|
|
|
|
| 29 |
if engine_type == "xboard":
|
| 30 |
-
return XBoardEngine(board, commands, config.get("xboardoptions"))
|
| 31 |
|
| 32 |
-
return UCIEngine(board, commands, config.get("ucioptions"))
|
| 33 |
|
| 34 |
|
| 35 |
class EngineWrapper:
|
| 36 |
|
| 37 |
-
def __init__(self, board, commands, options=None):
|
| 38 |
pass
|
| 39 |
|
| 40 |
def set_time_control(self, game):
|
|
@@ -63,9 +66,9 @@ class EngineWrapper:
|
|
| 63 |
|
| 64 |
class UCIEngine(EngineWrapper):
|
| 65 |
|
| 66 |
-
def __init__(self, board, commands, options):
|
| 67 |
commands = commands[0] if len(commands) == 1 else commands
|
| 68 |
-
self.engine = chess.uci.popen_engine(commands)
|
| 69 |
|
| 70 |
self.engine.uci()
|
| 71 |
|
|
@@ -102,9 +105,9 @@ class UCIEngine(EngineWrapper):
|
|
| 102 |
|
| 103 |
class XBoardEngine(EngineWrapper):
|
| 104 |
|
| 105 |
-
def __init__(self, board, commands, options=None):
|
| 106 |
commands = commands[0] if len(commands) == 1 else commands
|
| 107 |
-
self.engine = chess.xboard.popen_engine(commands)
|
| 108 |
|
| 109 |
self.engine.xboard()
|
| 110 |
|
|
|
|
| 3 |
import chess.xboard
|
| 4 |
import chess.uci
|
| 5 |
import backoff
|
| 6 |
+
import subprocess
|
| 7 |
|
| 8 |
@backoff.on_exception(backoff.expo, BaseException, max_time=120)
|
| 9 |
def create_engine(config, board):
|
|
|
|
| 27 |
commands.append("--gpu")
|
| 28 |
commands.append(str(gpu))
|
| 29 |
|
| 30 |
+
silence_stderr = cfg.get("silence_stderr", False)
|
| 31 |
+
|
| 32 |
if engine_type == "xboard":
|
| 33 |
+
return XBoardEngine(board, commands, config.get("xboardoptions"), silence_stderr)
|
| 34 |
|
| 35 |
+
return UCIEngine(board, commands, config.get("ucioptions"), silence_stderr)
|
| 36 |
|
| 37 |
|
| 38 |
class EngineWrapper:
|
| 39 |
|
| 40 |
+
def __init__(self, board, commands, options=None, silence_stderr=False):
|
| 41 |
pass
|
| 42 |
|
| 43 |
def set_time_control(self, game):
|
|
|
|
| 66 |
|
| 67 |
class UCIEngine(EngineWrapper):
|
| 68 |
|
| 69 |
+
def __init__(self, board, commands, options, silence_stderr=False):
|
| 70 |
commands = commands[0] if len(commands) == 1 else commands
|
| 71 |
+
self.engine = chess.uci.popen_engine(commands, stderr = subprocess.DEVNULL if silence_stderr else None)
|
| 72 |
|
| 73 |
self.engine.uci()
|
| 74 |
|
|
|
|
| 105 |
|
| 106 |
class XBoardEngine(EngineWrapper):
|
| 107 |
|
| 108 |
+
def __init__(self, board, commands, options=None, silence_stderr=False):
|
| 109 |
commands = commands[0] if len(commands) == 1 else commands
|
| 110 |
+
self.engine = chess.xboard.popen_engine(commands, stderr = subprocess.DEVNULL if silence_stderr else None)
|
| 111 |
|
| 112 |
self.engine.xboard()
|
| 113 |
|
lichess-bot.py
CHANGED
|
@@ -10,6 +10,7 @@ import logging
|
|
| 10 |
import multiprocessing
|
| 11 |
import traceback
|
| 12 |
import logging_pool
|
|
|
|
| 13 |
from config import load_config
|
| 14 |
from conversation import Conversation, ChatLine
|
| 15 |
from functools import partial
|
|
@@ -22,7 +23,7 @@ try:
|
|
| 22 |
except ImportError:
|
| 23 |
from http.client import BadStatusLine as RemoteDisconnected
|
| 24 |
|
| 25 |
-
__version__ = "0.
|
| 26 |
|
| 27 |
def upgrade_account(li):
|
| 28 |
if li.upgrade_to_bot_account() is None:
|
|
@@ -31,6 +32,7 @@ def upgrade_account(li):
|
|
| 31 |
print("Succesfully upgraded to Bot Account!")
|
| 32 |
return True
|
| 33 |
|
|
|
|
| 34 |
def watch_control_stream(control_queue, li):
|
| 35 |
for evnt in li.get_event_stream().iter_lines():
|
| 36 |
if evnt:
|
|
@@ -79,7 +81,6 @@ def start(li, user_profile, engine_factory, config):
|
|
| 79 |
pool.apply_async(play_game, [li, game_id, control_queue, engine_factory, user_profile, config])
|
| 80 |
busy_processes += 1
|
| 81 |
print("--- Process Used. Total Queued: {}. Total Used: {}".format(queued_processes, busy_processes))
|
| 82 |
-
|
| 83 |
while ((queued_processes + busy_processes) < max_games and challenge_queue): # keep processing the queue until empty or max_games is reached
|
| 84 |
chlng = challenge_queue.pop(0)
|
| 85 |
try:
|
|
@@ -96,6 +97,7 @@ def start(li, user_profile, engine_factory, config):
|
|
| 96 |
control_stream.terminate()
|
| 97 |
control_stream.join()
|
| 98 |
|
|
|
|
| 99 |
def play_game(li, game_id, control_queue, engine_factory, user_profile, config):
|
| 100 |
updates = li.get_game_stream(game_id).iter_lines()
|
| 101 |
|
|
@@ -225,11 +227,12 @@ if __name__ == "__main__":
|
|
| 225 |
parser = argparse.ArgumentParser(description='Play on Lichess with a bot')
|
| 226 |
parser.add_argument('-u', action='store_true', help='Add this flag to upgrade your account to a bot account.')
|
| 227 |
parser.add_argument('-v', action='store_true', help='Verbose output. Changes log level from INFO to DEBUG.')
|
|
|
|
| 228 |
args = parser.parse_args()
|
| 229 |
|
| 230 |
logger = logging.basicConfig(level=logging.DEBUG if args.v else logging.INFO)
|
| 231 |
|
| 232 |
-
CONFIG = load_config()
|
| 233 |
li = lichess.Lichess(CONFIG["token"], CONFIG["url"], __version__)
|
| 234 |
|
| 235 |
user_profile = li.get_profile()
|
|
|
|
| 10 |
import multiprocessing
|
| 11 |
import traceback
|
| 12 |
import logging_pool
|
| 13 |
+
import backoff
|
| 14 |
from config import load_config
|
| 15 |
from conversation import Conversation, ChatLine
|
| 16 |
from functools import partial
|
|
|
|
| 23 |
except ImportError:
|
| 24 |
from http.client import BadStatusLine as RemoteDisconnected
|
| 25 |
|
| 26 |
+
__version__ = "0.13"
|
| 27 |
|
| 28 |
def upgrade_account(li):
|
| 29 |
if li.upgrade_to_bot_account() is None:
|
|
|
|
| 32 |
print("Succesfully upgraded to Bot Account!")
|
| 33 |
return True
|
| 34 |
|
| 35 |
+
@backoff.on_exception(backoff.expo, BaseException, max_time=600)
|
| 36 |
def watch_control_stream(control_queue, li):
|
| 37 |
for evnt in li.get_event_stream().iter_lines():
|
| 38 |
if evnt:
|
|
|
|
| 81 |
pool.apply_async(play_game, [li, game_id, control_queue, engine_factory, user_profile, config])
|
| 82 |
busy_processes += 1
|
| 83 |
print("--- Process Used. Total Queued: {}. Total Used: {}".format(queued_processes, busy_processes))
|
|
|
|
| 84 |
while ((queued_processes + busy_processes) < max_games and challenge_queue): # keep processing the queue until empty or max_games is reached
|
| 85 |
chlng = challenge_queue.pop(0)
|
| 86 |
try:
|
|
|
|
| 97 |
control_stream.terminate()
|
| 98 |
control_stream.join()
|
| 99 |
|
| 100 |
+
@backoff.on_exception(backoff.expo, BaseException, max_time=600)
|
| 101 |
def play_game(li, game_id, control_queue, engine_factory, user_profile, config):
|
| 102 |
updates = li.get_game_stream(game_id).iter_lines()
|
| 103 |
|
|
|
|
| 227 |
parser = argparse.ArgumentParser(description='Play on Lichess with a bot')
|
| 228 |
parser.add_argument('-u', action='store_true', help='Add this flag to upgrade your account to a bot account.')
|
| 229 |
parser.add_argument('-v', action='store_true', help='Verbose output. Changes log level from INFO to DEBUG.')
|
| 230 |
+
parser.add_argument('--config', help='Specify a configuration file (defaults to ./config.yml)')
|
| 231 |
args = parser.parse_args()
|
| 232 |
|
| 233 |
logger = logging.basicConfig(level=logging.DEBUG if args.v else logging.INFO)
|
| 234 |
|
| 235 |
+
CONFIG = load_config(args.config or "./config.yml")
|
| 236 |
li = lichess.Lichess(CONFIG["token"], CONFIG["url"], __version__)
|
| 237 |
|
| 238 |
user_profile = li.get_profile()
|