Skip to content

Commit

Permalink
fix: bugs with link buttons (#252)
Browse files Browse the repository at this point in the history
* fix: bugs with link buttons

* chore: check enum instead of value

* chore: run pre-commit

* Fix component type issue

* Change comment

Co-authored-by: shiftinv <me@shiftinv.cc>
  • Loading branch information
Victorsitou and shiftinv authored Jan 19, 2022
1 parent 767a44c commit 41618b1
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions disnake/ui/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
SelectMenu as SelectComponent,
_component_factory,
)
from ..enums import try_enum_to_int
from ..enums import ComponentType, try_enum_to_int
from .item import Item

__all__ = ("View",)
Expand Down Expand Up @@ -397,23 +397,34 @@ def _dispatch_item(self, item: Item, interaction: MessageInteraction):
)

def refresh(self, components: List[Component]):
# This is pretty hacky at the moment
# fmt: off
# TODO: this is pretty hacky at the moment
old_state: Dict[Tuple[int, str], Item] = {
(item.type.value, item.custom_id): item # type: ignore
for item in self.children
if item.is_dispatchable()
}
# fmt: on
children: List[Item] = []
for component in _walk_all_components(components):
older: Optional[Item] = None
try:
older = old_state[(component.type.value, component.custom_id)] # type: ignore
except (KeyError, AttributeError):
children.append(_component_to_item(component))
else:
# workaround for url buttons, since they're not part of `old_state`
if isinstance(component, ButtonComponent):
for child in self.children:
if (
child.type is ComponentType.button
and child.label == component.label # type: ignore
and child.url == component.url # type: ignore
):
older = child
break

if older:
older.refresh_component(component)
children.append(older)
else:
children.append(_component_to_item(component))

self.children = children

Expand Down

0 comments on commit 41618b1

Please sign in to comment.