You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
168 lines
5.3 KiB
168 lines
5.3 KiB
"""
|
|
The MIT License (MIT)
|
|
|
|
Copyright (c) 2015-present Rapptz
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a
|
|
copy of this software and associated documentation files (the "Software"),
|
|
to deal in the Software without restriction, including without limitation
|
|
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
and/or sell copies of the Software, and to permit persons to whom the
|
|
Software is furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
all copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
DEALINGS IN THE SOFTWARE.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any, Callable, Coroutine, Dict, Generic, Optional, TYPE_CHECKING, Tuple, Type, TypeVar
|
|
|
|
from ..interactions import Interaction
|
|
from .._types import ClientT
|
|
|
|
# fmt: off
|
|
__all__ = (
|
|
'Item',
|
|
)
|
|
# fmt: on
|
|
|
|
if TYPE_CHECKING:
|
|
from ..enums import ComponentType
|
|
from .view import View
|
|
from ..components import Component
|
|
|
|
I = TypeVar('I', bound='Item[Any]')
|
|
V = TypeVar('V', bound='View', covariant=True)
|
|
ItemCallbackType = Callable[[V, Interaction[Any], I], Coroutine[Any, Any, Any]]
|
|
|
|
|
|
class Item(Generic[V]):
|
|
"""Represents the base UI item that all UI components inherit from.
|
|
|
|
The current UI items supported are:
|
|
|
|
- :class:`discord.ui.Button`
|
|
- :class:`discord.ui.Select`
|
|
- :class:`discord.ui.TextInput`
|
|
|
|
.. versionadded:: 2.0
|
|
"""
|
|
|
|
__item_repr_attributes__: Tuple[str, ...] = ('row',)
|
|
|
|
def __init__(self):
|
|
self._view: Optional[V] = None
|
|
self._row: Optional[int] = None
|
|
self._rendered_row: Optional[int] = None
|
|
# This works mostly well but there is a gotcha with
|
|
# the interaction with from_component, since that technically provides
|
|
# a custom_id most dispatchable items would get this set to True even though
|
|
# it might not be provided by the library user. However, this edge case doesn't
|
|
# actually affect the intended purpose of this check because from_component is
|
|
# only called upon edit and we're mainly interested during initial creation time.
|
|
self._provided_custom_id: bool = False
|
|
|
|
def to_component_dict(self) -> Dict[str, Any]:
|
|
raise NotImplementedError
|
|
|
|
def _refresh_component(self, component: Component) -> None:
|
|
return None
|
|
|
|
def _refresh_state(self, interaction: Interaction, data: Dict[str, Any]) -> None:
|
|
return None
|
|
|
|
@classmethod
|
|
def from_component(cls: Type[I], component: Component) -> I:
|
|
return cls()
|
|
|
|
@property
|
|
def type(self) -> ComponentType:
|
|
raise NotImplementedError
|
|
|
|
def is_dispatchable(self) -> bool:
|
|
return False
|
|
|
|
def is_persistent(self) -> bool:
|
|
return self._provided_custom_id
|
|
|
|
def __repr__(self) -> str:
|
|
attrs = ' '.join(f'{key}={getattr(self, key)!r}' for key in self.__item_repr_attributes__)
|
|
return f'<{self.__class__.__name__} {attrs}>'
|
|
|
|
@property
|
|
def row(self) -> Optional[int]:
|
|
return self._row
|
|
|
|
@row.setter
|
|
def row(self, value: Optional[int]) -> None:
|
|
if value is None:
|
|
self._row = None
|
|
elif 5 > value >= 0:
|
|
self._row = value
|
|
else:
|
|
raise ValueError('row cannot be negative or greater than or equal to 5')
|
|
|
|
@property
|
|
def width(self) -> int:
|
|
return 1
|
|
|
|
@property
|
|
def view(self) -> Optional[V]:
|
|
"""Optional[:class:`View`]: The underlying view for this item."""
|
|
return self._view
|
|
|
|
async def callback(self, interaction: Interaction[ClientT]) -> Any:
|
|
"""|coro|
|
|
|
|
The callback associated with this UI item.
|
|
|
|
This can be overridden by subclasses.
|
|
|
|
Parameters
|
|
-----------
|
|
interaction: :class:`.Interaction`
|
|
The interaction that triggered this UI item.
|
|
"""
|
|
pass
|
|
|
|
async def interaction_check(self, interaction: Interaction[ClientT], /) -> bool:
|
|
"""|coro|
|
|
|
|
A callback that is called when an interaction happens within this item
|
|
that checks whether the callback should be processed.
|
|
|
|
This is useful to override if, for example, you want to ensure that the
|
|
interaction author is a given user.
|
|
|
|
The default implementation of this returns ``True``.
|
|
|
|
.. note::
|
|
|
|
If an exception occurs within the body then the check
|
|
is considered a failure and :meth:`discord.ui.View.on_error` is called.
|
|
|
|
For :class:`~discord.ui.DynamicItem` this does not call the ``on_error``
|
|
handler.
|
|
|
|
.. versionadded:: 2.4
|
|
|
|
Parameters
|
|
-----------
|
|
interaction: :class:`~discord.Interaction`
|
|
The interaction that occurred.
|
|
|
|
Returns
|
|
---------
|
|
:class:`bool`
|
|
Whether the callback should be called.
|
|
"""
|
|
return True
|
|
|