"This interaction failed" Discord bot with discord.py stoper

53 Views Asked by At

bot will send an emblem that will contain 1 green button After clicking this button, the user's nickname on the Discord server will be displayed on the emblem and there will be a timer next to it. like thisin photo

but when i click that there's an error "This action failed"

import discord
from discord.ext import commands
from discord.ui import Button, View

bot = commands.Bot(command_prefix='?', intents=discord.Intents.all())
bot.remove_command('help')


@bot.event
async def on_ready():
    print(f'Logged in as {bot.user.name}  {bot.user.id}')
    print('------')
    

@bot.command()
async def tablet(ctx):
    embed = discord.Embed(title="◣**Tablet Ochrony**◢",
                          description="Click Start to begin the timer",
                          color=0b000000)
    embed.add_field(name="Imię i Nazwisko |⏳Na służbie:",
                    value="None",
                    inline=False)
    embed.add_field(name="Time Elapsed:", value="0 seconds", inline=False)

    start_button = Button(style=discord.ButtonStyle.green,
                          label="Status 1",
                          custom_id="button1")

    stop_button = Button(style=discord.ButtonStyle.red,
                         label="Status 3",
                         custom_id="button2")

    pause_button = Button(style=discord.ButtonStyle.blurple,
                          label="Status 2",
                          custom_id="button3")

    view = View()
    view.add_item(start_button)
    view.add_item(pause_button)
    view.add_item(stop_button)
    reset_button = Button(style=discord.ButtonStyle.grey,
                          label="Odejmij czas",
                          custom_id="Reset")
    view.add_item(reset_button)


    message = await ctx.send(embed=embed, view=view)
  # interaction = await bot.wait_for("button_click", check = lambda i: i.custom_id == "button1")
  # ------------------------------------------------------------------------------------
    interaction = await bot.wait_for(
        "button_click",
        check=lambda i:  i.user.id == ctx.author.id and i.message.id == message.id 
      .custom_id == "button1"
    )
  
  # ------------------------------------------------------------------------------------

    if interaction.custom_id == "button1":
        nickname = ctx.author.display_name
        embed.set_field_at(0,
                           name="Imię i Nazwisko |⏳Na służbie:",
                           value=nickname,
                           inline=False)
        await message.edit(embed=embed, view=view)
    elif interaction.custom_id == "button3":
        embed.set_field_at(0,
                           name="Imię i Nazwisko |⏳Na służbie:",
                           value="None",
                           inline=False)
        await message.edit(embed=embed, view=view)

bot.run('token')

after i click any button discord failed popping out.

1

There are 1 best solutions below

0
27Saumya On

The way you're performing an interaction check is very inefficient, you should rather use the discord.ui module for doing the same (docs), here's an example for the same:

from discord.ext import commands

import discord


class Bot(commands.Bot):
    def __init__(self):
        intents = discord.Intents.default()
        intents.message_content = True

        super().__init__(command_prefix=commands.when_mentioned_or('$'), intents=intents)

    async def on_ready(self):
        print(f'Logged in as {self.user} (ID: {self.user.id})')
        print('------')


# Define a simple View that gives us a confirmation menu
class Confirm(discord.ui.View):
    def __init__(self):
        super().__init__()
        self.value = None

    # When the confirm button is pressed, set the inner value to `True` and
    # stop the View from listening to more input.
    # We also send the user an ephemeral message that we're confirming their choice.
    @discord.ui.button(label='Confirm', style=discord.ButtonStyle.green)
    async def confirm(self, interaction: discord.Interaction, button: discord.ui.Button):
        await interaction.response.send_message('Confirming', ephemeral=True)
        self.value = True
        self.stop()

    # This one is similar to the confirmation button except sets the inner value to `False`
    @discord.ui.button(label='Cancel', style=discord.ButtonStyle.grey)
    async def cancel(self, interaction: discord.Interaction, button: discord.ui.Button):
        await interaction.response.send_message('Cancelling', ephemeral=True)
        self.value = False
        self.stop()


bot = Bot()


@bot.command()
async def ask(ctx: commands.Context):
    """Asks the user a question to confirm something."""
    # We create the view and assign it to a variable so we can wait for it later.
    view = Confirm()
    await ctx.send('Do you want to continue?', view=view)
    # Wait for the View to stop listening for input...
    await view.wait()
    if view.value is None:
        print('Timed out...')
    elif view.value:
        print('Confirmed...')
    else:
        print('Cancelled...')


bot.run('token')