Discord.py Button responses interaction failed after a certain time
Asked Answered
C

2

5

I have an extremely basic script that pops up a message with a button with the command ?place

Upon clicking this button the bot replies Hi to the user who clicked it.

If the button isn't interacted with for > approx 3 minutes it then starts to return "interaction failed".

enter image description here

after that the button becomes useless. I assume there is some sort of internal timeout i can't find in the docs. The button does the same thing whether using discord.py (2.0) or pycord. Nothing hits the console. It's as if the button click isn't picked up.

Very occasionally the button starts to work again and a host of these errors hit the console:

discord.errors.NotFound: 404 Not Found (error code: 10062): Unknown interaction
Ignoring exception in view <View timeout=180.0 children=1> for item <Button style=<ButtonStyle.success: 3> url=None disabled=False label='click me' emoji=None row=None>:

I assume the timeout = 180 is the cause of this issue but is anyone aware of how to stop this timeout and why it's happening? I can't see anything in the docs about discord buttons only being usable for 3 mins.

import discord

from discord.ext import commands
intents = discord.Intents.default()
intents.members = True
intents.message_content = True
bot = commands.Bot(command_prefix="?", intents=intents)


embed1=discord.Embed(title="Test", description = f"TESTING",color=0xffffff)   
print("bot connected")
 
@ bot.command(name='place')
async def hello(ctx):
    view = discord.ui.View()
    buttonSign = discord.ui.Button(label = "click me", style= discord.ButtonStyle.green)


    async def buttonSign_callback(interaction):
        userName = interaction.user.id
        embedText = f"test test test"
        embed=discord.Embed(title="Test", description = embedText,color=0xffffff)
        await interaction.response.send_message(f"Hi <@{userName}>")

       

    buttonSign.callback = buttonSign_callback
    view.add_item(item=buttonSign)
    await ctx.send(embed = embed1,view = view)

bot.run(TOKEN)

Coincidentally answered 19/5, 2022 at 23:33 Comment(1)
Please edit your post to trim the code down to a minimal reproducible example. Apart from that, the question is quite nice.Crosswise
C
12

Explanation

By default, Views in discord.py 2.0 have a timeout of 180 seconds (3 minutes). You can fix this error by passing in None as the timeout when creating the view.

Code

@bot.command(name='place')
async def hello(ctx):
    view = discord.ui.View(timeout=None)

References

discord.ui.View.timeout

Crosswise answered 20/5, 2022 at 0:12 Comment(0)
C
1

In addition to the answer, we know that you must set a property

timeout=None 

to discord.ui.View

But also you should add this ui.View in your main Client "on_ready" if you defined a class with your custom view like:

class MainView(discord.ui.View):
    def __init__(self):
        print("init")
        super().__init__(timeout=None)

an example to add the view on ready should be:

async def on_ready(self):
    self.add_view(MainView())

If not will not work with only the "timeout" property

Collin answered 19/12, 2023 at 15:50 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.