From e510e1aa5fb1308efd78213a6015904cd021879b Mon Sep 17 00:00:00 2001 From: syncblaze Date: Sun, 18 Aug 2024 19:11:09 +0200 Subject: [PATCH 1/2] add more specialized interaction events --- hikari/events/interaction_events.py | 67 ++++++++++++++++++++++++++++- hikari/impl/event_factory.py | 19 ++++++-- hikari/impl/event_manager.py | 9 +++- 3 files changed, 90 insertions(+), 5 deletions(-) diff --git a/hikari/events/interaction_events.py b/hikari/events/interaction_events.py index 5f16e1e47..5ff42debf 100644 --- a/hikari/events/interaction_events.py +++ b/hikari/events/interaction_events.py @@ -23,7 +23,13 @@ from __future__ import annotations -__all__: typing.Sequence[str] = ("InteractionCreateEvent",) +__all__: typing.Sequence[str] = ( + "InteractionCreateEvent", + "ComponentInteractionCreateEvent", + "ModalInteractionCreateEvent", + "CommandInteractionCreateEvent", + "AutocompleteInteractionCreateEvent", +) import typing @@ -36,6 +42,9 @@ from hikari import traits from hikari.api import shard as gateway_shard from hikari.interactions import base_interactions + from hikari.interactions import command_interactions + from hikari.interactions import component_interactions + from hikari.interactions import modal_interactions @attrs_extensions.with_copy @@ -53,3 +62,59 @@ class InteractionCreateEvent(shard_events.ShardEvent): def app(self) -> traits.RESTAware: # <>. return self.interaction.app + + +@attrs_extensions.with_copy +@attrs.define(kw_only=True, weakref_slot=False) +class ComponentInteractionCreateEvent(InteractionCreateEvent): + """Event fired when a component interaction is created.""" + + interaction: component_interactions.ComponentInteraction = attrs.field(repr=True) + """Interaction that this event is related to.""" + + @property + def app(self) -> traits.RESTAware: + # <>. + return self.interaction.app + + +@attrs_extensions.with_copy +@attrs.define(kw_only=True, weakref_slot=False) +class ModalInteractionCreateEvent(InteractionCreateEvent): + """Event fired when a modal interaction is created.""" + + interaction: modal_interactions.ModalInteraction = attrs.field(repr=True) + """Interaction that this event is related to.""" + + @property + def app(self) -> traits.RESTAware: + # <>. + return self.interaction.app + + +@attrs_extensions.with_copy +@attrs.define(kw_only=True, weakref_slot=False) +class CommandInteractionCreateEvent(InteractionCreateEvent): + """Event fired when a command interaction is created.""" + + interaction: command_interactions.CommandInteraction = attrs.field(repr=True) + """Interaction that this event is related to.""" + + @property + def app(self) -> traits.RESTAware: + # <>. + return self.interaction.app + + +@attrs_extensions.with_copy +@attrs.define(kw_only=True, weakref_slot=False) +class AutocompleteInteractionCreateEvent(InteractionCreateEvent): + """Event fired when an autocomplete interaction is created.""" + + interaction: command_interactions.AutocompleteInteraction = attrs.field(repr=True) + """Interaction that this event is related to.""" + + @property + def app(self) -> traits.RESTAware: + # <>. + return self.interaction.app diff --git a/hikari/impl/event_factory.py b/hikari/impl/event_factory.py index 515015d07..98dd18b7a 100644 --- a/hikari/impl/event_factory.py +++ b/hikari/impl/event_factory.py @@ -33,6 +33,7 @@ from hikari import channels as channel_models from hikari import colors from hikari import emojis as emojis_models +from hikari import interactions as interaction_models from hikari import snowflakes from hikari import undefined from hikari import users as user_models @@ -493,9 +494,21 @@ def deserialize_audit_log_entry_create_event( def deserialize_interaction_create_event( self, shard: gateway_shard.GatewayShard, payload: data_binding.JSONObject ) -> interaction_events.InteractionCreateEvent: - return interaction_events.InteractionCreateEvent( - shard=shard, interaction=self._app.entity_factory.deserialize_interaction(payload) - ) + interaction = self._app.entity_factory.deserialize_interaction(payload) + if interaction.type == interaction_models.InteractionType.MESSAGE_COMPONENT: + assert isinstance(interaction, interaction_models.ComponentInteraction), "Unexpected interaction type" + return interaction_events.ComponentInteractionCreateEvent(shard=shard, interaction=interaction) + elif interaction.type == interaction_models.InteractionType.APPLICATION_COMMAND: + assert isinstance(interaction, interaction_models.CommandInteraction), "Unexpected interaction type" + return interaction_events.CommandInteractionCreateEvent(shard=shard, interaction=interaction) + elif interaction.type == interaction_models.InteractionType.AUTOCOMPLETE: + assert isinstance(interaction, interaction_models.AutocompleteInteraction), "Unexpected interaction type" + return interaction_events.AutocompleteInteractionCreateEvent(shard=shard, interaction=interaction) + elif interaction.type == interaction_models.InteractionType.MODAL_SUBMIT: + assert isinstance(interaction, interaction_models.ModalInteraction), "Unexpected interaction type" + return interaction_events.ModalInteractionCreateEvent(shard=shard, interaction=interaction) + + return interaction_events.InteractionCreateEvent(shard=shard, interaction=interaction) ################# # MEMBER EVENTS # diff --git a/hikari/impl/event_manager.py b/hikari/impl/event_manager.py index b9fed4e30..1d9cdb19d 100644 --- a/hikari/impl/event_manager.py +++ b/hikari/impl/event_manager.py @@ -827,7 +827,14 @@ async def on_webhooks_update(self, shard: gateway_shard.GatewayShard, payload: d """See https://discord.com/developers/docs/topics/gateway-events#webhooks-update for more info.""" await self.dispatch(self._event_factory.deserialize_webhook_update_event(shard, payload)) - @event_manager_base.filtered(interaction_events.InteractionCreateEvent) + @event_manager_base.filtered( + ( + interaction_events.ComponentInteractionCreateEvent, + interaction_events.ModalInteractionCreateEvent, + interaction_events.CommandInteractionCreateEvent, + interaction_events.AutocompleteInteractionCreateEvent, + ) + ) async def on_interaction_create(self, shard: gateway_shard.GatewayShard, payload: data_binding.JSONObject) -> None: """See https://discord.com/developers/docs/topics/gateway-events#interaction-create for more info.""" await self.dispatch(self._event_factory.deserialize_interaction_create_event(shard, payload)) From 12aa2159e3d0e0f8018ab97e8d309096658da9c7 Mon Sep 17 00:00:00 2001 From: syncblaze Date: Sun, 18 Aug 2024 19:16:42 +0200 Subject: [PATCH 2/2] Add fragment --- changes/2023.feature.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 changes/2023.feature.md diff --git a/changes/2023.feature.md b/changes/2023.feature.md new file mode 100644 index 000000000..2d2d89f9e --- /dev/null +++ b/changes/2023.feature.md @@ -0,0 +1,2 @@ +Add more specialized interaction events (`ComponentInteractionCreateEvent`, `CommandInteractionCreateEvent`, +`ModalInteractionCreateEvent`, `AutocompleteInteractionCreateEvent`)