Skip to content

Commit a824153

Browse files
committed
feat: InteractionCreate
1 parent d0c2eb5 commit a824153

File tree

3 files changed

+82
-18
lines changed

3 files changed

+82
-18
lines changed

discord/app/cache.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,10 @@ async def delete_view_on(self, message_id: int) -> None:
8383
async def get_all_views(self) -> list[View]:
8484
...
8585

86-
async def store_modal(self, modal: Modal) -> None:
86+
async def store_modal(self, modal: Modal, user_id: int) -> None:
87+
...
88+
89+
async def delete_modal(self, custom_id: str) -> None:
8790
...
8891

8992
async def get_all_modals(self) -> list[Modal]:
@@ -348,3 +351,6 @@ async def get_message(self, message_id: int) -> Message | None:
348351

349352
async def get_all_messages(self) -> list[Message]:
350353
return list(self._messages)
354+
355+
async def delete_modal(self, custom_id: str) -> None:
356+
self._modals.pop(custom_id, None)

discord/app/state.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -524,23 +524,6 @@ async def query_members(
524524
)
525525
raise
526526

527-
def parse_interaction_create(self, data) -> None:
528-
interaction = Interaction(data=data, state=self)
529-
if data["type"] == 3: # interaction component
530-
custom_id = interaction.data["custom_id"] # type: ignore
531-
component_type = interaction.data["component_type"] # type: ignore
532-
self._view_store.dispatch(component_type, custom_id, interaction)
533-
if interaction.type == InteractionType.modal_submit:
534-
user_id, custom_id = (
535-
interaction.user.id,
536-
interaction.data["custom_id"],
537-
)
538-
asyncio.create_task(
539-
self._modal_store.dispatch(user_id, custom_id, interaction)
540-
)
541-
542-
self.dispatch("interaction", interaction)
543-
544527
def parse_presence_update(self, data) -> None:
545528
guild_id = utils._get_as_snowflake(data, "guild_id")
546529
# guild_id won't be None here

discord/events/interaction.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
"""
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2021-present Pycord Development
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a
7+
copy of this software and associated documentation files (the "Software"),
8+
to deal in the Software without restriction, including without limitation
9+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
10+
and/or sell copies of the Software, and to permit persons to whom the
11+
Software is furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17+
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22+
DEALINGS IN THE SOFTWARE.
23+
"""
24+
25+
from typing import Any, Self
26+
27+
from discord.enums import InteractionType
28+
from discord.types.interactions import Interaction as InteractionPayload
29+
from ..app.state import ConnectionState
30+
from ..interactions import Interaction
31+
from ..app.event_emitter import Event
32+
33+
34+
class InteractionCreate(Event, Interaction):
35+
__event_name__ = "INTERACTION_CREATE"
36+
37+
def __init__(self) -> None:
38+
pass
39+
40+
@classmethod
41+
async def __load__(cls, data: Any, state: ConnectionState) -> Self | None:
42+
interaction = Interaction(data=data, state=state)
43+
if data["type"] == 3:
44+
custom_id = interaction.data["custom_id"] # type: ignore
45+
component_type = interaction.data["component_type"] # type: ignore
46+
views = await state.cache.get_all_views()
47+
for view in views:
48+
if view.id == custom_id:
49+
for item in view.children:
50+
if item.type == component_type:
51+
item.refresh_state(interaction)
52+
view._dispatch_item(item, interaction)
53+
if interaction.type == InteractionType.modal_submit:
54+
custom_id = interaction.data["custom_id"]
55+
for modal in await state.cache.get_all_modals():
56+
if modal.custom_id != custom_id:
57+
continue
58+
try:
59+
components = [
60+
component
61+
for parent_component in interaction.data["components"]
62+
for component in parent_component["components"]
63+
]
64+
for component in components:
65+
for child in modal.children:
66+
if child.custom_id == component["custom_id"]: # type: ignore
67+
child.refresh_state(component)
68+
break
69+
await modal.callback(interaction)
70+
await state.cache.delete_modal(modal.custom_id)
71+
except Exception as e:
72+
return await modal.on_error(e, interaction)
73+
self = cls()
74+
self.__dict__.update(interaction.__dict__)
75+
return self

0 commit comments

Comments
 (0)