Skip to content

Commit

Permalink
Implement modals (#1002)
Browse files Browse the repository at this point in the history
Co-authored-by: davfsa <[email protected]>
Co-authored-by: HyperGH <[email protected]>
  • Loading branch information
3 people authored Dec 4, 2022
1 parent 507bf41 commit b638e0c
Show file tree
Hide file tree
Showing 30 changed files with 2,203 additions and 716 deletions.
1 change: 1 addition & 0 deletions changes/1002.deprecation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Deprecate `RESTClientImpl.build_action_row` in favour of `RESTClientImpl.build_message_action_row`.
2 changes: 2 additions & 0 deletions changes/1002.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Implement modal interactions.
- Additionally, it is now guaranteed (typing-wise) that top level components will be an action row
2 changes: 2 additions & 0 deletions hikari/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
from hikari.colors import *
from hikari.colours import *
from hikari.commands import *
from hikari.components import *
from hikari.embeds import *
from hikari.emojis import *
from hikari.errors import *
Expand Down Expand Up @@ -105,6 +106,7 @@
from hikari.interactions.base_interactions import *
from hikari.interactions.command_interactions import *
from hikari.interactions.component_interactions import *
from hikari.interactions.modal_interactions import *
from hikari.invites import *
from hikari.iterators import *
from hikari.locales import *
Expand Down
2 changes: 2 additions & 0 deletions hikari/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ from hikari.channels import *
from hikari.colors import *
from hikari.colours import *
from hikari.commands import *
from hikari.components import *
from hikari.embeds import *
from hikari.emojis import *
from hikari.errors import *
Expand Down Expand Up @@ -78,6 +79,7 @@ from hikari.intents import *
from hikari.interactions.base_interactions import *
from hikari.interactions.command_interactions import *
from hikari.interactions.component_interactions import *
from hikari.interactions.modal_interactions import *
from hikari.invites import *
from hikari.iterators import *
from hikari.locales import *
Expand Down
16 changes: 16 additions & 0 deletions hikari/api/entity_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
from hikari.interactions import base_interactions
from hikari.interactions import command_interactions
from hikari.interactions import component_interactions
from hikari.interactions import modal_interactions
from hikari.internal import data_binding


Expand Down Expand Up @@ -1298,6 +1299,21 @@ def deserialize_command_interaction(
The deserialized command interaction object.
"""

@abc.abstractmethod
def deserialize_modal_interaction(self, payload: data_binding.JSONObject) -> modal_interactions.ModalInteraction:
"""Parse a raw payload from Discord into a modal interaction object.
Parameters
----------
payload : hikari.internal.data_binding.JSONObject
The JSON payload to deserialize.
Returns
-------
hikari.interactions.modal_interactions.ModalInteraction
The deserialized modal interaction object.
"""

@abc.abstractmethod
def deserialize_interaction(self, payload: data_binding.JSONObject) -> base_interactions.PartialInteraction:
"""Parse a raw payload from Discord into a interaction object.
Expand Down
34 changes: 30 additions & 4 deletions hikari/api/interaction_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,18 @@
from hikari.interactions import base_interactions
from hikari.interactions import command_interactions
from hikari.interactions import component_interactions
from hikari.interactions import modal_interactions

_InteractionT_co = typing.TypeVar("_InteractionT_co", bound=base_interactions.PartialInteraction, covariant=True)
_ResponseT_co = typing.TypeVar("_ResponseT_co", bound=special_endpoints.InteractionResponseBuilder, covariant=True)
_MessageResponseBuilderT = typing.Union[
special_endpoints.InteractionDeferredBuilder,
special_endpoints.InteractionMessageBuilder,
]
_ModalOrMessageResponseBuilder = typing.Union[
_MessageResponseBuilderT,
special_endpoints.InteractionModalBuilder,
]


ListenerT = typing.Callable[["_InteractionT_co"], typing.Awaitable["_ResponseT_co"]]
Expand Down Expand Up @@ -142,14 +147,14 @@ async def on_interaction(self, body: bytes, signature: bytes, timestamp: bytes)
@abc.abstractmethod
def get_listener(
self, interaction_type: typing.Type[command_interactions.CommandInteraction], /
) -> typing.Optional[ListenerT[command_interactions.CommandInteraction, _MessageResponseBuilderT]]:
) -> typing.Optional[ListenerT[command_interactions.CommandInteraction, _ModalOrMessageResponseBuilder]]:
...

@typing.overload
@abc.abstractmethod
def get_listener(
self, interaction_type: typing.Type[component_interactions.ComponentInteraction], /
) -> typing.Optional[ListenerT[component_interactions.ComponentInteraction, _MessageResponseBuilderT]]:
) -> typing.Optional[ListenerT[component_interactions.ComponentInteraction, _ModalOrMessageResponseBuilder]]:
...

