Search code examples
pythondiscorddiscord.pydiscord-buttons

"This interaction failed" Discord bot with discord.py


I get "This Interaction failed" in this code:

class SimpleView(discord.ui.View):
@discord.ui.button(label="✔️", style=discord.ButtonStyle.success)
async def approve(self, interaction: discord.Interaction, button: discord.ui.Button):
    await interaction.response.send_message(content="Approved!", ephemeral=True)

@discord.ui.button(label="❌", style=discord.ButtonStyle.red)
async def deny(self, interaction: discord.Interaction, button: discord.ui.Button):
    await interaction.response.send_message(content="Denied!")

@bot.event
async def on_member_join(member: discord.Member):
 view = SimpleView()

 role_name = discord.utils.get(member.guild.roles, name="Test")

 approve_channel_id = 1158716789103013898
 approve = member.guild.get_channel(approve_channel_id)

 if approve is not None:
    await member.add_roles(role_name)
    await approve.send(f"{member} joined the server! Do you Approve?", view=view)
    await view.wait()
 else:
    print(f"Channel with ID {approve_channel_id} not found!")

What im trying to do is when someone joins the server he gets a specific role and in an Admin-only channel a message to be sent with 2 buttons one that accepts the user to be in the server and another to deny. But whan i press a button i get "This Interaction failed" and no errors in the console.


Solution

  • I think you have a timeout error here.

    class SimpleView(discord.ui.View):
        def __init__(self):
            super().__init__(timeout = None)
    
        @discord.ui.button(label="✔️", style=discord.ButtonStyle.green)
        async def approve(self, interaction: discord.Interaction, button: discord.ui.Button,custom_id="accept"):
            await interaction.response.send_message(content="Approved!", ephemeral=True)
    
        @discord.ui.button(label="❌", style=discord.ButtonStyle.red)
        async def deny(self, interaction: discord.Interaction, button: discord.ui.Button,custom_id="denied"):
            await interaction.response.send_message(content="Denied!")
    

    Also if you want to you can make your button respond even after the bot has been restarted:

    In your on_ready() function add bot.add_view(SimpleView()) without any awaiting.

    @bot.event
    async def on_ready():
        bot.add_view(SimpleView())