Created
April 21, 2020 04:29
-
-
Save nmay231/a06701ae9baa137253572bd46385f28f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import discord | |
import chess | |
token = "It's a secret *wink wink*" | |
class ChessClient(discord.Client): | |
def __init__(self): | |
super().__init__() | |
self.chess_board = None | |
self.players = None | |
self.resigning = False | |
async def on_ready(self): | |
print(f'logged on as {self.user}') | |
async def on_message(self, message): | |
if message.author.id == self.user.id or message.channel.name.lower() != 'chess': | |
return | |
if self.chess_board: | |
if message.author not in self.players: | |
return | |
elif message.content in ('/resign', '/quit'): | |
if not self.resigning: | |
await message.channel.send('Are you sure you want to resign in this game? Type `/quit` again to confirm') | |
self.resigning = True | |
else: | |
resigner = self.players[not self.players.index(message.author)] | |
await message.channel.send('Resign confirmed. '+resigner.mention + ', Your opponent has resigned') | |
self.resigning = False | |
self.chess_board = None | |
self.players = None | |
return | |
elif message.author.id == self.players[1].id: | |
pass | |
m = message.content.strip() | |
if m.startswith('m ') or m.startswith('move '): | |
_, m, *_ = m.split() | |
try: | |
self.chess_board.push_san(m) | |
except ValueError as err: | |
if err.args[0].startswith('illegal'): | |
reply = f'Illegal move! `{m}`' | |
else: | |
reply = f'Malformed move notation `{m}`. Type `/help` to get help on how to write moves.' | |
await message.channel.send(reply) | |
return | |
if self.chess_board.is_game_over(): | |
await message.channel.send(f'Game over, GG!\n```{self.chess_board!s}```') | |
self.chess_board = None | |
self.players = None | |
return | |
self.players = tuple(reversed(self.players)) | |
await message.channel.send('Your turn ' + self.players[0].mention) | |
if message.content.count('challenge') > 0: | |
if len(message.mentions) == 0: | |
await message.channel.send(message.author.mention + ' You must mention someone to challenge them to a game of chess') | |
return | |
if not self.players: | |
if message.mentions[0].id == self.user.id: | |
await message.channel.send('I\'m not very good at chess :flushed: Try challenging someone else!') | |
return | |
self.players = ( | |
message.author, | |
message.mentions[0], | |
) | |
await message.channel.send(message.author.mention + " Challenge initiated") | |
print("Challenge initiated:", message.author.mention, message.mentions[0].mention) | |
else: | |
await message.channel.send(message.author.mention + " Sorry, a challenge has already been initiated") | |
if message.content.count('accept') > 0: | |
if not self.players: | |
await message.channel.send(message.author.mention + " No one challenged *you*") | |
return | |
elif self.players[1].id != message.author.id: | |
await message.channel.send(message.author.mention + " You can't just accept someone else's challenge like that! Rude...") | |
return | |
await message.channel.send("Challenge accepted! Prepare to play " + self.players[0].mention) | |
self.chess_board = chess.Board() | |
# The challenge acceptor goes first | |
self.players = tuple(reversed(self.players)) | |
await message.channel.send(self.players[0].mention + ' You go first') | |
if message.content.count('reject') > 0: | |
if not self.players: | |
await message.channel.send('Why do you reject me? :cry: (There is no challenge to reject)') | |
return | |
elif self.players[1].id != message.author.id: | |
await message.channel.send("But what if they wanted to play...") | |
return | |
await message.channel.send("Maybe another time...\n" + self.players[0].mention + ' They don\'t want to play chess at the moment :disappointed:') | |
self.players = None | |
if message.content.startswith('/help'): | |
await message.channel.send( | |
'Welcome to the chess room!\n' \ | |
+ 'You can challenge a user to a game of chess by typing `challenge` and mentioning the user in the same message (using @-NAME)\n' \ | |
+ 'Accept a challenge by simply typing `accept` (you can reject with `reject`). The game now starts!\n' \ | |
+ 'Move your pieces with `move MOVE` where MOVE is a move in Standard Algebraic Notation, SAN (trust me, it\'s easy!) You can read up on how it works here: http://cfajohnson.com/chess/SAN/\n' \ | |
+ 'The game ends according to the normal rules of chess. Alternatively, you can end it early with `/resign` or `/quit`\n' \ | |
+ 'show the board at any time with `show board`\n' | |
) | |
if message.content.count('show board') > 0: | |
if not self.chess_board: | |
await message.channel.send(''' | |
``` | |
|a b c d e f g h | |
-+--------------- | |
8| I ' M | |
7| W I N N I N G | |
6| Y O U ' R E | |
5| L O S I N G | |
4| T O O B A D | |
3| S O S A D | |
2| | |
1| L O L J K | |
``` | |
*No game is playing right now*''') | |
return | |
board = str(self.chess_board) | |
board = f' |a b c d e f g h\n-+{"-"*15}\n' + '\n'.join(str(8-i)+'|'+row for i, row in enumerate(board.split('\n'))) | |
await message.channel.send(f'```\n{board}\n```') | |
return | |
print(f'Message from {message.author}: {message.content}') | |
client = ChessClient() | |
client.run(token) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment