Skip to content
Open
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: 1 addition & 1 deletion discord/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1688,7 +1688,7 @@ async def send(
if view.is_dispatchable():
state.store_view(view, ret.id)
view.message = ret
view.refresh(ret.components)
view._refresh(ret.components)

if delete_after is not None:
await ret.delete(delay=delete_after)
Expand Down
9 changes: 5 additions & 4 deletions discord/interactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,8 +597,9 @@ async def edit_original_response(
previous_allowed_mentions=previous_mentions,
suppress=suppress,
)
if view and self.message:
self._state.prevent_view_updates_for(self.message.id)
_message = self.message or self._original_response
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe call it something different, like target, would be less confusing.

if view and _message:
self._state.prevent_view_updates_for(_message.id)
adapter = async_context.get()
http = self._state.http
data = await adapter.edit_original_interaction_response(
Expand All @@ -617,7 +618,7 @@ async def edit_original_response(
message = InteractionMessage(state=state, channel=self.channel, data=data) # type: ignore
if view:
if not view.is_finished():
view.refresh(message.components)
view._refresh(message.components)
if view.is_dispatchable():
self._state.store_view(view, message.id)

Expand Down Expand Up @@ -1191,7 +1192,7 @@ async def edit_message(
raise InteractionResponded(self._parent)

parent = self._parent
msg = parent.message
msg = parent.message or parent._original_response
state = parent._state
message_id = msg.id if msg else None
if parent.type not in (InteractionType.component, InteractionType.modal_submit):
Expand Down
4 changes: 2 additions & 2 deletions discord/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -1882,7 +1882,7 @@ async def edit(

if view and not view.is_finished():
view.message = message
view.refresh(message.components)
view._refresh(message.components)
if view.is_dispatchable():
self._state.store_view(view, self.id)

Expand Down Expand Up @@ -2551,7 +2551,7 @@ async def edit(self, **fields: Any) -> Message | None:
msg = self._state.create_message(channel=self.channel, data=data) # type: ignore
if view and not view.is_finished():
view.message = msg
view.refresh(msg.components)
view._refresh(msg.components)
if view.is_dispatchable():
self._state.store_view(view, self.id)
return msg
Expand Down
6 changes: 3 additions & 3 deletions discord/ui/modal.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ def remove_item(self, item: InputText) -> Self:
pass
return self

def refresh(self, interaction: Interaction, data: list[ComponentPayload]):
def _refresh(self, interaction: Interaction, data: list[ComponentPayload]):
components = [
component
for parent_component in data
Expand Down Expand Up @@ -444,7 +444,7 @@ def add_item(self, item: ModalItem) -> Self:
super().add_item(item)
return self

def refresh(self, interaction: Interaction, data: list[ComponentPayload]):
def _refresh(self, interaction: Interaction, data: list[ComponentPayload]):
for component, child in zip(data, self.children):
child.refresh_from_modal(interaction, component)

Expand Down Expand Up @@ -516,7 +516,7 @@ async def dispatch(self, user_id: int, custom_id: str, interaction: Interaction)

try:
components = interaction.data["components"]
modal.refresh(interaction, components)
modal._refresh(interaction, components)
await modal.callback(interaction)
self.remove_modal(modal, user_id)
except Exception as e:
Expand Down
14 changes: 11 additions & 3 deletions discord/ui/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from __future__ import annotations

import asyncio
import logging
import os
import sys
import time
Expand Down Expand Up @@ -75,6 +76,8 @@
from ..state import ConnectionState
from ..types.components import Component as ComponentPayload

_log = logging.getLogger(__name__)

V = TypeVar("V", bound="BaseView", covariant=True)


Expand Down Expand Up @@ -726,7 +729,7 @@ def clear_items(self) -> None:
self.__weights.clear()
return self

def refresh(self, components: list[Component]):
def _refresh(self, components: list[Component]):
# This is pretty hacky at the moment
old_state: dict[tuple[int, str], ViewItem[V]] = {
(item.type.value, item.custom_id): item for item in self.children if item.is_dispatchable() # type: ignore
Expand Down Expand Up @@ -895,7 +898,7 @@ def add_item(self, item: ViewItem[V]) -> Self:
super().add_item(item)
return self

def refresh(self, components: list[Component]):
def _refresh(self, components: list[Component]):
# Refreshes view data using discord's values
# Assumes the components and items are identical
if not components:
Expand Down Expand Up @@ -996,4 +999,9 @@ def update_from_message(self, message_id: int, components: list[ComponentPayload
# pre-req: is_message_tracked == true
view = self._synced_message_views[message_id]
components = [_component_factory(d, state=self._state) for d in components]
view.refresh(components)
try:
view._refresh(components)
except:
_log.warning(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure here whether we should prefer _log.warning or _log.exception - with the reasoning behind this being that this potentially removes from existence useful traceback info in case someone encounters this when they should not.

Alternatively we maybe could add a _log.debug with exc_info=True as well or something ? Idk just an idea.

f"Failed to refresh View {view} from Message {message_id} due to mismatched state. Items may not have complete data."
)
4 changes: 2 additions & 2 deletions discord/webhook/async_.py
Original file line number Diff line number Diff line change
Expand Up @@ -1902,7 +1902,7 @@ async def send(
if self.parent and not view.parent:
view.parent = self.parent
if msg:
view.refresh(msg.components)
view._refresh(msg.components)
if view.is_dispatchable():
self._state.store_view(view, message_id)

Expand Down Expand Up @@ -2113,7 +2113,7 @@ async def edit_message(
message = self._create_message(data)
if view and not view.is_finished():
view.message = message
view.refresh(message.components)
view._refresh(message.components)
if view.is_dispatchable():
self._state.store_view(view, message_id)
return message
Expand Down