Browse Source

chore: fix messy code on ViewStore.schedule_dynamic_item_call

pull/10166/head
DA-344 1 month ago
parent
commit
4bd97e1c71
  1. 37
      discord/ui/view.py

37
discord/ui/view.py

@ -1129,9 +1129,9 @@ class ViewStore:
view = view_cls.from_message(interaction.message, timeout=None) view = view_cls.from_message(interaction.message, timeout=None)
try: try:
base_item_index, base_item = next( base_item = next(
(index, child) child
for index, child in enumerate(view.walk_children()) for child in view.walk_children()
if child.type.value == component_type and getattr(child, 'custom_id', None) == custom_id if child.type.value == component_type and getattr(child, 'custom_id', None) == custom_id
) )
except StopIteration: except StopIteration:
@ -1144,23 +1144,24 @@ class ViewStore:
return return
# Swap the item in the view or parent with our new dynamic item # Swap the item in the view or parent with our new dynamic item
# if the item has a parent, then it is a nested item # Prioritize the item parent:
if base_item._parent: parent = base_item._parent or view
# try and find the item reference on the parent children
try: try:
child_index = base_item._parent._children.index(base_item) # type: ignore child_index = parent._children.index(base_item) # type: ignore
except ValueError: except ValueError:
# there can be cases in which the button is an accessory # handle cases in which the item is a section accesory
# of a section, so the index will fail if getattr(base_item._parent, '__discord_ui_section__', False):
if getattr(base_item._parent, '__discord_ui_section__', False): if (
accessory = base_item._parent.accessory # type: ignore base_item._parent.accessory.type.value == component_type # type: ignore
if accessory.type.value == component_type and getattr(accessory, 'custom_id', None) == custom_id: and getattr(base_item._parent.accessory, 'custom_id', None) == custom_id # type: ignore
base_item._parent.accessory = item # type: ignore ):
base_item._parent.accessory = item # type: ignore
else: else:
base_item._parent._children[child_index] = item # type: ignore return
# if it does not have a parent then it is at top level
else: else:
view._children[base_item_index] = item # type: ignore parent._children[child_index] = item # type: ignore
item._view = view item._view = view
item._rendered_row = base_item._rendered_row item._rendered_row = base_item._rendered_row
item._refresh_state(interaction, interaction.data) # type: ignore item._refresh_state(interaction, interaction.data) # type: ignore

Loading…
Cancel
Save