|
| 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