mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-09-07 10:22:59 +00:00
Change View callback order to (self, interaction, item)
This is more consistent with the rest of the library which always has the interaction as the first parameter. This has been done before in the command extension as well, the first parameter is always either self or the context.
This commit is contained in:
@ -240,8 +240,8 @@ def button(
|
||||
"""A decorator that attaches a button to a component.
|
||||
|
||||
The function being decorated should have three parameters, ``self`` representing
|
||||
the :class:`discord.ui.View`, the :class:`discord.ui.Button` being pressed and
|
||||
the :class:`discord.Interaction` you receive.
|
||||
the :class:`discord.ui.View`, the :class:`discord.Interaction` you receive and
|
||||
the :class:`discord.ui.Button` being pressed.
|
||||
|
||||
.. note::
|
||||
|
||||
|
@ -41,7 +41,7 @@ if TYPE_CHECKING:
|
||||
|
||||
I = TypeVar('I', bound='Item')
|
||||
V = TypeVar('V', bound='View', covariant=True)
|
||||
ItemCallbackType = Callable[[V, I, Interaction], Coroutine[Any, Any, Any]]
|
||||
ItemCallbackType = Callable[[V, Interaction, I], Coroutine[Any, Any, Any]]
|
||||
|
||||
|
||||
class Item(Generic[V]):
|
||||
|
@ -305,8 +305,8 @@ def select(
|
||||
"""A decorator that attaches a select menu to a component.
|
||||
|
||||
The function being decorated should have three parameters, ``self`` representing
|
||||
the :class:`discord.ui.View`, the :class:`discord.ui.Select` being pressed and
|
||||
the :class:`discord.Interaction` you receive.
|
||||
the :class:`discord.ui.View`, the :class:`discord.Interaction` you receive and
|
||||
the :class:`discord.ui.Select` being used.
|
||||
|
||||
In order to get the selected items that the user has chosen within the callback
|
||||
use :attr:`Select.values`.
|
||||
|
@ -23,7 +23,7 @@ DEALINGS IN THE SOFTWARE.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
from typing import Any, Callable, ClassVar, Dict, Iterator, List, Optional, Sequence, TYPE_CHECKING, Tuple
|
||||
from typing import Any, Callable, ClassVar, Coroutine, Dict, Iterator, List, Optional, Sequence, TYPE_CHECKING, Tuple
|
||||
from functools import partial
|
||||
from itertools import groupby
|
||||
|
||||
@ -127,6 +127,18 @@ class _ViewWeights:
|
||||
self.weights = [0, 0, 0, 0, 0]
|
||||
|
||||
|
||||
class _ViewCallback:
|
||||
__slots__ = ('view', 'callback', 'item')
|
||||
|
||||
def __init__(self, callback: ItemCallbackType[Any, Any], view: View, item: Item[View]) -> None:
|
||||
self.callback: ItemCallbackType[Any, Any] = callback
|
||||
self.view: View = view
|
||||
self.item: Item[View] = item
|
||||
|
||||
def __call__(self, interaction: Interaction) -> Coroutine[Any, Any, Any]:
|
||||
return self.callback(self.view, interaction, self.item)
|
||||
|
||||
|
||||
class View:
|
||||
"""Represents a UI view.
|
||||
|
||||
@ -169,7 +181,7 @@ class View:
|
||||
children = []
|
||||
for func in self.__view_children_items__:
|
||||
item: Item = func.__discord_ui_model_type__(**func.__discord_ui_model_kwargs__)
|
||||
item.callback = partial(func, self, item) # type: ignore
|
||||
item.callback = _ViewCallback(func, self, item)
|
||||
item._view = self
|
||||
setattr(self, func.__name__, item)
|
||||
children.append(item)
|
||||
|
Reference in New Issue
Block a user