Add ButtonStyle.premium

This commit is contained in:
DA344 2024-06-01 09:48:50 +02:00 committed by GitHub
parent 51142743bc
commit 356474ffb9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 52 additions and 34 deletions

View File

@ -170,6 +170,10 @@ class Button(Component):
The label of the button, if any. The label of the button, if any.
emoji: Optional[:class:`PartialEmoji`] emoji: Optional[:class:`PartialEmoji`]
The emoji of the button, if available. The emoji of the button, if available.
sku_id: Optional[:class:`int`]
The SKU ID this button sends you to, if available.
.. versionadded:: 2.4
""" """
__slots__: Tuple[str, ...] = ( __slots__: Tuple[str, ...] = (
@ -179,6 +183,7 @@ class Button(Component):
'disabled', 'disabled',
'label', 'label',
'emoji', 'emoji',
'sku_id',
) )
__repr_info__: ClassVar[Tuple[str, ...]] = __slots__ __repr_info__: ClassVar[Tuple[str, ...]] = __slots__
@ -195,6 +200,11 @@ class Button(Component):
except KeyError: except KeyError:
self.emoji = None self.emoji = None
try:
self.sku_id: Optional[int] = int(data['sku_id'])
except KeyError:
self.sku_id = None
@property @property
def type(self) -> Literal[ComponentType.button]: def type(self) -> Literal[ComponentType.button]:
""":class:`ComponentType`: The type of component.""" """:class:`ComponentType`: The type of component."""
@ -207,6 +217,9 @@ class Button(Component):
'disabled': self.disabled, 'disabled': self.disabled,
} }
if self.sku_id:
payload['sku_id'] = str(self.sku_id)
if self.label: if self.label:
payload['label'] = self.label payload['label'] = self.label

View File

@ -603,7 +603,7 @@ class InteractionResponseType(Enum):
message_update = 7 # for components message_update = 7 # for components
autocomplete_result = 8 autocomplete_result = 8
modal = 9 # for modals modal = 9 # for modals
premium_required = 10 # premium_required = 10 (deprecated)
class VideoQualityMode(Enum): class VideoQualityMode(Enum):
@ -635,6 +635,7 @@ class ButtonStyle(Enum):
success = 3 success = 3
danger = 4 danger = 4
link = 5 link = 5
premium = 6
# Aliases # Aliases
blurple = 1 blurple = 1

View File

@ -1050,38 +1050,6 @@ class InteractionResponse(Generic[ClientT]):
self._parent._state.store_view(modal) self._parent._state.store_view(modal)
self._response_type = InteractionResponseType.modal self._response_type = InteractionResponseType.modal
async def require_premium(self) -> None:
"""|coro|
Sends a message to the user prompting them that a premium purchase is required for this interaction.
This type of response is only available for applications that have a premium SKU set up.
Raises
-------
HTTPException
Sending the response failed.
InteractionResponded
This interaction has already been responded to before.
"""
if self._response_type:
raise InteractionResponded(self._parent)
parent = self._parent
adapter = async_context.get()
http = parent._state.http
params = interaction_response_params(InteractionResponseType.premium_required.value)
await adapter.create_interaction_response(
parent.id,
parent.token,
session=parent._session,
proxy=http.proxy,
proxy_auth=http.proxy_auth,
params=params,
)
self._response_type = InteractionResponseType.premium_required
async def autocomplete(self, choices: Sequence[Choice[ChoiceT]]) -> None: async def autocomplete(self, choices: Sequence[Choice[ChoiceT]]) -> None:
"""|coro| """|coro|

View File

@ -31,7 +31,7 @@ from .emoji import PartialEmoji
from .channel import ChannelType from .channel import ChannelType
ComponentType = Literal[1, 2, 3, 4] ComponentType = Literal[1, 2, 3, 4]
ButtonStyle = Literal[1, 2, 3, 4, 5] ButtonStyle = Literal[1, 2, 3, 4, 5, 6]
TextStyle = Literal[1, 2] TextStyle = Literal[1, 2]
DefaultValueType = Literal['user', 'role', 'channel'] DefaultValueType = Literal['user', 'role', 'channel']
@ -49,6 +49,7 @@ class ButtonComponent(TypedDict):
disabled: NotRequired[bool] disabled: NotRequired[bool]
emoji: NotRequired[PartialEmoji] emoji: NotRequired[PartialEmoji]
label: NotRequired[str] label: NotRequired[str]
sku_id: NotRequired[str]
class SelectOption(TypedDict): class SelectOption(TypedDict):

View File

