pycord icon indicating copy to clipboard operation
pycord copied to clipboard

Auto deferring raises 404

Open Luc1412 opened this issue 5 years ago • 10 comments

Summary

The implemented defer in the View._scheduled_task sometimes raises 404

Reproduction Steps

  • Wait in an item callback for more than 3 seconds

Minimal Reproducible Code

class TestView(discord.ui.View):

    def __init__(self):
        super().__init__()
        help_select = discord.ui.Select(options=[
            discord.SelectOption(label='Home', value='home', emoji='🏠', default=True),
            discord.SelectOption(label='Name', value='stats', emoji='📊'),
        ])
        help_select.callback = self.select_page
        self.add_item(help_select)

    async def select_page(self, interaction: discord.Interaction):
        await asyncio.sleep(4)

Expected Results

I think no exception should be raised because you can't handle it properly and it just spams your error log.

Actual Results

The exception should be catched

Traceback (most recent call last):
  File "\venv\lib\site-packages\discord\ui\view.py", line 361, in _scheduled_task
    await interaction.response.defer()
  File "\venv\lib\site-packages\discord\interactions.py", line 427, in defer
    await adapter.create_interaction_response(
  File "\venv\lib\site-packages\discord\webhook\async_.py", line 189, in request
    raise NotFound(response, data)
discord.errors.NotFound: 404 Not Found (error code: 10062): Unknown interaction

Intents

guilds, members, emojis, webhooks, voice_states, guild_messages, guild_reactions, dm_messages, dm_reactions

System Information

  • Python v3.9.5-final
  • pycord v2.0.0-alpha
    • pycord pkg_resources: v2.0.0a3783+gaf4d6caf
  • aiohttp v3.7.4.post0
  • system info: Windows 10 10.0.22000

Checklist

  • [X] I have searched the open issues for duplicates.
  • [X] I have shown the entire traceback, if possible.
  • [X] I have removed my token from display, if visible.

Additional Context

No response

Luc1412 avatar Sep 06 '21 06:09 Luc1412

You have to send an initial response to an interaction within 3 seconds: image

However, if you manually defer the interaction e.g. doing

    async def select_page(self, interaction: discord.Interaction):
        await interaction.response.defer()
        await asyncio.sleep(4)

then you have up to 15 minutes (since that is how long the interaction token is valid for) to send follow-up messages.

The reason that you are getting this error is because pycord automatically defers the response if you do not respond yourself, and since you slept for more than 3 seconds in the component callback, the interaction token is invalidated.

esmaycat avatar Sep 06 '21 08:09 esmaycat

Yeah, am aware of the reason. But I think that the library should catch the 404. Depending on the load of the server the bot is not able to respond in the given time which results in this error.

Luc1412 avatar Sep 06 '21 08:09 Luc1412

You can catch it yourself? discord.ui.View provides a on_error function for this image

esmaycat avatar Sep 06 '21 08:09 esmaycat

I think the library shouldn't throw errors without the developer really expecting them. You also can also barely differentiate those library exceptions from those who get thrown in the callback.

Luc1412 avatar Sep 06 '21 09:09 Luc1412

It should be expected because you have exceeded the 3 second limit. Continuing from your point, neither should the library silently ignore errors.

esmaycat avatar Sep 06 '21 09:09 esmaycat

It should be expected

No. It is not mentioned anywhere in the documentation. "Not Found" also doesn't make sense in this context and won't be useful to the library user. Exceptions are only expected on library user input eg. by calling a core.

Continuing from your point, neither should the library silently ignore errors.

But not everyone wants that the interaction to be auto-deferred

Luc1412 avatar Sep 06 '21 09:09 Luc1412

It is documented in the discord API documentation. The reason that it gets auto-deferred is because you have to respond to the interaction and the defer happens only when the callback is done and no response was made.

It probably should be documented with more clarity in the fact that you have to respond within 3 seconds and that the library auto-defers it for you if you don't respond youself.

esmaycat avatar Sep 06 '21 09:09 esmaycat

It is documented in the discord API documentation.

No. The user doesn't know that it will be auto-deferred. The average user probably also only uses pycord's documentation.

The reason that it gets auto-deferred is because you have to respond to the interaction and the defer happens only when the callback is done and no response was made.

You don't need to respond. If you don't respond, it just shows that the interaction failed.

Luc1412 avatar Sep 06 '21 09:09 Luc1412

The documentation that I referenced documents the fact that you need to respond within 3 seconds if you don't want it to fail.

I suppose the point of having it was that it is better than the "This interaction has failed" text that appears if you don't respond at all. I already said that it should probably be documented that it will auto-defer.

Of course you could have a task that waits for 3 seconds and then checks if the interaction has been responded to or not but this could be prone to race conditions. In my opinion, it is better to keep what we have at the moment but document it better.

esmaycat avatar Sep 06 '21 09:09 esmaycat

Stale

Lulalaby avatar Dec 16 '21 05:12 Lulalaby