How to make a python bot that defeats everyone on Lichess.

How to make a python bot that defeats everyone on Lichess.

Automation is the new hot thing today, in this post, I am gonna tell you how can you automate the chess moves just with help of python and a chess engine. You don’t need to know python, you just need to know the basics of any programming language to understand the code, you can still run the bot even with zero knowledge of any programming language, just copy the code at end of this post and edit some things that are commented in the script.

We are going to automate the chess moves on Lichess website for this these are the things that are required:

  • python -> python interpreter

  • pyautogui -> python package

  • stockfish ->python package

  • requests -> python package

  • stockfish engine -> for making strong moves

  • lichess account -> for getting the FEN for the current board position

Step 1: Python

Install python from the official website and tick the add to environment variables in the installer to add python command to the command line.

Screenshot_1.png

Step 2: Downloading Engine

Screenshot_2.png

Download Stockfish 13 which is compatible with your os and hardware from the stockfish website, stockfish is an open-source chess engine that will help the python script to make the best moves according to the position.

Screenshot_3.png

Step 3: Lichess Account

Create a liches account if you don’t have it yet, you will need this to make API requests to play against anyone on lichess.

To get the API key do the following steps:

Login to your Lichess account

Go to this lichess.org/account/oauth/token link to create an API token. Write a description and switch on all the options below and click submit.

Screenshot 2021-05-15 163409.png

Make sure to copy your new personal access token now because You won’t be able to see that again!

Screenshot 2021-05-15 163437.png

Step 4: Python Package

Screenshot 2021-05-15 163643.png

Now we need to install all the packages that I have written above pyautogui, stockfish and requests, To install a python package we need to do pip install “package-name” in the command line without double-quotes.

Step 5: Lichess Typing Mode

Screenshot 2021-05-15 163803.png

Screenshot 2021-05-15 163744.png

Switch on input moves with the keyboard mode from the Lichess website so that Lichess can read the moves from the keyboard that are written by out python bot.

Step 6: Coding

Now we start to write the code in notepad++ or vs-code the editor of your choice.

import pyautogui
from stockfish import Stockfish
import requests
def hit(key):
    pyautogui.press(key)
    return
dir=r""
# complete path of your stockfish.exe file in double quotes up
# if your location is C:/stock/stockfish-x64.exe then
# dir = r"C:/stock/stockfish-x64.exe"
stockfish = Stockfish(dir, parameters={"Threads":2})
# increase or decrese cpu threads as you want
print(stockfish.get_parameters())
headers = {'Authorization': 'Bearer "API"'} 
# type your api key instead of API without double quotes
# headers line will look like this => headers = {'Authorization': 'Bearer lskdhflksdf'} 
while True:
    response = requests.get(
        "https://lichess.org/api/account/playing", headers=headers)
    jsonD = response.json()
    stockfish.set_fen_position(
        jsonD["nowPlaying"][0]["fen"]+" "+jsonD["nowPlaying"][0]["color"][0])
    if(jsonD["nowPlaying"][0]["isMyTurn"]):
      # calling pautogui
        pyautogui.keyDown('enter')
        # increase move time to increase engine strength
        move = stockfish.get_best_move_time(2500)
        hit(move[0])
        hit(move[1])
        hit(move[2])
        hit(move[3])
        # while loop will end when someone gets checkmate or resigns

Now start the game from the website and run the program as python main.py if you have named the python file as main.py and then switch to chrome tab to see your bot defeat everyone.

Warning: Don’t do this in your main Lichess account because Lichess can ban your account for cheating.