I’m making a discord bot that sends a message with two buttons. Both buttons sends a message with a picture/gif when pressed. One of them works but the other one gives an error:
raise NotFound(response, data) discord.errors.NotFound: 404 Not Found (error code: 10062): Unknown interaction
Here is the full code:
import os import discord from discord.ext import commands from discord.ui import Button from discord.ui import View from dotenv import load_dotenv import random intents = discord.Intents.default() intents.message_content = True load_dotenv() TOKEN = os.getenv('Sommer_Challenge_2022_TOKEN') bot = commands.Bot(command_prefix=';', intents=intents, help_command=None) channel = bot.get_channel(channel id here) #facts about sea and beach #fakta om hav og strand fact1 = ('Verdens længste strand hedder "Praia Do Cassino". Den ligger i brasilien og er 241 km lang.🏖️') fact2 = ('Havet dækker omkring 71% af jordens overflade.🌊') fact3 = ('Ca. 73% af folk der besøger stranden, går i vandet.🛀') fact4 = ('Der udledes omkring 8-10 tons plastik i havet hvert år. Det svarer til ca. 375.000 halvliters plastikflasker.') fact5 = ('Over 400 milioner amerikanere går på stranden hvert år.') fact6 = ('Det Røde Hav er det salteste hav i verden. Vandet indenholder ca. 60 gram salt pr. liter.🧂') fact7 = ('Ca. 94% af dyrelivet findes havet.🐟') fact8 = ('Man siger at regnskoven er "jordens lunger", men i virkeligheden producere havet mere end 70% af alt ilt.') fact9 = ('Det er solen som gør vandet blåt. Det samme gælder himlen.☀️') fact10 = ('Hvert år dræber hajer mellem fem til ti mennesker. Til gengæld dræber mennesker omkring 100 millioner hajer om året.🦈') fact11 = ('Ved vesterhavet kan man se bunkere fra 2. verdenskrig.') fact12 = ('Verdens største sandslot har en diameter på 32 meter og har en højde på 21 meter.') #Scratch games about sea and beach #Scratch spil om hav og strand game_button1 = Button(label="Scratch", url='https://scratch.mit.edu/projects/119134771/') game_button2 = Button(label="Scratch", url='https://scratch.mit.edu/projects/113456625/') game_button3 = Button(label="Scratch", url='https://scratch.mit.edu/projects/20743182/') game_button4 = Button(label="Scratch", url='https://scratch.mit.edu/projects/16250800/') game_button5 = Button(label="Scratch", url='https://scratch.mit.edu/projects/559210446/') game_button6 = Button(label="Scratch", url='https://scratch.mit.edu/projects/73644874/') game_button7 = Button(label="Scratch", url='https://scratch.mit.edu/projects/546214248/') game_button8 = Button(label="Scratch", url='https://scratch.mit.edu/projects/571081880/') #tells when bot goes online #fortæller når en bot går online @bot.event async def on_ready(): channel = bot.get_channel(channel name here) await channel.send('Jeg er online!') print(f'{bot.user.name} has connected to Discord!') print(f'conected to: {channel}') @bot.event async def on_member_join(member): await member.send(f'Hej {member.mention}, velkommen på stranden. Nyd solen!☀️') #does stuff when a specific message is recived #gør ting når en bestemt besked er modtaget @bot.event async def on_message(msg): if msg.author != bot.user: if msg.content == (';info'): await msg.channel.send(f'{bot.user.mention} er lavet af "username" i fobindelse med sommer challenge 2022. n Hvis du har nogle spøgsmål eller har brug for hjælp, er du velkommen til at sende en dm til "username". n Kun et af scratch spillene der er givet link til er lavet af "username" n Piratskibet.dk brugernavn: other username') elif msg.content == (';fact'): await msg.channel.send(random.choice([fact1, fact2, fact3, fact5, fact6])) elif msg.content == (';game'): view = View() button = random.choice([game_button1, game_button2, game_button3, game_button4, game_button5, game_button6, game_button7, game_button8]) view.add_item(button) if button != game_button8: await msg.channel.send(view=view) else: await msg.channel.send('Det her spil er lavet af "username"', view=view) elif 'hello' in msg.content: msg.channel.send(f'hello {msg.author.mention}!') elif 'hej' in msg.content: await msg.channel.send(f'hej {msg.author.mention}!') elif msg.content == (';choice'): embed = discord.Embed(title="", description="", color=0xc27c0e) file = discord.File(r"C:Usersusernamevs-code-filesSommer_challenge_2022sandslot_før_DESTROY.png", filename="sandslot_før_DESTROY.png") embed.set_image(url="attachment://sandslot_før_DESTROY.png") embed.set_footer(text="Ødelæg sandslottet?") button = Button(label="Ødelæg!", style=discord.ButtonStyle.danger) button2 = Button(label="Lad det være", style=discord.ButtonStyle.success) view = View() view.add_item(button) view.add_item(button2) async def button2_callback(interaction): await msg.delete() embed = discord.Embed(title="", description="", color=0xc27c0e) file = discord.File(r"C:Usersusernamevs-code-filesSommer_challenge_2022sandslot_don't_destroy.png", filename="sandslot_don't_destroy.png") embed.set_image(url="attachment://sandslot_don't_destroy.png") embed.set_footer(text="Ok") await interaction.response.send_message(file=file, embed=embed, view=None, delete_after=6.25) async def button_callback(interaction): await msg.delete() embed = discord.Embed(title="", description="", color=0xc27c0e) file = discord.File(r"C:UsersusernamePicturessandslot_destoy.gif", filename="sandcastle_destoy.gif") embed.set_image(url="attachment://sandcastle_destoy.gif") embed.set_footer(text="Sandslottet blev ødelagt") await interaction.response.send_message(file=file, embed=embed, view=None, delete_after=6.25) button.callback = button_callback button2.callback = button2_callback await msg.channel.send(file=file, embed=embed, view=view, delete_after=5) bot.run(TOKEN)
Why does this happen?
Advertisement
Answer
With discord API you need to send an initial response within 3 seconds and afterward, you have 15 minutes to send the follow-up message. You should look into deferring. You’re uploading an image that might take some time and you might need to defer the message.
Instead of doing :
Interaction.response.send_message()
Try :
Interaction.response.defer()
asyncio.sleep()
Interaction.followup.send()