Skip to content

Commit

Permalink
Add clear_dynamic_items param to View
Browse files Browse the repository at this point in the history
If set to False it will not remove DynamicItem from the view store
  • Loading branch information
PredaaA committed Jan 15, 2024
1 parent fca44f8 commit c201cfe
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions discord/ui/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ class View:
timeout: Optional[:class:`float`]
Timeout in seconds from last interaction with the UI before no longer accepting input.
If ``None`` then there is no timeout.
clear_dynamic_items: :class:`bool`
Whether to clear dynamic items from the view store when it is stopped.
Defaults to ``True``.
"""

__discord_ui_view__: ClassVar[bool] = True
Expand Down Expand Up @@ -183,8 +186,9 @@ def _init_children(self) -> List[Item[Self]]:
children.append(item)
return children

def __init__(self, *, timeout: Optional[float] = 180.0):
def __init__(self, *, timeout: Optional[float] = 180.0, clear_dynamic_items: bool = True):
self.__timeout = timeout
self._clear_dynamic_items = clear_dynamic_items
self._children: List[Item[Self]] = self._init_children()
self.__weights = _ViewWeights(self._children)
self.id: str = os.urandom(16).hex()
Expand Down Expand Up @@ -596,7 +600,7 @@ def remove_view(self, view: View) -> None:
dispatch_info = self._views.get(view._cache_key)
if dispatch_info:
for item in view._children:
if isinstance(item, DynamicItem):
if isinstance(item, DynamicItem) and view._clear_dynamic_items:
pattern = item.__discord_ui_compiled_template__
self._dynamic_items.pop(pattern, None)
elif item.is_dispatchable():
Expand Down

0 comments on commit c201cfe

Please sign in to comment.