I have never left the area of hobbies, so please kindly tell me if you can write more beautifully.
Allow chess matches on Discord using discord.py and chess packages
Register at discord developer portal and get a token
Install discord.py py -3 -m pip install -U discord.py` `` chess package install
py -m pip install chess```
ChessBot.py
import discord
import chess
from discord.ext import commands
token = "token"
bot = commands.Bot(command_prefix="!")
board = None
@bot.event
async def on_ready():
print("ready")
@bot.command()
async def start(ctx):
global board
board = chess.Board()
await ctx.send("I created a board")
await ctx.send("```" + str(board) + "```")
@bot.command()
async def move(ctx,movePos):
global board
if board == None:
await ctx.send("The board has not been created")
return
try:
board.push_san(movePos)
await ctx.send("```" + str(board) + "```")
except:
await ctx.send(movePos + "Is not a valid value")
a = ""
for i in board.legal_moves:
a += str(i) + ","
await ctx.send("> " + a)
if board.is_game_over():
await ctx.send("game over")
board = None
bot.run(token)
I wrote it correctly, but I get a lot of errors (connector.py and http.py)
The cause seems to be that the SSL certificate has expired, so you can install a new certificate
When I couldn't get a valid value, I used `` `legal_moves``` to display the place where I could move as it was, but since unnecessary things were displayed, I turned it with for and combined. I feel that I can write the part over there neatly. Since user information can be acquired with ctx, I would like to use dict to make wins and losses for each user.
https://discord.com/ https://pypi.org/project/chess/ https://teratail.com/questions/267889