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

Search for a command to run...

No comments yet. Be the first to comment.
When it comes to online security, two terms you’ve probably heard a lot are authentication and authorization. These concepts often go hand in hand, but they’re not the same thing. Understanding the difference between them is important—not just for te...
You don't have to be a professional computer expert to access the dark web. The dark web covers around 6% of the total internet. While the deep web covers around 90% and only 4-5% is the surface web that we are using till now. You just need to have t...

You just started learning solidity and the functions look a bit overwhelming in solidity? Don't worry I'll explain everything about the functions, how to use them, visibility, and mutability in this post. If you are here then I assume you already hav...

Do you want to get your business on the internet? Your website must rank on Google and other search engines, for increasing your business. Speed of the website is very important for SEO because the faster speed of the website increases user experienc...

GitHub is the best way to host static assets. But most people don't know this so they tend to buy a hosting service that is not cheap. From now on you are gonna host all your static websites on Github pages. Let's understand everything about GitHub p...

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
Install python from the official website and tick the add to environment variables in the installer to add python command to the command line.


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.

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 https://lichess.org/account/oauth/token link to create an API token. Write a description and switch on all the options below and click submit.

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


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.


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.
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.