Skip to content

Commit

Permalink
Fix menu inter handling
Browse files Browse the repository at this point in the history
  • Loading branch information
hypergonial committed Dec 11, 2023
1 parent 43d3416 commit 821d784
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 11 deletions.
2 changes: 1 addition & 1 deletion miru/abc/item_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def clear(self) -> None:
self._weights = [0, 0, 0, 0, 0]


# Type ignore: Python 3.8 doesn't support type-arg in abc.ABC
# TODO Type ignore: Python 3.8 doesn't support type-arg in abc.ABC, add when 3.9 is released
class ItemHandler(Sequence, abc.ABC, t.Generic[BuilderT, ContextT, ItemT]): # type: ignore[type-arg]
"""Abstract base class all item-handlers (e.g. views, modals) inherit from.
Expand Down
1 change: 0 additions & 1 deletion miru/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ async def _handle_events(self, event: hikari.InteractionCreateEvent) -> None:
await handler._process_interactions(event)
return

# God why does mypy hate me so much for naming two variables the same in two if statement arms >_<
if isinstance(event.interaction, hikari.ComponentInteraction):
comp_ctx = RawComponentContext(event.interaction)
self._app.event_manager.dispatch(ComponentInteractionCreateEvent(self._app, event.interaction, comp_ctx))
Expand Down
18 changes: 11 additions & 7 deletions miru/ext/menu/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@

__all__ = ("Menu",)

ViewContextT = t.TypeVar("ViewContextT", bound="miru.ViewContext")


class Menu(miru.View):
"""A menu that can be used to display multiple nested screens of components."""

def __init__(self, *, timeout: float = 300.0):
super().__init__(timeout=timeout)
def __init__(self, *, timeout: float = 300.0, autodefer: bool = True):
super().__init__(timeout=timeout, autodefer=autodefer)
self._stack: t.List[Screen] = []
# The last interaction received, used for inter-based handling
# The interaction that was used to send the menu, if any.
self._inter: t.Optional[hikari.MessageResponseMixin[t.Any]] = None
self._ephemeral: bool = False
self._payload: t.Dict[str, t.Any] = {}
Expand Down Expand Up @@ -47,7 +49,9 @@ async def _edit_message(self) -> None:
if self.message is None:
return

if self._inter is not None:
if self.last_context is not None:
await self.last_context.edit_response(components=self, **self._payload)
elif self._inter is not None:
await self._inter.edit_message(self.message, components=self, **self._payload)
else:
await self.message.edit(components=self, **self._payload)
Expand Down Expand Up @@ -152,17 +156,17 @@ async def send(

if isinstance(to, (int, hikari.TextableChannel)):
channel = hikari.Snowflake(to)
message = await self.app.rest.create_message(channel, **self._payload)
message = await self.app.rest.create_message(channel, components=self, **self._payload)
elif isinstance(to, miru.Context):
self._inter = to.interaction
resp = await to.respond(**self._payload)
message = await resp.retrieve_message()
else:
self._inter = to
if not responded:
await to.create_initial_response(hikari.ResponseType.MESSAGE_CREATE, **self._payload)
await to.create_initial_response(hikari.ResponseType.MESSAGE_CREATE, components=self, **self._payload)
message = await to.fetch_initial_response()
else:
message = await to.execute(**self._payload)
message = await to.execute(components=self, **self._payload)

await self.start(message)
1 change: 0 additions & 1 deletion miru/text_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ def __init__(
row: t.Optional[int] = None,
) -> None:
super().__init__(custom_id=custom_id, row=row, position=0, width=5, required=required)
# Set value directly to avoid validation with missing values
self._value: t.Optional[str] = str(value) if value else None
self.style = style
self.placeholder = placeholder
Expand Down
2 changes: 1 addition & 1 deletion miru/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def __init__(
self._message_id: t.Optional[hikari.Snowflake] = None
self._input_event: asyncio.Event = asyncio.Event()

for decorated_item in self._view_children: # Sort and instantiate decorated callbacks
for decorated_item in self._view_children:
# Must deepcopy, otherwise multiple views will have the same item reference
decorated_item = copy.deepcopy(decorated_item)
item = decorated_item.build(self)
Expand Down

0 comments on commit 821d784

Please sign in to comment.