@typing.overload
Expand All @@ -161,6 +166,13 @@ def get_listener(
]:
...

@typing.overload
@abc.abstractmethod
def get_listener(
self, interaction_type: typing.Type[modal_interactions.ModalInteraction], /
) -> typing.Optional[ListenerT[modal_interactions.ModalInteraction, _MessageResponseBuilderT]]:
...

@typing.overload
@abc.abstractmethod
def get_listener(
Expand Down Expand Up @@ -191,7 +203,7 @@ def get_listener(
def set_listener(
self,
interaction_type: typing.Type[command_interactions.CommandInteraction],
listener: typing.Optional[ListenerT[command_interactions.CommandInteraction, _MessageResponseBuilderT]],
listener: typing.Optional[ListenerT[command_interactions.CommandInteraction, _ModalOrMessageResponseBuilder]],
/,
*,
replace: bool = False,
Expand All @@ -203,7 +215,9 @@ def set_listener(
def set_listener(
self,
interaction_type: typing.Type[component_interactions.ComponentInteraction],
listener: typing.Optional[ListenerT[component_interactions.ComponentInteraction, _MessageResponseBuilderT]],
listener: typing.Optional[
ListenerT[component_interactions.ComponentInteraction, _ModalOrMessageResponseBuilder]
],
/,
*,
replace: bool = False,
Expand All @@ -224,6 +238,18 @@ def set_listener(
) -> None:
...

@typing.overload
@abc.abstractmethod
def set_listener(
self,
interaction_type: typing.Type[modal_interactions.ModalInteraction],
listener: typing.Optional[ListenerT[modal_interactions.ModalInteraction, _MessageResponseBuilderT]],
/,
*,
replace: bool = False,
) -> None:
...

@abc.abstractmethod
def set_listener(
self,
Expand Down
69 changes: 66 additions & 3 deletions hikari/api/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7977,6 +7977,23 @@ def interaction_message_builder(
The interaction message response builder object.
"""

@abc.abstractmethod
def interaction_modal_builder(self, title: str, custom_id: str) -> special_endpoints.InteractionModalBuilder:
"""Create a builder for a modal interaction response.
Parameters
----------
title : builtins.str
The title that will show up in the modal.
custom_id : builtins.str
Developer set custom ID used for identifying interactions with this modal.
Returns
-------
hikari.api.special_endpoints.InteractionModalBuilder
The interaction modal response builder object.
"""

@abc.abstractmethod
async def fetch_interaction_response(
self, application: snowflakes.SnowflakeishOr[guilds.PartialApplication], token: str
Expand Down Expand Up @@ -8378,13 +8395,59 @@ async def create_autocomplete_response(
If an internal error occurs on Discord while handling the request.
"""

async def create_modal_response(
self,
interaction: snowflakes.SnowflakeishOr[base_interactions.PartialInteraction],
token: str,
*,
title: str,
custom_id: str,
component: undefined.UndefinedOr[special_endpoints.ComponentBuilder] = undefined.UNDEFINED,
components: undefined.UndefinedOr[typing.Sequence[special_endpoints.ComponentBuilder]] = undefined.UNDEFINED,
) -> None:
"""Create a response by sending a modal.
Parameters
----------
interaction : hikari.snowflakes.SnowflakeishOr[hikari.interactions.base_interactions.PartialInteraction]
Object or ID of the interaction this response is for.
token : builtins.str
The command interaction's token.
title : str
The title that will show up in the modal.
custom_id : str
Developer set custom ID used for identifying interactions with this modal.
Other Parameters
----------------
component : hikari.undefined.UndefinedOr[typing.Sequence[special_endpoints.ComponentBuilder]]
A component builders to send in this modal.
components : hikari.undefined.UndefinedOr[typing.Sequence[special_endpoints.ComponentBuilder]]
A sequence of component builders to send in this modal.
Raises
------
ValueError
If both `component` and `components` are specified or if none are specified.
"""

@abc.abstractmethod
def build_message_action_row(self) -> special_endpoints.MessageActionRowBuilder:
"""Build a message action row message component for use in message create and REST calls.
Returns
-------
hikari.api.special_endpoints.MessageActionRowBuilder
The initialised action row builder.
"""

@abc.abstractmethod
def build_action_row(self) -> special_endpoints.ActionRowBuilder:
"""Build an action row message component for use in message create and REST calls.
def build_modal_action_row(self) -> special_endpoints.ModalActionRowBuilder:
"""Build an action row modal component for use in interactions and REST calls.
Returns
-------
hikari.api.special_endpoints.ActionRowBuilder
hikari.api.special_endpoints.ModalActionRowBuilder
The initialised action row builder.
"""

Expand Down
Loading

0 comments on commit b638e0c

Please sign in to comment.