@ -77,6 +77,10 @@ class Button(Item[V]):
like to control the relative positioning of the row then passing an index is advised. like to control the relative positioning of the row then passing an index is advised.
For example, row=1 will show up before row=2. Defaults to ``None``, which is automatic For example, row=1 will show up before row=2. Defaults to ``None``, which is automatic
ordering. The row number must be between 0 and 4 (i.e. zero indexed). ordering. The row number must be between 0 and 4 (i.e. zero indexed).
sku_id: Optional[:class:`int`]
The SKU ID this button sends you to. Can't be combined with ``url``.
.. versionadded:: 2.4
""" """
__item_repr_attributes__: Tuple[str, ...] = ( __item_repr_attributes__: Tuple[str, ...] = (
@ -86,6 +90,7 @@ class Button(Item[V]):
'label', 'label',
'emoji', 'emoji',
'row', 'row',
'sku_id',
) )
def __init__( def __init__(
@ -98,6 +103,7 @@ class Button(Item[V]):
url: Optional[str] = None, url: Optional[str] = None,
emoji: Optional[Union[str, Emoji, PartialEmoji]] = None, emoji: Optional[Union[str, Emoji, PartialEmoji]] = None,
row: Optional[int] = None, row: Optional[int] = None,
sku_id: Optional[int] = None,
): ):
super().__init__() super().__init__()
if custom_id is not None and url is not None: if custom_id is not None and url is not None:
@ -113,6 +119,9 @@ class Button(Item[V]):
if url is not None: if url is not None:
style = ButtonStyle.link style = ButtonStyle.link
if sku_id is not None:
style = ButtonStyle.premium
if emoji is not None: if emoji is not None:
if isinstance(emoji, str): if isinstance(emoji, str):
emoji = PartialEmoji.from_str(emoji) emoji = PartialEmoji.from_str(emoji)
@ -128,6 +137,7 @@ class Button(Item[V]):
label=label, label=label,
style=style, style=style,
emoji=emoji, emoji=emoji,
sku_id=sku_id,
) )
self.row = row self.row = row
@ -202,6 +212,19 @@ class Button(Item[V]):
else: else:
self._underlying.emoji = None self._underlying.emoji = None
@property
def sku_id(self) -> Optional[int]:
"""Optional[:class:`int`]: The SKU ID this button sends you to.
.. versionadded:: 2.4
"""
return self._underlying.sku_id
@sku_id.setter
def sku_id(self, value: Optional[int]) -> None:
self.style = ButtonStyle.premium
self._underlying.sku_id = value
@classmethod @classmethod
def from_component(cls, button: ButtonComponent) -> Self: def from_component(cls, button: ButtonComponent) -> Self:
return cls( return cls(
@ -212,6 +235,7 @@ class Button(Item[V]):
url=button.url, url=button.url,
emoji=button.emoji, emoji=button.emoji,
row=None, row=None,
sku_id=button.sku_id,
) )
@property @property
@ -241,6 +265,7 @@ def button(
style: ButtonStyle = ButtonStyle.secondary, style: ButtonStyle = ButtonStyle.secondary,
emoji: Optional[Union[str, Emoji, PartialEmoji]] = None, emoji: Optional[Union[str, Emoji, PartialEmoji]] = None,
row: Optional[int] = None, row: Optional[int] = None,
sku_id: Optional[int] = None,
) -> Callable[[ItemCallbackType[V, Button[V]]], Button[V]]: ) -> Callable[[ItemCallbackType[V, Button[V]]], Button[V]]:
"""A decorator that attaches a button to a component. """A decorator that attaches a button to a component.
@ -278,6 +303,10 @@ def button(
like to control the relative positioning of the row then passing an index is advised. like to control the relative positioning of the row then passing an index is advised.
For example, row=1 will show up before row=2. Defaults to ``None``, which is automatic For example, row=1 will show up before row=2. Defaults to ``None``, which is automatic
ordering. The row number must be between 0 and 4 (i.e. zero indexed). ordering. The row number must be between 0 and 4 (i.e. zero indexed).
sku_id: Optional[:class:`int`]
The SKU ID this button sends you to. Can't be combined with ``url``.
.. versionadded:: 2.4
""" """
def decorator(func: ItemCallbackType[V, Button[V]]) -> ItemCallbackType[V, Button[V]]: def decorator(func: ItemCallbackType[V, Button[V]]) -> ItemCallbackType[V, Button[V]]:
@ -293,6 +322,7 @@ def button(
'label': label, 'label': label,
'emoji': emoji, 'emoji': emoji,
'row': row, 'row': row,
'sku_id': sku_id,
} }
return func return func

View File

@ -334,7 +334,12 @@ Enumerations
.. attribute:: link .. attribute:: link
Represents a link button. Represents a link button.
.. attribute:: premium
Represents a gradient button denoting that buying a SKU is
required to perform this action.
.. versionadded:: 2.4
.. attribute:: blurple .. attribute:: blurple
An alias for :attr:`primary`. An alias for :attr:`primary`.