-
Notifications
You must be signed in to change notification settings - Fork 0
/
discord_bot.py
140 lines (111 loc) · 5.43 KB
/
discord_bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import discord
from discord_token import *
from chess_engine.chess_game import Chess_game
import time
import chess, chess.svg
in_game = False
game_player = None
start_time = None
game = None
def activate_bot():
intents = discord.Intents.default()
intents.message_content = True
# intents = discord.Intents.all()
client = discord.Client(intents=intents)
@client.event
async def on_ready():
print(f'We have logged in as {client.user}')
@client.event
async def on_message(message):
global in_game, game_player, end_time, game
chess_channel = client.get_channel(1083081699363139774)
if message.channel == chess_channel and message.content[0] == "!":
command = message.content
player = message.author
if command == "!help":
help_text = '''
Instructions:\n
Only one player can play against the bot. There will be 10 minutes worth of gametime.\n
to make a move, type in ![move]. For example, if you want to move a pawn from e2 to e4, type "!e2e4".'''
await chess_channel.send(help_text)
if in_game and end_time-time.time() < 0:
await chess_channel.send(f'ran out of time with player {game_player}')
game_player = None
in_game = False
if command[:6] == "!start" and not in_game:
try:
plies = 4
if len(command) == 8 and int(command[7]) <= 4:
plies = int(command[7])
await chess_channel.send(f'Starting chess game with {player} with difficulty at {plies}')
end_time = time.time() + 600
game = Chess_game(plies)
# embed = discord.Embed()
# a = chess.svg.board(board=game.board, size=700)
# embed.description = a
# await chess_channel.send(a)
game_player = player
in_game = True
except Exception:
await chess_channel.send("Incorrect input, please type in correct input to start game. Ex: '!start 4'")
else:
await chess_channel.send(f'Currently in game with {game_player}. {round(end_time - time.time(), 0)} seconds remainig')
if in_game and message.author == game_player:
if command == "!moves":
await chess_channel.send(game.board.legal_moves)
send_string = f'\nTime remaning: {round(end_time - time.time(), 0)}\n'
try:
game.board.push_san(command[1:])
if game.board.is_checkmate():
send_string = f"You won!"
in_game = False
if game.board.is_stalemate():
send_string = "Stalemate"
if game.board.is_insufficient_material():
send_string = "Stalemate"
if game.turn == 1:
game.turn = 0
else:
game.turn = 1
move = game.make_move()
if game.turn == 1:
game.turn = 0
else:
game.turn = 1
if move == 0:
send_string = f"Winner is {game.winner}!"
in_game = False
else:
game.board.push(move)
send_string = f"{send_string}Your move: {command[1:]}\nAI move: {move}\n"
number = 8
board_string = str(game.board)
temp_string = ''
for i in range(len(board_string)):
if board_string[i] == '\n':
temp_string = f"{temp_string} {str(number)}\n{str(number-1)} "
number -= 1
else:
temp_string = temp_string + board_string[i]
temp_string = f"8 {temp_string} 1"
board_string = f"` a b c d e f g h \n{temp_string}\n a b c d e f g h `\n{'='*20}"
board_string = board_string.replace(".","-")
send_string = f"{send_string}{board_string}"
await chess_channel.send(send_string)
except Exception as e:
number = 8
board_string = str(game.board)
temp_string = ''
for i in range(len(board_string)):
if board_string[i] == '\n':
temp_string = f"{temp_string} {str(number)}\n{str(number-1)} "
number -= 1
else:
temp_string = temp_string + board_string[i]
temp_string = f"8 {temp_string} 1"
board_string = f"` a b c d e f g h \n{temp_string}\n a b c d e f g h `\n{'='*20}"
board_string = board_string.replace(".","-")
print(f"Failed to move in chess with exception {e}")
await chess_channel.send(f"{send_string}Illegal move, try again.\n{board_string}")
client.run(token)
activate_bot()