Reputation: 17
I am trying to create a bot for Discord in Python. However I am running into an issue with the newly added buttons. The issue is as follows: I use a command multiple times and this results into multiple embeds with buttons, however when I press the button in one of the embeds, the button with the same label in the other embed is also triggered.
The behaviour I would like to have is having the button only triggering in the embed that is being clicked on.
The code I have is
async def open(ctx):
def check(res):
return ctx.author == res.user and res.channel == ctx.channel
blue = ["Possibly", "No"]
blueresult = random.choice(blue)
em = discord.Embed(title= f"Congratulations!",description =f"The result is **{blueresult}**!",color = discord.Color.blue())
yes = Button(style=ButtonStyle.green, label="Yes")
no = Button(style=ButtonStyle.red, label="No")
m = await ctx.send(embed = em,components=[[no,yes]])
res = await client.wait_for("button_click", check=check)
user = res.component.label
if player=="Yes":
await m.edit(embed=em,components=[])
elif player=="No":
await m.edit(embed=em,components=[])
Upvotes: 0
Views: 1073
Reputation: 334
You can change the check function to:
def check(res):
return ctx.author == res.user and res.channel == ctx.channel and m == res.message
But you have to place it under this piece of code:
yes = Button(style=ButtonStyle.green, label="Yes")
no = Button(style=ButtonStyle.red, label="No")
m = await ctx.send(embed = em,components=[[no,yes]])
So the result should be:
yes = Button(style=ButtonStyle.green, label="Yes")
no = Button(style=ButtonStyle.red, label="No")
m = await ctx.send(embed = em,components=[[no,yes]])
def check(res):
return ctx.author == res.user and res.channel == ctx.channel and m == res.message
The new code checks if the embed message the bot sent is the responded message. I tested it, and it worked for me.
Upvotes: 2