#InteractionTimedOut Error

1 messages · Page 1 of 1 (latest)

lofty cloak
#

Hello! Im making a discord bot with apis, bot when i do a command, it gives me this error:

lease defer it using "interaction.response.defer" on the start of your command. Later you may send a response by editing the deferred message using "interac
tion.edit_original_response"
Note: This might also be caused by a misconfiguration in the components make sure you do not respond twice in case this is a component.```

How do i fix this error?
rose lion
#

Message states exactly what the problem is and how to fix it..

#

Interactions require a response within 3 seconds. If the response will take longer, you must defer() the response, then you can send your response up to 15 minutes later with await inter.edit_original_response(...)

#

Also, make sure you're NOT using requests for API calls. It's blocking and long blocking calls can and will cause issues with your bot

earnest crow
#

aiohttp is your friend

#

disnake uses aiohttp internally, too, so you should just be able to import it

lofty cloak
# earnest crow aiohttp is your friend
@bot.slash_command(description="Get a random dog picture")
async def dog(inter):
    async with aiohttp.ClientSession() as session:
        request = await session.get('https://some-random-api.ml/animal/dog')
        dog = await request.json()
        embed = disnake.Embed(title="Random Dog Picture",description=dog['fact'])
        embed.set_image(url=dog['image'])
        await inter.send(embed=embed)```

this is my code, but what's wrong about it?
rose lion
#

If you're already using aiohttp, then no issue. I was just making sure you weren't using requests library

lofty cloak
#

yeah, but why isnt working?

lofty cloak
#

I'm really sorry, but I don't know how to change my code to make it work

earnest crow
#

as per the error you got:

Please defer it using "interaction.response.defer" on the start of your command

# NOTE: this still won't work!

@bot.slash_command(description="Get a random dog picture")
async def dog(inter):
    # Defer the response at the start of the command as suggested:
    await inter.response.defer()
    # We now have 15 minutes to respond instead of 3 seconds.

    async with aiohttp.ClientSession() as session:
        request = await session.get('https://some-random-api.ml/animal/dog')
        dog = await request.json()
        embed = disnake.Embed(title="Random Dog Picture",description=dog['fact'])
        embed.set_image(url=dog['image'])
        await inter.send(embed=embed)

Later you may send a response by editing the deferred message using "interaction.edit_original_response"
This is because you can only respond (inter.response.<...>) once to an interaction. inter.response.defer() creates a temporary "<bot> is thinking..." message, which we can then edit into whatever you want it to be:

# Final result:

@bot.slash_command(description="Get a random dog picture")
async def dog(inter):
    await inter.response.defer()

    async with aiohttp.ClientSession() as session:
        request = await session.get('https://some-random-api.ml/animal/dog')
        dog = await request.json()
        embed = disnake.Embed(title="Random Dog Picture",description=dog['fact'])
        embed.set_image(url=dog['image'])

        # Use edit_original_message so as to not respond twice:
        await inter.edit_original_message(embed=embed)
        # NOTE: inter.send actually works here because of some
        #       internal shenanigans, but I personally prefer
        #       the explicitness of using edit_original_message.
#

and as far as the request goes, the "proper" way would be:

async with aiohttp.ClientSession() as session:
    async with session.get('https://some-random-api.ml/animal/dog') as response:
        dog = await response.json()

    ...  # use data now stored in dog
```though in this specific situation it doesn't make too much of a difference
lofty cloak
#

Ahh, thank you very much :D