Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more specialized interaction events #2023

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changes/2023.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add more specialized interaction events (`ComponentInteractionCreateEvent`, `CommandInteractionCreateEvent`,
`ModalInteractionCreateEvent`, `AutocompleteInteractionCreateEvent`)
67 changes: 66 additions & 1 deletion hikari/events/interaction_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@

from __future__ import annotations

__all__: typing.Sequence[str] = ("InteractionCreateEvent",)
__all__: typing.Sequence[str] = (
"InteractionCreateEvent",
"ComponentInteractionCreateEvent",
"ModalInteractionCreateEvent",
"CommandInteractionCreateEvent",
"AutocompleteInteractionCreateEvent",
)

import typing

Expand All @@ -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
Expand All @@ -53,3 +62,59 @@ class InteractionCreateEvent(shard_events.ShardEvent):
def app(self) -> traits.RESTAware:
# <<inherited docstring from Event>>.
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:
# <<inherited docstring from Event>>.
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:
# <<inherited docstring from Event>>.
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:
# <<inherited docstring from Event>>.
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:
# <<inherited docstring from Event>>.
return self.interaction.app
19 changes: 16 additions & 3 deletions hikari/impl/event_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 #
Expand Down
9 changes: 8 additions & 1 deletion hikari/impl/event_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Loading