mirror of
				https://github.com/Rapptz/discord.py.git
				synced 2025-10-25 02:23:04 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			136 lines
		
	
	
		
			4.3 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			136 lines
		
	
	
		
			4.3 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
| """
 | |
| 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
 |