Skip to content

Commit

Permalink
Improve missing response handling
Browse files Browse the repository at this point in the history
  • Loading branch information
hypergonial committed Dec 29, 2023
1 parent 3085f10 commit 63c33b3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
25 changes: 20 additions & 5 deletions arc/client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import abc
import asyncio
import functools
import importlib
import inspect
Expand All @@ -16,7 +17,7 @@

from .command import MessageCommand, SlashCommand, SlashGroup, SlashSubCommand, SlashSubGroup, UserCommand
from .context import AutodeferMode, Context
from .errors import ExtensionLoadError, ExtensionUnloadError
from .errors import ExtensionLoadError, ExtensionUnloadError, NoResponseIssuedError
from .events import CommandErrorEvent
from .internal.sync import _sync_commands
from .internal.types import AppT, BuilderT, EventCallbackT, EventT, ResponseBuilderT
Expand Down Expand Up @@ -257,8 +258,16 @@ async def on_command_interaction(self, interaction: hikari.CommandInteraction) -

fut = await command.invoke(interaction)

if fut is not None:
return await fut
if fut is None:
return

try:
return await asyncio.wait_for(fut, timeout=3.0)
except asyncio.TimeoutError:
logger.warning(
f"Timed out waiting for response from command: '{interaction.command_name} ({interaction.command_type})'"
f" Did you forget to respond?"
)

async def on_autocomplete_interaction(
self, interaction: hikari.AutocompleteInteraction
Expand Down Expand Up @@ -869,14 +878,20 @@ async def on_restbot_shutdown(self, bot: hikari.RESTBotAware) -> None:

async def _on_restbot_interaction_create(self, interaction: hikari.CommandInteraction) -> ResponseBuilderT:
builder = await self.on_command_interaction(interaction)
assert builder is not None
if builder is None:
raise NoResponseIssuedError(
f"No response was issued to interaction for command: {interaction.command_name} ({interaction.command_type})."
)
return builder

async def _on_restbot_autocomplete_interaction_create(
self, interaction: hikari.AutocompleteInteraction
) -> hikari.api.InteractionAutocompleteBuilder:
builder = await self.on_autocomplete_interaction(interaction)
assert builder is not None
if builder is None:
raise NoResponseIssuedError(
f"No response was issued to autocomplete request for command: {interaction.command_name} ({interaction.command_type})."
)
return builder


Expand Down
18 changes: 17 additions & 1 deletion arc/errors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
__all__ = ("ArcError", "AutocompleteError", "CommandInvokeError")
__all__ = (
"ArcError",
"AutocompleteError",
"CommandInvokeError",
"ExtensionError",
"ExtensionLoadError",
"ExtensionUnloadError",
"NoResponseIssuedError",
)


class ArcError(Exception):
Expand All @@ -25,6 +33,14 @@ class ExtensionUnloadError(ExtensionError):
"""An error occurred while trying to unload an extension."""


class NoResponseIssuedError(ArcError):
"""Raised when no response was issued by a command.
Interactions must be responded to or deferred within 3 seconds to avoid this error.
`arc` tries to automatically defer responses when possible, so this error should rarely occur, unless autodefer is disabled.
"""


# MIT License
#
# Copyright (c) 2023-present hypergonial
Expand Down
4 changes: 4 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ hide:

Here you can find all the changelogs for `hikari-arc`.

## v0.1.4

- Improve handling missing responses via REST by adding `NoResponseIssuedError`.

## v0.1.3

- Fix `Context.respond_with_builder` issuing the response twice in REST.
Expand Down

0 comments on commit 63c33b3

Please sign in to comment.