0
votes

Its my first time using buttons in discord.py

I want the embed to delete itself when someone clicks the delete button. This is my code but there are some problems.

  • Anyone can delete the message by clicking the button. I only want the user who ran the command to interact with the button.

  • When I click delete button, it automatically deletes all messages which were previously sent by the bot.

msg = await ctx.channel.send(embed=embed,components = [Button(label = "Delete", style=ButtonStyle.red)])
          interaction = await bot.wait_for("button_click")
          if interaction.component.label.startswith("Delete"):
            await msg.delete()
2

2 Answers

0
votes

I am not familiar with this components, but you could add an if statement to check for author id == user id who press said button,

0
votes
msg = await ctx.send(embed = embed,components=[[copy,delete]])
 #Copy and delete are buttons

def check(res):
      return ctx.author == res.user and res.channel == ctx.channel and msg == res.message

while True:
      res = await bot.wait_for("button_click",timeout=60, check=check)
         
      if res.component.label.startswith("Copy User Name"):
        await msg.edit(embed=embed_copy,components = [[back,delete]])
            
      elif res.component.label.startswith("Back"):
        await msg.edit(embed=embed,components=[[copy,delete]])
              
      elif res.component.label.startswith("Delete"):
        await msg.delete()
         

As LeSauvage suggested , this problem can easily be solved by using checks!