Skip to content

Commit a1263df

Browse files
authored
feat: add premium buttons (#1701)
1 parent de8c91d commit a1263df

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

interactions/models/discord/components.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import discord_typings
88

99
import interactions.models.discord as d_models
10-
from interactions.models.discord.snowflake import Snowflake
10+
from interactions.models.discord.snowflake import Snowflake, Snowflake_Type
1111
from interactions.client.const import ACTION_ROW_MAX_ITEMS, MISSING
1212
from interactions.client.mixins.serialization import DictSerializationMixin
1313
from interactions.models.discord.base import DiscordObject
@@ -212,6 +212,7 @@ class Button(InteractiveComponent):
212212
label optional[str]: The text that appears on the button, max 80 characters.
213213
emoji optional[Union[PartialEmoji, dict, str]]: The emoji that appears on the button.
214214
custom_id Optional[str]: A developer-defined identifier for the button, max 100 characters.
215+
sku_id: Optional[Snowflake_Type]: Identifier for a purchasable SKU, only available when using premium-style buttons
215216
url Optional[str]: A url for link-style buttons.
216217
disabled bool: Disable the button and make it not interactable, default false.
217218
@@ -226,13 +227,15 @@ def __init__(
226227
label: str | None = None,
227228
emoji: "PartialEmoji | None | str" = None,
228229
custom_id: str | None = None,
230+
sku_id: Snowflake_Type | None = None,
229231
url: str | None = None,
230232
disabled: bool = False,
231233
) -> None:
232234
self.style: ButtonStyle = ButtonStyle(style)
233235
self.label: str | None = label
234236
self.emoji: "PartialEmoji | None" = emoji
235237
self.custom_id: str | None = custom_id
238+
self.sku_id: Snowflake_Type | None = sku_id
236239
self.url: str | None = url
237240
self.disabled: bool = disabled
238241

@@ -244,10 +247,17 @@ def __init__(
244247
if self.url is None:
245248
raise ValueError("URL buttons must have a url.")
246249

250+
elif self.style == ButtonStyle.PREMIUM:
251+
if any(p is not None for p in (self.custom_id, self.url, self.emoji, self.label)):
252+
raise ValueError("Premium buttons cannot have a custom_id, url, emoji, or label.")
253+
if self.sku_id is None:
254+
raise ValueError("Premium buttons must have a sku_id.")
255+
247256
elif self.custom_id is None:
248257
self.custom_id = str(uuid.uuid4())
249-
if not self.label and not self.emoji:
250-
raise ValueError("Buttons must have a label or an emoji.")
258+
259+
if self.style != ButtonStyle.PREMIUM and not self.label and not self.emoji:
260+
raise ValueError("Non-premium buttons must have a label or an emoji.")
251261

252262
if isinstance(self.emoji, str):
253263
self.emoji = PartialEmoji.from_str(self.emoji)
@@ -261,12 +271,13 @@ def from_dict(cls, data: discord_typings.ButtonComponentData) -> "Button":
261271
label=data.get("label"),
262272
emoji=emoji,
263273
custom_id=data.get("custom_id"),
274+
sku_id=data.get("sku_id"),
264275
url=data.get("url"),
265276
disabled=data.get("disabled", False),
266277
)
267278

268279
def __repr__(self) -> str:
269-
return f"<{self.__class__.__name__} type={self.type} style={self.style} label={self.label} emoji={self.emoji} custom_id={self.custom_id} url={self.url} disabled={self.disabled}>"
280+
return f"<{self.__class__.__name__} type={self.type} style={self.style} label={self.label} emoji={self.emoji} custom_id={self.custom_id} sku_id={self.sku_id} url={self.url} disabled={self.disabled}>"
270281

271282
def to_dict(self) -> discord_typings.ButtonComponentData:
272283
emoji = self.emoji.to_dict() if self.emoji else None
@@ -279,6 +290,7 @@ def to_dict(self) -> discord_typings.ButtonComponentData:
279290
"label": self.label,
280291
"emoji": emoji,
281292
"custom_id": self.custom_id,
293+
"sku_id": self.sku_id,
282294
"url": self.url,
283295
"disabled": self.disabled,
284296
}

interactions/models/discord/enums.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,8 @@ class ButtonStyle(CursedIntEnum):
734734
"""red"""
735735
LINK = 5
736736
"""url button"""
737+
PREMIUM = 6
738+
"""premium button"""
737739

738740
# Aliases
739741
BLUE = 1

interactions/models/internal/context.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,10 @@ async def send_premium_required(self) -> None:
457457
"""
458458
Send a premium required response.
459459
460+
!!! warn
461+
This response has been deprecated by Discord and will be removed in the future.
462+
Use a button with the PREMIUM type instead.
463+
460464
When used, the user will be prompted to subscribe to premium to use this feature.
461465
Only available for applications with monetization enabled.
462466
"""

0 commit comments

Comments
 (0)