'Is there a way to replace a button in discord.py 2.0?
So I am trying to make a TicTacToe bot but with buttons in discord.py 2.0
The problem I am facing is that when I click on button1 it works perfectly i.e it disables itself and shows the x mark but when I interact with button2 it changes the button2 to be the o mark but it removes the previous x mark from button1
So I want a way in which I can preserve the changes made to buttons before
I tried using discord.components and arrays but I use replit that doesn't have discord 2.0 package in it. You have to manually install it. It causes an error as replit configures the package to discord 1.7.3 which does not support buttons
I know I am not the smartest so any other better way to do it would also be great!
@client.command(aliases=["gp"])
async def gamepad(ctx,play_1:discord.Member,play_2:discord.Member,turnis:discord.Member,counti):
button1=Button(emoji="<:t0:978369178937471106>",style=discord.ButtonStyle.gray)
button2=Button(emoji="<:t1:978369216514248764>",style=discord.ButtonStyle.gray)
button3=Button(emoji="<:t2:978369254724366417>",style=discord.ButtonStyle.gray)
button4=Button(emoji="<:t3:978369578251993098>",style=discord.ButtonStyle.gray,row=2)
button5=Button(emoji="<:t4:978369613693874186>",style=discord.ButtonStyle.gray,row=2)
button6=Button(emoji="<:t5:978369653795610704>",style=discord.ButtonStyle.gray,row=2)
button7=Button(emoji="<:t6:978369690558668913>",style=discord.ButtonStyle.gray,row=3)
button8=Button(emoji="<:t7:978369733055356928>",style=discord.ButtonStyle.gray,row=3)
button9=Button(emoji="<:t8:978369768765685840>",style=discord.ButtonStyle.gray,row=3)
view=View()
view.add_item(button1)
view.add_item(button2)
view.add_item(button3)
view.add_item(button4)
view.add_item(button5)
view.add_item(button6)
view.add_item(button7)
view.add_item(button8)
view.add_item(button9)
em=Embed(title=f"Game between {play_1} and {play_2}",color=0x2c2f33)
await ctx.send(view=view,embed=em)
async def button_callback1(interaction):
button1=Button(emoji="<:x_:978379686608515142>",style=discord.ButtonStyle.gray,disabled=True)
vu=View()
vu.add_item(button1)
vu.add_item(button2)
vu.add_item(button3)
vu.add_item(button4)
vu.add_item(button5)
vu.add_item(button6)
vu.add_item(button7)
vu.add_item(button8)
vu.add_item(button9)
await interaction.response.edit_message(view=vu,embed=em)
async def button_callback2(interaction):
button1=Button(emoji="<:o_:978380190856122419>",style=discord.ButtonStyle.gray,disabled=True)
vu=View()
vu.add_item(button1)
vu.add_item(button2)
vu.add_item(button3)
vu.add_item(button4)
vu.add_item(button5)
vu.add_item(button6)
vu.add_item(button7)
vu.add_item(button8)
vu.add_item(button9)
await interaction.response.edit_message(view=view,embed=em)
button1.callback=button_callback1
button2.callback=button_callback2
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
