mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-06-07 04:17:16 +00:00
Add item parameter to DynamicItem.from_custom_id
This commit is contained in:
parent
7c3868ef3b
commit
f793af0456
@ -169,7 +169,9 @@ class DynamicItem(Generic[BaseT], Item['View']):
|
||||
return self.item.width
|
||||
|
||||
@classmethod
|
||||
async def from_custom_id(cls: Type[Self], interaction: Interaction[ClientT], match: re.Match[str], /) -> Self:
|
||||
async def from_custom_id(
|
||||
cls: Type[Self], interaction: Interaction[ClientT], item: Item[Any], match: re.Match[str], /
|
||||
) -> Self:
|
||||
"""|coro|
|
||||
|
||||
A classmethod that is called when the ``custom_id`` of a component matches the
|
||||
@ -192,6 +194,8 @@ class DynamicItem(Generic[BaseT], Item['View']):
|
||||
------------
|
||||
interaction: :class:`~discord.Interaction`
|
||||
The interaction that the component belongs to.
|
||||
item: :class:`~discord.ui.Item`
|
||||
The base item that is being dispatched.
|
||||
match: ``re.Match``
|
||||
The match object that was created from the ``template``
|
||||
matching the ``custom_id``.
|
||||
|
@ -604,15 +604,33 @@ class ViewStore:
|
||||
component_type: int,
|
||||
factory: Type[DynamicItem[Item[Any]]],
|
||||
interaction: Interaction,
|
||||
custom_id: str,
|
||||
match: re.Match[str],
|
||||
) -> None:
|
||||
if interaction.message is None:
|
||||
return
|
||||
|
||||
view = View.from_message(interaction.message)
|
||||
|
||||
base_item_index: Optional[int] = None
|
||||
for index, child in enumerate(view._children):
|
||||
if child.type.value == component_type and getattr(child, 'custom_id', None) == custom_id:
|
||||
base_item_index = index
|
||||
break
|
||||
|
||||
if base_item_index is None:
|
||||
return
|
||||
|
||||
base_item = view._children[base_item_index]
|
||||
try:
|
||||
item = await factory.from_custom_id(interaction, match)
|
||||
item = await factory.from_custom_id(interaction, base_item, match)
|
||||
except Exception:
|
||||
_log.exception('Ignoring exception in dynamic item creation for %r', factory)
|
||||
return
|
||||
|
||||
# Unfortunately cannot set Item.view here...
|
||||
# Swap the item in the view with our new dynamic item
|
||||
view._children[base_item_index] = item
|
||||
item._view = view
|
||||
item._refresh_state(interaction, interaction.data) # type: ignore
|
||||
|
||||
try:
|
||||
@ -623,17 +641,6 @@ class ViewStore:
|
||||
if not allow:
|
||||
return
|
||||
|
||||
if interaction.message is None:
|
||||
item._view = None
|
||||
else:
|
||||
item._view = view = View.from_message(interaction.message)
|
||||
|
||||
# Find the original item and replace it with the dynamic item
|
||||
for index, child in enumerate(view._children):
|
||||
if child.type.value == component_type and getattr(child, 'custom_id', None) == item.custom_id:
|
||||
view._children[index] = item
|
||||
break
|
||||
|
||||
try:
|
||||
await item.callback(interaction)
|
||||
except Exception:
|
||||
@ -644,7 +651,7 @@ class ViewStore:
|
||||
match = pattern.fullmatch(custom_id)
|
||||
if match is not None:
|
||||
asyncio.create_task(
|
||||
self.schedule_dynamic_item_call(component_type, item, interaction, match),
|
||||
self.schedule_dynamic_item_call(component_type, item, interaction, custom_id, match),
|
||||
name=f'discord-ui-dynamic-item-{item.__name__}-{custom_id}',
|
||||
)
|
||||
|
||||
|
@ -45,10 +45,10 @@ class DynamicCounter(
|
||||
|
||||
# This method actually extracts the information from the custom ID and creates the item.
|
||||
@classmethod
|
||||
async def from_custom_id(cls, interaction: discord.Interaction, match: re.Match[str], /):
|
||||
async def from_custom_id(cls, interaction: discord.Interaction, item: discord.ui.Button, match: re.Match[str], /):
|
||||
count = int(match['count'])
|
||||
user_id = int(match['id'])
|
||||
return cls(count, user_id)
|
||||
return cls(user_id, count=count)
|
||||
|
||||
# We want to ensure that our button is only called by the user who created it.
|
||||
async def interaction_check(self, interaction: discord.Interaction) -> bool:
|
||||
|
@ -51,7 +51,7 @@ class DynamicButton(discord.ui.DynamicItem[discord.ui.Button], template=r'button
|
||||
|
||||
# This is called when the button is clicked and the custom_id matches the template.
|
||||
@classmethod
|
||||
async def from_custom_id(cls, interaction: discord.Interaction, match: re.Match[str], /):
|
||||
async def from_custom_id(cls, interaction: discord.Interaction, item: discord.ui.Button, match: re.Match[str], /):
|
||||
user_id = int(match['id'])
|
||||
return cls(user_id)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user