I'm creating a connect 4 game in discord.py, using buttons to place pieces. Currently, it resends a new gameboard every time a piece is placed, with the buttons causing issues as they display "this interaction failed", even though its code ran fine. So is there a way to edit the message of the game board to prevent this issue?
def place(name, Line, row):
open_file = open(name, "r")
board = []
piece = open_file.readline().strip("\n")
for x in range(6):
value = open_file.readline()
board.append(value.strip("\n").split(","))
open_file.close()
Place = True
while Place and Line != 6:
if board[Line][row - 1] == ":white_large_square:":
Line = 1
else:
Place = False
if not board[0][row - 1] == piece:
Line -= 1
board[Line][row - 1] = piece
Line1 = ",".join(board[0])
Line2 = ",".join(board[1])
Line3 = ",".join(board[2])
Line4 = ",".join(board[3])
Line5 = ",".join(board[4])
Line6 = ",".join(board[5])
Lines = [Line1, Line2, Line3, Line4, Line5, Line6]
phrase = "\n".join(Lines)
new_piece = ""
if piece == ":green_circle:":
new_piece = ":red_circle:\n"
elif piece == ":red_circle:":
new_piece = ":green_circle:\n"
open_file = open(name, "w")
open_file.write(new_piece phrase)
open_file.close()
return "Valid Move"
else:
return "Invalid Move"
@bot.command(
brief=" Begins your Connect 4 Game",
description=
" Displays the board and buttons, which will place your piece in the desired lane",
Arguements="None")
async def Connect4(ctx):
open_file = open(ctx.author.name "#", "w")
open_file.write(
":green_circle:\n:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:\n:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:\n:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:\n:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:\n:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:\n:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:,:white_large_square:"
)
open_file.close()
open_file = open(ctx.author.name "#", "r")
board = []
piece = open_file.readline()
for _ in range(6):
value = open_file.readline()
board.append(value.strip("\n").split(","))
open_file.close()
L1 = "".join(board[0])
L2 = "".join(board[1])
L3 = "".join(board[2])
L4 = "".join(board[3])
L5 = "".join(board[4])
L6 = "".join(board[5])
button1 = Button(label="",
emoji="1️⃣",
style=discord.ButtonStyle.gray,
row=0)
button2 = Button(label="",
emoji="2️⃣",
style=discord.ButtonStyle.gray,
row=0)
button3 = Button(label="",
emoji="3️⃣",
style=discord.ButtonStyle.gray,
row=0)
button4 = Button(label="",
emoji="4️⃣",
style=discord.ButtonStyle.gray,
row=1)
button5 = Button(label="",
emoji="5️⃣",
style=discord.ButtonStyle.gray,
row=1)
button6 = Button(label="",
emoji="6️⃣",
style=discord.ButtonStyle.gray,
row=1)
button7 = Button(label="",
emoji="7️⃣",
style=discord.ButtonStyle.gray,
row=1)
async def button1Clicked(interaction):
x = place(ctx.author.name "#", 0, 1)
if x == "Invalid Move":
await ctx.send("Invalid Move")
else:
open_file = open(ctx.author.name "#", "r")
board = []
piece = open_file.readline()
for _ in range(6):
value = open_file.readline()
board.append(value.strip("\n").split(","))
open_file.close()
L1 = "".join(board[0])
L2 = "".join(board[1])
L3 = "".join(board[2])
L4 = "".join(board[3])
L5 = "".join(board[4])
L6 = "".join(board[5])
message = piece " turn\n" L1 "\n" L2 "\n" L3 "\n" L4 "\n" L5 "\n" L6
await ctx.send(message, view=view1)
async def button2Clicked(interaction):
x = place(ctx.author.name "#", 0, 2)
if x == "Invalid Move":
await ctx.send("Invalid Move")
else:
open_file = open(ctx.author.name "#", "r")
board = []
piece = open_file.readline()
for _ in range(6):
value = open_file.readline()
board.append(value.strip("\n").split(","))
open_file.close()
L1 = "".join(board[0])
L2 = "".join(board[1])
L3 = "".join(board[2])
L4 = "".join(board[3])
L5 = "".join(board[4])
L6 = "".join(board[5])
message = piece " turn\n" L1 "\n" L2 "\n" L3 "\n" L4 "\n" L5 "\n" L6
await ctx.send(message, view=view1)
async def button3Clicked(interaction):
x = place(ctx.author.name "#", 0, 3)
if x == "Invalid Move":
await ctx.send("Invalid Move")
else:
open_file = open(ctx.author.name "#", "r")
board = []
piece = open_file.readline()
for _ in range(6):
value = open_file.readline()
board.append(value.strip("\n").split(","))
open_file.close()
L1 = "".join(board[0])
L2 = "".join(board[1])
L3 = "".join(board[2])
L4 = "".join(board[3])
L5 = "".join(board[4])
L6 = "".join(board[5])
message = piece " turn\n" L1 "\n" L2 "\n" L3 "\n" L4 "\n" L5 "\n" L6
await ctx.send(message, view=view1)
async def button4Clicked(interaction):
x = place(ctx.author.name "#", 0, 4)
if x == "Invalid Move":
await ctx.send("Invalid Move")
else:
open_file = open(ctx.author.name "#", "r")
board = []
piece = open_file.readline()
for _ in range(6):
value = open_file.readline()
board.append(value.strip("\n").split(","))
open_file.close()
L1 = "".join(board[0])
L2 = "".join(board[1])
L3 = "".join(board[2])
L4 = "".join(board[3])
L5 = "".join(board[4])
L6 = "".join(board[5])
message = piece " turn\n" L1 "\n" L2 "\n" L3 "\n" L4 "\n" L5 "\n" L6
await ctx.send(message, view=view1)
async def button5Clicked(interaction):
x = place(ctx.author.name "#", 0, 5)
if x == "Invalid Move":
await ctx.send("Invalid Move")
else:
open_file = open(ctx.author.name "#", "r")
board = []
piece = open_file.readline()
for _ in range(6):
value = open_file.readline()
board.append(value.strip("\n").split(","))
open_file.close()
L1 = "".join(board[0])
L2 = "".join(board[1])
L3 = "".join(board[2])
L4 = "".join(board[3])
L5 = "".join(board[4])
L6 = "".join(board[5])
message = piece " turn\n" L1 "\n" L2 "\n" L3 "\n" L4 "\n" L5 "\n" L6
await ctx.send(message, view=view1)
async def button6Clicked(interaction):
x = place(ctx.author.name "#", 0, 6)
if x == "Invalid Move":
await ctx.send("Invalid Move")
else:
open_file = open(ctx.author.name "#", "r")
board = []
piece = open_file.readline()
for _ in range(6):
value = open_file.readline()
board.append(value.strip("\n").split(","))
open_file.close()
L1 = "".join(board[0])
L2 = "".join(board[1])
L3 = "".join(board[2])
L4 = "".join(board[3])
L5 = "".join(board[4])
L6 = "".join(board[5])
message = piece " turn\n" L1 "\n" L2 "\n" L3 "\n" L4 "\n" L5 "\n" L6
await ctx.send(message, view=view1)
async def button7Clicked(interaction):
x = place(ctx.author.name "#", 0, 7)
if x == "Invalid Move":
await ctx.send("Invalid Move")
else:
open_file = open(ctx.author.name "#", "r")
board = []
piece = open_file.readline()
for _ in range(6):
value = open_file.readline()
board.append(value.strip("\n").split(","))
open_file.close()
L1 = "".join(board[0])
L2 = "".join(board[1])
L3 = "".join(board[2])
L4 = "".join(board[3])
L5 = "".join(board[4])
L6 = "".join(board[5])
message = piece " turn\n" L1 "\n" L2 "\n" L3 "\n" L4 "\n" L5 "\n" L6
await ctx.send(message, view=view1)
button1.callback = button1Clicked
button2.callback = button2Clicked
button3.callback = button3Clicked
button4.callback = button4Clicked
button5.callback = button5Clicked
button6.callback = button6Clicked
button7.callback = button7Clicked
view1 = View()
view1.add_item(button1)
view1.add_item(button2)
view1.add_item(button3)
view1.add_item(button4)
view1.add_item(button5)
view1.add_item(button6)
view1.add_item(button7)
await ctx.send(piece " turn\n" L1 "\n" L2 "\n" L3 "\n" L4 "\n" L5 "\n"
L6,
view=view1)
CodePudding user response:
Search in the docs: I did it for you The docs is the most useful thing readily available to you.
.
.
message = piece " turn\n" L1 "\n" L2 "\n" L3 "\n" L4 "\n" L5 "\n" L6
m = await ctx.send(message, view=view1) #btw this still sends the message
.
.
m.edit(content=message, view=...)